Skip to content

Commit

Permalink
feat: expand api to .withTsConfig(string|TsConfig), .forProject() (
Browse files Browse the repository at this point in the history
…#561)

Closes #557
  • Loading branch information
dherges authored Jan 27, 2018
1 parent 15cfb29 commit 48f3569
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
13 changes: 5 additions & 8 deletions src/lib/commands/build.command.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Command } from './command';
import { ngPackagr, provideProject } from '../ng-v5/packagr';
import { ngPackagr } from '../ng-v5/packagr';

/** CLI arguments passed to `ng-packagr` executable and `build()` command. */
export interface CliArguments {

/** Path to the project file 'package.json', 'ng-package.json', or 'ng-package.js'. */
project: string
project: string;
}

/** @stable */
export const build: Command<CliArguments, void> =
(opts) => ngPackagr()
.withProviders([
provideProject(opts.project)
])
export const build: Command<CliArguments, void> = opts =>
ngPackagr()
.forProject(opts.project)
.build();
6 changes: 3 additions & 3 deletions src/lib/ng-v5/packagr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ describe(`ngPackagr()`, () => {
describe(`withTsConfig()`, () => {
it(`should return self instance for chaining`, () => {
const toBeTested = ngPackagr();
const mockConfig = ('foo' as any) as ng.ParsedConfiguration;
const mockConfig = ({ project: 'foo' } as any) as ng.ParsedConfiguration;
expect(toBeTested.withTsConfig(mockConfig)).to.equal(toBeTested);
});
it(`should override the default tsconfig provider`, () => {
const mockConfig = ('foo' as any) as ng.ParsedConfiguration;
const mockConfig = ({ project: 'foo' } as any) as ng.ParsedConfiguration;
const toBeTested = ngPackagr().withTsConfig(mockConfig);
const tsConfigProviders = toBeTested['providers'].filter(p => (p as any).provide === DEFAULT_TS_CONFIG_TOKEN);

expect(tsConfigProviders).to.have.length(2);
expect((tsConfigProviders[1] as any).useValue).to.equal('foo');
expect((tsConfigProviders[1] as any).useValue).to.be.satisfy(val => val.project === 'foo');
});
});
});
Expand Down
30 changes: 24 additions & 6 deletions src/lib/ng-v5/packagr.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import { ParsedConfiguration } from '@angular/compiler-cli';
import { InjectionToken, Provider, ReflectiveInjector, ValueProvider } from 'injection-js';
import { BUILD_NG_PACKAGE_TOKEN, BUILD_NG_PACKAGE_PROVIDER, BuildCallSignature } from '../steps/build-ng-package';
import { TsConfig, DEFAULT_TS_CONFIG_PROVIDER, DEFAULT_TS_CONFIG_TOKEN } from '../ts/default-tsconfig';
import {
defaultTsConfigFactory,
TsConfig,
DEFAULT_TS_CONFIG_PROVIDER,
DEFAULT_TS_CONFIG_TOKEN
} from '../ts/default-tsconfig';
import { INIT_TS_CONFIG_PROVIDER } from '../ts/init-tsconfig';
import { ENTRY_POINT_TRANSFORMS_PROVIDER } from '../steps/entry-point-transforms';

export class NgPackagr {
constructor(private providers: Provider[]) {}

public forProject(project: string): NgPackagr {
this.providers.push(provideProject(project));

return this;
}

public withProviders(providers: Provider[]): NgPackagr {
this.providers = [...this.providers, ...providers];

return this;
}

/** Overwrites the default TypeScript configuration. */
public withTsConfig(defaultValues: TsConfig): NgPackagr {
this.providers.push({
provide: DEFAULT_TS_CONFIG_TOKEN,
useValue: defaultValues
});
public withTsConfig(defaultValues: TsConfig | string): NgPackagr {
this.providers.push(provideTsConfig(defaultValues));

return this;
}
Expand Down Expand Up @@ -48,3 +57,12 @@ export const provideProject = (project: string): ValueProvider => ({
provide: PROJECT_TOKEN,
useValue: project
});

export const provideTsConfig = (values: TsConfig | string): ValueProvider => {
const tsConfig: TsConfig = typeof values === 'string' ? defaultTsConfigFactory(values) : values;

return {
provide: DEFAULT_TS_CONFIG_TOKEN,
useValue: tsConfig
};
};
8 changes: 6 additions & 2 deletions src/lib/ts/default-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ export type TsConfig = ng.ParsedConfiguration;
/**
* Reads the default TypeScript configuration.
*/
export function defaultTsConfigFactory() {
return ng.readConfiguration(path.resolve(__dirname, 'conf', 'tsconfig.ngc.json'));
export function defaultTsConfigFactory(fileName?: string) {
if (!fileName) {
fileName = path.resolve(__dirname, 'conf', 'tsconfig.ngc.json');
}

return ng.readConfiguration(fileName);
}

export const DEFAULT_TS_CONFIG_TOKEN = new InjectionToken<TsConfig>('ng.v5.defaultTsConfig');
Expand Down

0 comments on commit 48f3569

Please sign in to comment.