UNPKG

3.72 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10const core_1 = require("@angular-devkit/core");
11const schematics_1 = require("@angular-devkit/schematics");
12const tasks_1 = require("@angular-devkit/schematics/tasks");
13function 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}
27function default_1(options) {
28 const schematicsVersion = require('@angular-devkit/schematics/package.json').version;
29 const coreVersion = require('@angular-devkit/core/package.json').version;
30 // Verify if we need to create a full project, or just add a new schematic.
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 // Simply create a new schematic project.
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}
85exports.default = default_1;