Skip to content

Commit 37d2cb4

Browse files
andrewseguinIgorMinar
authored andcommitted
feat(elements): add schematics (angular#23298)
PR Close angular#23298
1 parent ce40e85 commit 37d2cb4

File tree

12 files changed

+292
-3
lines changed

12 files changed

+292
-3
lines changed

BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ node_modules_filegroup(
2929
"tsutils",
3030
"typescript",
3131
"zone.js",
32+
"@angular-devkit/core",
33+
"@angular-devkit/schematics",
3234
"@types",
3335
"@webcomponents/custom-elements",
3436
],

karma-js.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ module.exports = function(config) {
6868
'dist/all/@angular/compiler/test/aot/**',
6969
'dist/all/@angular/compiler/test/render3/**',
7070
'dist/all/@angular/core/test/bundling/**',
71+
'dist/all/@angular/elements/schematics/**',
7172
'dist/all/@angular/examples/**/e2e_test/*',
7273
'dist/all/@angular/language-service/**',
7374
'dist/all/@angular/router/test/**',

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
"commitmsg": "node ./scripts/git/commit-msg.js"
2828
},
2929
"dependencies": {
30+
"@angular-devkit/schematics": "^0.5.5",
31+
"@schematics/angular": "^0.5.4",
3032
"core-js": "^2.4.1",
3133
"reflect-metadata": "^0.1.3",
3234
"rxjs": "^6.0.0-terrific-rc.3",

packages/elements/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
"ng-update": {
2828
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
2929
},
30-
"sideEffects": false
30+
"sideEffects": false,
31+
"schematics": "./schematics/collection.json"
3132
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ts_library")
4+
load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test")
5+
6+
exports_files([
7+
"package.json",
8+
"collection.json",
9+
])
10+
11+
ts_library(
12+
name = "schematics",
13+
srcs = glob(
14+
[
15+
"ng-add/index.ts",
16+
"ng-add/schema.d.ts",
17+
],
18+
),
19+
module_name = "@angular/elements/schematics",
20+
deps = [
21+
"//packages/common",
22+
"//packages/core",
23+
"@rxjs",
24+
],
25+
)
26+
27+
ts_library(
28+
name = "test_lib",
29+
testonly = 1,
30+
srcs = glob(
31+
[
32+
"ng-add/index_spec.ts",
33+
],
34+
),
35+
deps = [
36+
":schematics",
37+
"//packages/common",
38+
"//packages/core",
39+
"@rxjs",
40+
"@rxjs//operators",
41+
],
42+
)
43+
44+
jasmine_node_test(
45+
name = "test",
46+
data = [":collection"],
47+
deps = [
48+
":test_lib",
49+
],
50+
)
51+
52+
genrule(
53+
name = "collection",
54+
srcs = ["collection.json"],
55+
outs = ["test-collection.json"],
56+
cmd = "cp $< $@",
57+
output_to_bindir = 1,
58+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
3+
"schematics": {
4+
"ng-add": {
5+
"description": "Adds the document-register-element polyfill.",
6+
"factory": "./ng-add"
7+
}
8+
}
9+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 {Rule, SchematicContext, Tree, chain, noop} from '@angular-devkit/schematics';
9+
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
10+
import {Schema} from './schema';
11+
12+
export default function(options: Schema): Rule {
13+
return chain([
14+
options && options.skipPackageJson ? noop() : addPackageJsonDependency(), addScript(options)
15+
]);
16+
}
17+
18+
/** Adds a package.json dependency for document-register-element */
19+
function addPackageJsonDependency() {
20+
return (host: Tree, context: SchematicContext) => {
21+
22+
if (host.exists('package.json')) {
23+
const jsonStr = host.read('package.json') !.toString('utf-8');
24+
const json = JSON.parse(jsonStr);
25+
26+
// If there are no dependencies, create an entry for dependencies.
27+
const type = 'dependencies';
28+
if (!json[type]) {
29+
json[type] = {};
30+
}
31+
32+
// If not already present, add the dependency.
33+
const pkg = 'document-register-element';
34+
const version = '^1.7.2';
35+
if (!json[type][pkg]) {
36+
json[type][pkg] = version;
37+
}
38+
39+
// Write the JSON back to package.json
40+
host.overwrite('package.json', JSON.stringify(json, null, 2));
41+
context.logger.log('info', 'Added `document-register-element` as a dependency.');
42+
43+
// Install the dependency
44+
context.addTask(new NodePackageInstallTask());
45+
}
46+
47+
return host;
48+
};
49+
}
50+
51+
/** Adds the document-register-element.js script to the angular CLI json. */
52+
function addScript(options: Schema) {
53+
return (host: Tree, context: SchematicContext) => {
54+
const script = 'node_modules/document-register-element/build/document-register-element.js';
55+
56+
57+
try {
58+
// Handle the new json - angular.json
59+
const angularJsonFile = host.read('angular.json');
60+
if (angularJsonFile) {
61+
const json = JSON.parse(angularJsonFile.toString('utf-8'));
62+
const project = Object.keys(json['projects'])[0] || options.project;
63+
const scripts = json['projects'][project]['architect']['build']['options']['scripts'];
64+
scripts.push({input: script});
65+
host.overwrite('angular.json', JSON.stringify(json, null, 2));
66+
}
67+
} catch (e) {
68+
context.logger.log(
69+
'warn', 'Failed to add the polyfill document-register-element.js to scripts');
70+
}
71+
72+
context.logger.log('info', 'Added document-register-element.js polyfill to scripts');
73+
74+
return host;
75+
};
76+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
9+
import * as path from 'path';
10+
import {Observable} from 'rxjs';
11+
import {concatMap} from 'rxjs/operators';
12+
13+
import {Schema as ElementsOptions} from './schema';
14+
15+
16+
const polyfillPath = 'node_modules/document-register-element/build/document-register-element.js';
17+
18+
// tslint:disable:max-line-length
19+
describe('Elements Schematics', () => {
20+
const schematicRunner = new SchematicTestRunner(
21+
'@angular/elements', path.join(__dirname, '../test-collection.json'), );
22+
const defaultOptions: ElementsOptions = {project: 'bar', skipPackageJson: false};
23+
24+
let appTree: UnitTestTree;
25+
26+
// tslint:disable-next-line:no-any
27+
const workspaceOptions: any = {
28+
name: 'workspace',
29+
newProjectRoot: 'projects',
30+
version: '6.0.0',
31+
};
32+
33+
// tslint:disable-next-line:no-any
34+
const appOptions: any = {
35+
name: 'elements',
36+
inlineStyle: false,
37+
inlineTemplate: false,
38+
routing: false,
39+
style: 'css',
40+
skipTests: false,
41+
};
42+
43+
beforeEach((done) => {
44+
schematicRunner.runExternalSchematicAsync('@schematics/angular', 'workspace', workspaceOptions)
45+
.pipe(concatMap(
46+
(tree) => schematicRunner.runExternalSchematicAsync(
47+
'@schematics/angular', 'application', appOptions, tree)))
48+
.subscribe((tree: UnitTestTree) => appTree = tree, done.fail, done);
49+
});
50+
51+
it('should run the ng-add schematic', () => {
52+
const tree = schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
53+
const configText = tree.readContent('/angular.json');
54+
const config = JSON.parse(configText);
55+
const scripts = config.projects.elements.architect.build.options.scripts;
56+
expect(scripts[0].input).toEqual(polyfillPath);
57+
});
58+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface Schema {
2+
/**
3+
* Skip package.json install.
4+
*/
5+
skipPackageJson: boolean;
6+
7+
/**
8+
* The project that needs the polyfill scripts
9+
*/
10+
project: name;
11+
}

packages/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"bazel",
3232
"compiler-cli/integrationtest",
3333
"platform-server/integrationtest",
34-
"common/locales"
34+
"common/locales",
35+
"elements/schematics"
3536
]
3637

3738
}

tools/tslint/rollupConfigRule.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const sourceFilePathBlacklist = [
2929
/[/\\]packages[/\\]bazel[/\\]/,
3030
/[/\\]packages[/\\]benchpress[/\\]/,
3131
/[/\\]packages[/\\]examples[/\\]/,
32+
/[/\\]packages[/\\]elements[/\\]schematics[/\\]/,
3233

3334
// language-service bundles everything in its UMD, so we don't need a globals. There are
3435
// exceptions but we simply ignore those files from this rule.

yarn.lock

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,56 @@
22
# yarn lockfile v1
33

44

5+
"@angular-devkit/core@0.5.4":
6+
version "0.5.4"
7+
resolved "https://registry.yarnpkg.com/@angular-devkit/core/-/core-0.5.4.tgz#94b7462f5039cf811c7e06db0c87bb2299d61c71"
8+
dependencies:
9+
ajv "~5.5.1"
10+
chokidar "^1.7.0"
11+
rxjs "^6.0.0-beta.3"
12+
source-map "^0.5.6"
13+
14+
"@angular-devkit/core@0.5.5":
15+
version "0.5.5"
16+
resolved "https://registry.yarnpkg.com/@angular-devkit/core/-/core-0.5.5.tgz#659c9ef3f22c3e99c459e325441b1009412a4af6"
17+
dependencies:
18+
ajv "~5.5.1"
19+
chokidar "^1.7.0"
20+
rxjs "^6.0.0-beta.3"
21+
source-map "^0.5.6"
22+
23+
"@angular-devkit/schematics@0.5.4":
24+
version "0.5.4"
25+
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-0.5.4.tgz#6a4b0abb30091fa1a5d0751737f9ed036ac8704f"
26+
dependencies:
27+
"@angular-devkit/core" "0.5.4"
28+
"@ngtools/json-schema" "^1.1.0"
29+
rxjs "^6.0.0-beta.3"
30+
31+
"@angular-devkit/schematics@^0.5.5":
32+
version "0.5.5"
33+
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-0.5.5.tgz#c0b20980993237f2eeb4182e253c7c9efa299f3b"
34+
dependencies:
35+
"@angular-devkit/core" "0.5.5"
36+
"@ngtools/json-schema" "^1.1.0"
37+
rxjs "^6.0.0-beta.3"
38+
539
"@bazel/ibazel@^0.1.1":
640
version "0.1.1"
741
resolved "https://registry.yarnpkg.com/@bazel/ibazel/-/ibazel-0.1.1.tgz#f970c08b4e4efb0ab17e04ade3cc610554f33bed"
842

43+
"@ngtools/json-schema@^1.1.0":
44+
version "1.1.0"
45+
resolved "https://registry.yarnpkg.com/@ngtools/json-schema/-/json-schema-1.1.0.tgz#c3a0c544d62392acc2813a42c8a0dc6f58f86922"
46+
47+
"@schematics/angular@^0.5.4":
48+
version "0.5.4"
49+
resolved "https://registry.yarnpkg.com/@schematics/angular/-/angular-0.5.4.tgz#1c87706703a985fd291d283a4db1a9fc0aa6238f"
50+
dependencies:
51+
"@angular-devkit/core" "0.5.4"
52+
"@angular-devkit/schematics" "0.5.4"
53+
typescript "~2.6.2"
54+
955
"@types/angularjs@1.5.14-alpha":
1056
version "1.5.14-alpha"
1157
resolved "https://registry.yarnpkg.com/@types/angularjs/-/angularjs-1.5.14-alpha.tgz#2add80c88e1d84ade07e042918843093b6ac9808"
@@ -177,6 +223,15 @@ ajv@^5.1.0:
177223
json-schema-traverse "^0.3.0"
178224
json-stable-stringify "^1.0.1"
179225

226+
ajv@~5.5.1:
227+
version "5.5.2"
228+
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.5.2.tgz#73b5eeca3fab653e3d3f9422b341ad42205dc965"
229+
dependencies:
230+
co "^4.6.0"
231+
fast-deep-equal "^1.0.0"
232+
fast-json-stable-stringify "^2.0.0"
233+
json-schema-traverse "^0.3.0"
234+
180235
align-text@^0.1.1, align-text@^0.1.3:
181236
version "0.1.4"
182237
resolved "https://registry.yarnpkg.com/align-text/-/align-text-0.1.4.tgz#0cd90a561093f35d0a99256c22b7069433fad117"
@@ -840,7 +895,7 @@ chalk@^1.0.0, chalk@^1.1.1, chalk@^1.1.3:
840895
strip-ansi "^3.0.0"
841896
supports-color "^2.0.0"
842897

843-
chokidar@1.7.0, chokidar@^1.0.0, chokidar@^1.4.1:
898+
chokidar@1.7.0, chokidar@^1.0.0, chokidar@^1.4.1, chokidar@^1.7.0:
844899
version "1.7.0"
845900
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468"
846901
dependencies:
@@ -2030,6 +2085,10 @@ fast-deep-equal@^1.0.0:
20302085
version "1.0.0"
20312086
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz#96256a3bc975595eb36d82e9929d060d893439ff"
20322087

2088+
fast-json-stable-stringify@^2.0.0:
2089+
version "2.0.0"
2090+
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"
2091+
20332092
faye-websocket@~0.10.0:
20342093
version "0.10.0"
20352094
resolved "https://registry.yarnpkg.com/faye-websocket/-/faye-websocket-0.10.0.tgz#4e492f8d04dfb6f89003507f6edbf2d501e7c6f4"
@@ -5083,6 +5142,12 @@ rollup@0.47.4:
50835142
version "0.47.4"
50845143
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.47.4.tgz#e3a55de83a78221d232ce29619a8d68189ae845e"
50855144

5145+
rxjs@^6.0.0-beta.3:
5146+
version "6.0.0-tactical-rc.1"
5147+
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.0.0-tactical-rc.1.tgz#1fe1f1204132d1c71c72f249a487f8e76a5ec1d5"
5148+
dependencies:
5149+
tslib "^1.9.0"
5150+
50865151
rxjs@^6.0.0-terrific-rc.3:
50875152
version "6.0.0-terrific-rc.3"
50885153
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.0.0-terrific-rc.3.tgz#3acee937c1789ee4addf3cc3f7cc843d7cc2887c"
@@ -5928,6 +5993,10 @@ typescript@2.7.x:
59285993
version "2.7.2"
59295994
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.7.2.tgz#2d615a1ef4aee4f574425cdff7026edf81919836"
59305995

5996+
typescript@~2.6.2:
5997+
version "2.6.2"
5998+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.6.2.tgz#3c5b6fd7f6de0914269027f03c0946758f7673a4"
5999+
59316000
uglify-es@^3.3.9:
59326001
version "3.3.9"
59336002
resolved "https://registry.yarnpkg.com/uglify-es/-/uglify-es-3.3.9.tgz#0c1c4f0700bed8dbc124cdb304d2592ca203e677"

0 commit comments

Comments
 (0)