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 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | const core_1 = require("@angular-devkit/core");
|
---|
30 | const schematics_1 = require("@angular-devkit/schematics");
|
---|
31 | const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
|
---|
32 | const ast_utils_1 = require("../utility/ast-utils");
|
---|
33 | const change_1 = require("../utility/change");
|
---|
34 | const find_module_1 = require("../utility/find-module");
|
---|
35 | const lint_fix_1 = require("../utility/lint-fix");
|
---|
36 | const parse_name_1 = require("../utility/parse-name");
|
---|
37 | const validation_1 = require("../utility/validation");
|
---|
38 | const workspace_1 = require("../utility/workspace");
|
---|
39 | const schema_1 = require("./schema");
|
---|
40 | function readIntoSourceFile(host, modulePath) {
|
---|
41 | const text = host.read(modulePath);
|
---|
42 | if (text === null) {
|
---|
43 | throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
|
---|
44 | }
|
---|
45 | const sourceText = text.toString('utf-8');
|
---|
46 | return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
|
---|
47 | }
|
---|
48 | function addDeclarationToNgModule(options) {
|
---|
49 | return (host) => {
|
---|
50 | if (options.skipImport || !options.module) {
|
---|
51 | return host;
|
---|
52 | }
|
---|
53 | options.type = options.type != null ? options.type : 'Component';
|
---|
54 | const modulePath = options.module;
|
---|
55 | const source = readIntoSourceFile(host, modulePath);
|
---|
56 | const componentPath = `/${options.path}/` +
|
---|
57 | (options.flat ? '' : core_1.strings.dasherize(options.name) + '/') +
|
---|
58 | core_1.strings.dasherize(options.name) +
|
---|
59 | (options.type ? '.' : '') +
|
---|
60 | core_1.strings.dasherize(options.type);
|
---|
61 | const relativePath = find_module_1.buildRelativePath(modulePath, componentPath);
|
---|
62 | const classifiedName = core_1.strings.classify(options.name) + core_1.strings.classify(options.type);
|
---|
63 | const declarationChanges = ast_utils_1.addDeclarationToModule(source, modulePath, classifiedName, relativePath);
|
---|
64 | const declarationRecorder = host.beginUpdate(modulePath);
|
---|
65 | for (const change of declarationChanges) {
|
---|
66 | if (change instanceof change_1.InsertChange) {
|
---|
67 | declarationRecorder.insertLeft(change.pos, change.toAdd);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | host.commitUpdate(declarationRecorder);
|
---|
71 | if (options.export) {
|
---|
72 | // Need to refresh the AST because we overwrote the file in the host.
|
---|
73 | const source = readIntoSourceFile(host, modulePath);
|
---|
74 | const exportRecorder = host.beginUpdate(modulePath);
|
---|
75 | const exportChanges = ast_utils_1.addExportToModule(source, modulePath, core_1.strings.classify(options.name) + core_1.strings.classify(options.type), relativePath);
|
---|
76 | for (const change of exportChanges) {
|
---|
77 | if (change instanceof change_1.InsertChange) {
|
---|
78 | exportRecorder.insertLeft(change.pos, change.toAdd);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | host.commitUpdate(exportRecorder);
|
---|
82 | }
|
---|
83 | return host;
|
---|
84 | };
|
---|
85 | }
|
---|
86 | function buildSelector(options, projectPrefix) {
|
---|
87 | let selector = core_1.strings.dasherize(options.name);
|
---|
88 | if (options.prefix) {
|
---|
89 | selector = `${options.prefix}-${selector}`;
|
---|
90 | }
|
---|
91 | else if (options.prefix === undefined && projectPrefix) {
|
---|
92 | selector = `${projectPrefix}-${selector}`;
|
---|
93 | }
|
---|
94 | return selector;
|
---|
95 | }
|
---|
96 | function default_1(options) {
|
---|
97 | return async (host) => {
|
---|
98 | const workspace = await workspace_1.getWorkspace(host);
|
---|
99 | const project = workspace.projects.get(options.project);
|
---|
100 | if (!project) {
|
---|
101 | throw new schematics_1.SchematicsException(`Project "${options.project}" does not exist.`);
|
---|
102 | }
|
---|
103 | if (options.path === undefined) {
|
---|
104 | options.path = workspace_1.buildDefaultPath(project);
|
---|
105 | }
|
---|
106 | options.module = find_module_1.findModuleFromOptions(host, options);
|
---|
107 | const parsedPath = parse_name_1.parseName(options.path, options.name);
|
---|
108 | options.name = parsedPath.name;
|
---|
109 | options.path = parsedPath.path;
|
---|
110 | options.selector =
|
---|
111 | options.selector || buildSelector(options, (project && project.prefix) || '');
|
---|
112 | validation_1.validateName(options.name);
|
---|
113 | validation_1.validateHtmlSelector(options.selector);
|
---|
114 | const skipStyleFile = options.inlineStyle || options.style === schema_1.Style.None;
|
---|
115 | const templateSource = schematics_1.apply(schematics_1.url('./files'), [
|
---|
116 | options.skipTests ? schematics_1.filter((path) => !path.endsWith('.spec.ts.template')) : schematics_1.noop(),
|
---|
117 | skipStyleFile ? schematics_1.filter((path) => !path.endsWith('.__style__.template')) : schematics_1.noop(),
|
---|
118 | options.inlineTemplate ? schematics_1.filter((path) => !path.endsWith('.html.template')) : schematics_1.noop(),
|
---|
119 | schematics_1.applyTemplates({
|
---|
120 | ...core_1.strings,
|
---|
121 | 'if-flat': (s) => (options.flat ? '' : s),
|
---|
122 | ...options,
|
---|
123 | }),
|
---|
124 | !options.type
|
---|
125 | ? schematics_1.forEach(((file) => {
|
---|
126 | return file.path.includes('..')
|
---|
127 | ? {
|
---|
128 | content: file.content,
|
---|
129 | path: file.path.replace('..', '.'),
|
---|
130 | }
|
---|
131 | : file;
|
---|
132 | }))
|
---|
133 | : schematics_1.noop(),
|
---|
134 | schematics_1.move(parsedPath.path),
|
---|
135 | ]);
|
---|
136 | return schematics_1.chain([
|
---|
137 | addDeclarationToNgModule(options),
|
---|
138 | schematics_1.mergeWith(templateSource),
|
---|
139 | options.lintFix ? lint_fix_1.applyLintFix(options.path) : schematics_1.noop(),
|
---|
140 | ]);
|
---|
141 | };
|
---|
142 | }
|
---|
143 | exports.default = default_1;
|
---|