1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const core_1 = require("@angular-devkit/core");
|
11 | const schematics_1 = require("@angular-devkit/schematics");
|
12 | const tasks_1 = require("@angular-devkit/schematics/tasks");
|
13 | function addSchematicToCollectionJson(collectionPath, schematicName, description) {
|
14 | return (tree) => {
|
15 | const collectionJsonContent = tree.read(collectionPath);
|
16 | if (!collectionJsonContent) {
|
17 | throw new Error('Invalid collection path: ' + collectionPath);
|
18 | }
|
19 | const collectionJson = JSON.parse(collectionJsonContent.toString());
|
20 | if (!(0, core_1.isJsonObject)(collectionJson.schematics)) {
|
21 | throw new Error('Invalid collection.json; schematics needs to be an object.');
|
22 | }
|
23 | collectionJson['schematics'][schematicName] = description;
|
24 | tree.overwrite(collectionPath, JSON.stringify(collectionJson, undefined, 2));
|
25 | };
|
26 | }
|
27 | function default_1(options) {
|
28 | const schematicsVersion = require('@angular-devkit/schematics/package.json').version;
|
29 | const coreVersion = require('@angular-devkit/core/package.json').version;
|
30 |
|
31 | return (tree, context) => {
|
32 | if (!options.name) {
|
33 | throw new schematics_1.SchematicsException('name option is required.');
|
34 | }
|
35 | let collectionPath;
|
36 | try {
|
37 | const packageJsonContent = tree.read('/package.json');
|
38 | if (packageJsonContent) {
|
39 | const packageJson = JSON.parse(packageJsonContent.toString());
|
40 | if (typeof packageJson.schematics === 'string') {
|
41 | const p = (0, core_1.normalize)(packageJson.schematics);
|
42 | if (tree.exists(p)) {
|
43 | collectionPath = p;
|
44 | }
|
45 | }
|
46 | }
|
47 | }
|
48 | catch { }
|
49 | let source = (0, schematics_1.apply)((0, schematics_1.url)('./schematic-files'), [
|
50 | (0, schematics_1.template)({
|
51 | ...options,
|
52 | coreVersion,
|
53 | schematicsVersion,
|
54 | dot: '.',
|
55 | camelize: core_1.strings.camelize,
|
56 | dasherize: core_1.strings.dasherize,
|
57 | }),
|
58 | ]);
|
59 |
|
60 | if (!collectionPath) {
|
61 | collectionPath = (0, core_1.normalize)('/' + options.name + '/src/collection.json');
|
62 | source = (0, schematics_1.apply)((0, schematics_1.url)('./project-files'), [
|
63 | (0, schematics_1.template)({
|
64 | ...options,
|
65 | coreVersion,
|
66 | schematicsVersion,
|
67 | dot: '.',
|
68 | camelize: core_1.strings.camelize,
|
69 | dasherize: core_1.strings.dasherize,
|
70 | }),
|
71 | (0, schematics_1.mergeWith)(source),
|
72 | (0, schematics_1.move)(options.name),
|
73 | ]);
|
74 | context.addTask(new tasks_1.NodePackageInstallTask(options.name));
|
75 | }
|
76 | return (0, schematics_1.chain)([
|
77 | (0, schematics_1.mergeWith)(source),
|
78 | addSchematicToCollectionJson(collectionPath, core_1.strings.dasherize(options.name), {
|
79 | description: 'A blank schematic.',
|
80 | factory: './' + core_1.strings.dasherize(options.name) + '/index#' + core_1.strings.camelize(options.name),
|
81 | }),
|
82 | ]);
|
83 | };
|
84 | }
|
85 | exports.default = default_1;
|