Skip to content

Commit fc48101

Browse files
clydinalexeagle
authored andcommitted
feat(@angular-devkit/build-ng-packagr): implement stable architect API
1 parent cdda1c3 commit fc48101

File tree

6 files changed

+131
-66
lines changed

6 files changed

+131
-66
lines changed

packages/angular_devkit/build_ng_packagr/builders.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"builders": {
44
"build": {
55
"class": "./src/build",
6+
"implementation": "./src/build/index2",
67
"schema": "./src/build/schema.json",
78
"description": "Build a library with ng-packagr."
89
}

packages/angular_devkit/build_ng_packagr/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
"semver": "5.6.0"
1414
},
1515
"peerDependencies": {
16-
"ng-packagr": "^2.2.0 || ^3.0.0 || ^4.0.0"
16+
"ng-packagr": "^4.0.0"
1717
},
1818
"devDependencies": {
1919
"@angular/compiler": "^8.0.0-beta.0",
2020
"@angular/compiler-cli": "^8.0.0-beta.0",
21-
"ng-packagr": "^4.2.0",
21+
"ng-packagr": "^4.0.0",
2222
"tsickle": ">=0.34.0",
2323
"tslib": "^1.9.0"
2424
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect/src/index2';
9+
import { resolve } from 'path';
10+
import { Observable, from } from 'rxjs';
11+
import { catchError, mapTo, switchMap } from 'rxjs/operators';
12+
import { Schema as NgPackagrBuilderOptions } from './schema';
13+
14+
async function initialize(
15+
options: NgPackagrBuilderOptions,
16+
root: string,
17+
): Promise<import ('ng-packagr').NgPackagr> {
18+
const packager = (await import('ng-packagr')).ngPackagr();
19+
20+
packager.forProject(resolve(root, options.project));
21+
22+
if (options.tsConfig) {
23+
packager.withTsConfig(resolve(root, options.tsConfig));
24+
}
25+
26+
return packager;
27+
}
28+
29+
export function buildLibrary(
30+
options: NgPackagrBuilderOptions,
31+
context: BuilderContext,
32+
): Observable<BuilderOutput> {
33+
return from(initialize(options, context.workspaceRoot)).pipe(
34+
switchMap(packager => options.watch ? packager.watch() : packager.build()),
35+
mapTo({ success: true }),
36+
catchError(error => {
37+
context.reportStatus('Error: ' + error);
38+
39+
return [{ success: false }];
40+
}),
41+
);
42+
}
43+
44+
export default createBuilder<Record<string, string> & NgPackagrBuilderOptions>(buildLibrary);

packages/angular_devkit/build_ng_packagr/src/build/index_spec_large.ts

+48-42
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,55 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
9-
import { TargetSpecifier } from '@angular-devkit/architect';
10-
import { TestProjectHost, runTargetSpec } from '@angular-devkit/architect/testing';
11-
import { join, normalize, virtualFs } from '@angular-devkit/core';
12-
import { debounceTime, map, take, tap } from 'rxjs/operators';
13-
14-
const devkitRoot = normalize((global as any)._DevKitRoot); // tslint:disable-line:no-any
15-
const workspaceRoot = join(devkitRoot, 'tests/angular_devkit/build_ng_packagr/ng-packaged/');
16-
export const host = new TestProjectHost(workspaceRoot);
8+
import { WorkspaceNodeModulesArchitectHost } from '@angular-devkit/architect/node';
9+
import { Architect } from '@angular-devkit/architect/src/index2';
10+
import { TestProjectHost } from '@angular-devkit/architect/testing';
11+
import { TestingArchitectHost } from '@angular-devkit/architect/testing/testing-architect-host';
12+
import { experimental, join, normalize, schema, virtualFs } from '@angular-devkit/core';
13+
import { map, take, tap } from 'rxjs/operators';
14+
15+
const devkitRoot = (global as unknown as { _DevKitRoot: string})._DevKitRoot;
16+
const workspaceRoot = join(
17+
normalize(devkitRoot),
18+
'tests/angular_devkit/build_ng_packagr/ng-packaged/',
19+
);
1720

1821
describe('NgPackagr Builder', () => {
19-
beforeEach(done => host.initialize().toPromise().then(done, done.fail));
20-
afterEach(done => host.restore().toPromise().then(done, done.fail));
21-
22-
it('works', (done) => {
23-
const targetSpec: TargetSpecifier = { project: 'lib', target: 'build' };
24-
25-
runTargetSpec(host, targetSpec).pipe(
26-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
27-
).toPromise().then(done, done.fail);
22+
const host = new TestProjectHost(workspaceRoot);
23+
let architect: Architect;
24+
25+
beforeEach(async () => {
26+
await host.initialize().toPromise();
27+
28+
const registry = new schema.CoreSchemaRegistry();
29+
registry.addPostTransform(schema.transforms.addUndefinedDefaults);
30+
31+
const workspace = await experimental.workspace.Workspace.fromPath(host, host.root(), registry);
32+
const architectHost = new TestingArchitectHost(
33+
host.root(),
34+
host.root(),
35+
new WorkspaceNodeModulesArchitectHost(workspace, host.root()),
36+
);
37+
architect = new Architect(architectHost, registry);
2838
});
2939

30-
it('tests works', (done) => {
31-
const targetSpec: TargetSpecifier = { project: 'lib', target: 'test' };
40+
afterEach(() => host.restore().toPromise());
3241

33-
runTargetSpec(host, targetSpec).pipe(
34-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
35-
).toPromise().then(done, done.fail);
36-
});
42+
it('builds and packages a library', async () => {
43+
const run = await architect.scheduleTarget({ project: 'lib', target: 'build' });
3744

38-
it('lint works', (done) => {
39-
const targetSpec: TargetSpecifier = { project: 'lib', target: 'lint' };
45+
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: true }));
4046

41-
runTargetSpec(host, targetSpec).pipe(
42-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
43-
).toPromise().then(done, done.fail);
44-
});
47+
await run.stop();
4548

46-
it('rebuilds on TS file changes', (done) => {
47-
const targetSpec: TargetSpecifier = { project: 'lib', target: 'build' };
49+
expect(host.scopedSync().exists(normalize('./dist/lib/fesm5/lib.js'))).toBe(true);
50+
const content = virtualFs.fileBufferToString(
51+
host.scopedSync().read(normalize('./dist/lib/fesm5/lib.js')),
52+
);
53+
expect(content).toContain('lib works');
54+
});
4855

56+
it('rebuilds on TS file changes', async () => {
4957
const goldenValueFiles: { [path: string]: string } = {
5058
'projects/lib/src/lib/lib.component.ts': `
5159
import { Component } from '@angular/core';
@@ -58,16 +66,14 @@ describe('NgPackagr Builder', () => {
5866
`,
5967
};
6068

61-
const overrides = { watch: true };
69+
const run = await architect.scheduleTarget(
70+
{ project: 'lib', target: 'build' },
71+
{ watch: true },
72+
);
6273

6374
let buildNumber = 0;
6475

65-
runTargetSpec(host, targetSpec, overrides)
66-
.pipe(
67-
// We must debounce on watch mode because file watchers are not very accurate.
68-
// Changes from just before a process runs can be picked up and cause rebuilds.
69-
// In this case, cleanup from the test right before this one causes a few rebuilds.
70-
debounceTime(1000),
76+
await run.output.pipe(
7177
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
7278
map(() => {
7379
const fileName = './dist/lib/fesm5/lib.js';
@@ -93,8 +99,8 @@ describe('NgPackagr Builder', () => {
9399
}
94100
}),
95101
take(2),
96-
)
97-
.toPromise()
98-
.then(done, done.fail);
102+
).toPromise();
103+
104+
await run.stop();
99105
});
100106
});

packages/angular_devkit/build_ng_packagr/src/build/schema.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
23
"title": "ng-packagr Target",
34
"description": "ng-packagr target options for Build Architect.",
45
"type": "object",
56
"properties": {
67
"project": {
78
"type": "string",
8-
"description": "The file path of the package.json for distribution via npm."
9+
"description": "The file path for the ng-packagr configuration file, relative to the current workspace."
910
},
1011
"tsConfig": {
1112
"type": "string",

yarn.lock

+34-21
Original file line numberDiff line numberDiff line change
@@ -1642,10 +1642,10 @@ builtin-modules@^1.0.0, builtin-modules@^1.1.1:
16421642
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"
16431643
integrity sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=
16441644

1645-
builtin-modules@^2.0.0:
1646-
version "2.0.0"
1647-
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-2.0.0.tgz#60b7ef5ae6546bd7deefa74b08b62a43a232648e"
1648-
integrity sha512-3U5kUA5VPsRUA3nofm/BXX7GVHKfxz0hOBAPxXrIvHzlDRkQVqEn6yi8QJegxl4LzOHLdvb7XF5dVawa/VVYBg==
1645+
builtin-modules@^3.0.0:
1646+
version "3.0.0"
1647+
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.0.0.tgz#1e587d44b006620d90286cc7a9238bbc6129cab1"
1648+
integrity sha512-hMIeU4K2ilbXV6Uv93ZZ0Avg/M91RaKXucQ+4me2Do1txxBDyDZWCBa5bJSLqoNTRpXTLwEzIk1KmloenDDjhg==
16491649

16501650
builtin-status-codes@^3.0.0:
16511651
version "3.0.0"
@@ -6229,10 +6229,10 @@ neo-async@^2.5.0:
62296229
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.0.tgz#b9d15e4d71c6762908654b5183ed38b753340835"
62306230
integrity sha512-MFh0d/Wa7vkKO3Y3LlacqAEeHK0mckVqzDieUKTT+KGxi+zIpeVsFxymkIiRpbpDziHc290Xr9A1O4Om7otoRA==
62316231

6232-
ng-packagr@^4.2.0:
6233-
version "4.4.0"
6234-
resolved "https://registry.yarnpkg.com/ng-packagr/-/ng-packagr-4.4.0.tgz#b868a829b5743870c6392491de3faf4e4cec6f4d"
6235-
integrity sha512-dLpC/kmQsdbkL96ZclGjNRhq/J4MwpPKwPYNom74lvXqFC2jbbT/fnwmxX9WKXjvE8MEGsg2D2x8MsRURiNscg==
6232+
ng-packagr@^4.0.0:
6233+
version "4.7.1"
6234+
resolved "https://registry.yarnpkg.com/ng-packagr/-/ng-packagr-4.7.1.tgz#a3e8fd2a7b70573a3a7759c4c57408a3b0e8ce11"
6235+
integrity sha512-MIPKxyrnV22fS3wSfst2XjwWOonFKujVVEnIehYJhiu8GOg37bCdbbr9plsE1jRDmDAUz6M1MvdKibUrJyRp6Q==
62366236
dependencies:
62376237
"@ngtools/json-schema" "^1.1.0"
62386238
autoprefixer "^9.0.0"
@@ -6248,14 +6248,15 @@ ng-packagr@^4.2.0:
62486248
less-plugin-npm-import "^2.1.0"
62496249
node-sass "^4.9.3"
62506250
node-sass-tilde-importer "^1.0.0"
6251+
opencollective-postinstall "^2.0.1"
62516252
postcss "^7.0.0"
62526253
postcss-url "^8.0.0"
62536254
read-pkg-up "^4.0.0"
62546255
rimraf "^2.6.1"
6255-
rollup "^0.66.0"
6256+
rollup "^0.67.0"
62566257
rollup-plugin-commonjs "^9.1.3"
62576258
rollup-plugin-json "^3.1.0"
6258-
rollup-plugin-node-resolve "^3.0.0"
6259+
rollup-plugin-node-resolve "^4.0.0"
62596260
rollup-plugin-sourcemaps "^0.4.2"
62606261
rxjs "^6.0.0"
62616262
stylus "^0.54.5"
@@ -6672,6 +6673,11 @@ onetime@^2.0.0:
66726673
dependencies:
66736674
mimic-fn "^1.0.0"
66746675

6676+
opencollective-postinstall@^2.0.1:
6677+
version "2.0.2"
6678+
resolved "https://registry.yarnpkg.com/opencollective-postinstall/-/opencollective-postinstall-2.0.2.tgz#5657f1bede69b6e33a45939b061eb53d3c6c3a89"
6679+
integrity sha512-pVOEP16TrAO2/fjej1IdOyupJY8KDUM1CvsaScRbw6oddvpQoOfGk4ywha0HKKVAD6RkW4x6Q+tNBwhf3Bgpuw==
6680+
66756681
opn@5.4.0, opn@^5.1.0:
66766682
version "5.4.0"
66776683
resolved "https://registry.yarnpkg.com/opn/-/opn-5.4.0.tgz#cb545e7aab78562beb11aa3bfabc7042e1761035"
@@ -7051,7 +7057,7 @@ path-key@^2.0.0, path-key@^2.0.1:
70517057
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"
70527058
integrity sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=
70537059

7054-
path-parse@^1.0.5:
7060+
path-parse@^1.0.5, path-parse@^1.0.6:
70557061
version "1.0.6"
70567062
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
70577063
integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==
@@ -7910,6 +7916,13 @@ resolve@^1.1.5, resolve@^1.1.6, resolve@^1.1.7, resolve@^1.3.2, resolve@^1.8.1:
79107916
dependencies:
79117917
path-parse "^1.0.5"
79127918

7919+
resolve@^1.10.0:
7920+
version "1.10.0"
7921+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.10.0.tgz#3bdaaeaf45cc07f375656dfd2e54ed0810b101ba"
7922+
integrity sha512-3sUr9aq5OfSg2S9pNtPA9hL1FVEAjvfOC4leW0SNf/mpnaakz2a9femSd6LqAww2RaFctwyf1lCqnTHuF1rxDg==
7923+
dependencies:
7924+
path-parse "^1.0.6"
7925+
79137926
responselike@^1.0.2:
79147927
version "1.0.2"
79157928
resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7"
@@ -7972,14 +7985,14 @@ rollup-plugin-json@^3.1.0:
79727985
dependencies:
79737986
rollup-pluginutils "^2.3.1"
79747987

7975-
rollup-plugin-node-resolve@^3.0.0:
7976-
version "3.4.0"
7977-
resolved "https://registry.yarnpkg.com/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-3.4.0.tgz#908585eda12e393caac7498715a01e08606abc89"
7978-
integrity sha512-PJcd85dxfSBWih84ozRtBkB731OjXk0KnzN0oGp7WOWcarAFkVa71cV5hTJg2qpVsV2U8EUwrzHP3tvy9vS3qg==
7988+
rollup-plugin-node-resolve@^4.0.0:
7989+
version "4.0.1"
7990+
resolved "https://registry.yarnpkg.com/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-4.0.1.tgz#f95765d174e5daeef9ea6268566141f53aa9d422"
7991+
integrity sha512-fSS7YDuCe0gYqKsr5OvxMloeZYUSgN43Ypi1WeRZzQcWtHgFayV5tUSPYpxuaioIIWaBXl6NrVk0T2/sKwueLg==
79797992
dependencies:
7980-
builtin-modules "^2.0.0"
7993+
builtin-modules "^3.0.0"
79817994
is-module "^1.0.0"
7982-
resolve "^1.1.6"
7995+
resolve "^1.10.0"
79837996

79847997
rollup-plugin-sourcemaps@^0.4.2:
79857998
version "0.4.2"
@@ -7997,10 +8010,10 @@ rollup-pluginutils@^2.0.1, rollup-pluginutils@^2.3.1, rollup-pluginutils@^2.3.3:
79978010
estree-walker "^0.5.2"
79988011
micromatch "^2.3.11"
79998012

8000-
rollup@^0.66.0:
8001-
version "0.66.6"
8002-
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.66.6.tgz#ce7d6185beb7acea644ce220c25e71ae03275482"
8003-
integrity sha512-J7/SWanrcb83vfIHqa8+aVVGzy457GcjA6GVZEnD0x2u4OnOd0Q1pCrEoNe8yLwM6z6LZP02zBT2uW0yh5TqOw==
8013+
rollup@^0.67.0:
8014+
version "0.67.4"
8015+
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.67.4.tgz#8ed6b0993337f84ec8a0387f824fa6c197e833ec"
8016+
integrity sha512-AVuP73mkb4BBMUmksQ3Jw0jTrBTU1i7rLiUYjFxLZGb3xiFmtVEg40oByphkZAsiL0bJC3hRAJUQos/e5EBd+w==
80048017
dependencies:
80058018
"@types/estree" "0.0.39"
80068019
"@types/node" "*"

0 commit comments

Comments
 (0)