UNPKG

6.39 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * @license
5 * Copyright Google Inc. All Rights Reserved.
6 *
7 * Use of this source code is governed by an MIT-style license that can be
8 * found in the LICENSE file at https://angular.io/license
9 */
10const core_1 = require("@angular-devkit/core");
11const schematics_1 = require("@angular-devkit/schematics");
12const ts = require("../third_party/github.com/Microsoft/TypeScript/lib/typescript");
13const ast_utils_1 = require("../utility/ast-utils");
14const change_1 = require("../utility/change");
15const find_module_1 = require("../utility/find-module");
16const lint_fix_1 = require("../utility/lint-fix");
17const parse_name_1 = require("../utility/parse-name");
18const validation_1 = require("../utility/validation");
19const workspace_1 = require("../utility/workspace");
20function readIntoSourceFile(host, modulePath) {
21 const text = host.read(modulePath);
22 if (text === null) {
23 throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
24 }
25 const sourceText = text.toString('utf-8');
26 return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
27}
28function addDeclarationToNgModule(options) {
29 return (host) => {
30 if (options.skipImport || !options.module) {
31 return host;
32 }
33 options.type = options.type != null ? options.type : 'Component';
34 const modulePath = options.module;
35 const source = readIntoSourceFile(host, modulePath);
36 const componentPath = `/${options.path}/`
37 + (options.flat ? '' : core_1.strings.dasherize(options.name) + '/')
38 + core_1.strings.dasherize(options.name)
39 + (options.type ? '.' : '')
40 + core_1.strings.dasherize(options.type);
41 const relativePath = find_module_1.buildRelativePath(modulePath, componentPath);
42 const classifiedName = core_1.strings.classify(options.name) + core_1.strings.classify(options.type);
43 const declarationChanges = ast_utils_1.addDeclarationToModule(source, modulePath, classifiedName, relativePath);
44 const declarationRecorder = host.beginUpdate(modulePath);
45 for (const change of declarationChanges) {
46 if (change instanceof change_1.InsertChange) {
47 declarationRecorder.insertLeft(change.pos, change.toAdd);
48 }
49 }
50 host.commitUpdate(declarationRecorder);
51 if (options.export) {
52 // Need to refresh the AST because we overwrote the file in the host.
53 const source = readIntoSourceFile(host, modulePath);
54 const exportRecorder = host.beginUpdate(modulePath);
55 const exportChanges = ast_utils_1.addExportToModule(source, modulePath, core_1.strings.classify(options.name) + core_1.strings.classify(options.type), relativePath);
56 for (const change of exportChanges) {
57 if (change instanceof change_1.InsertChange) {
58 exportRecorder.insertLeft(change.pos, change.toAdd);
59 }
60 }
61 host.commitUpdate(exportRecorder);
62 }
63 if (options.entryComponent) {
64 // Need to refresh the AST because we overwrote the file in the host.
65 const source = readIntoSourceFile(host, modulePath);
66 const entryComponentRecorder = host.beginUpdate(modulePath);
67 const entryComponentChanges = ast_utils_1.addEntryComponentToModule(source, modulePath, core_1.strings.classify(options.name) + core_1.strings.classify(options.type), relativePath);
68 for (const change of entryComponentChanges) {
69 if (change instanceof change_1.InsertChange) {
70 entryComponentRecorder.insertLeft(change.pos, change.toAdd);
71 }
72 }
73 host.commitUpdate(entryComponentRecorder);
74 }
75 return host;
76 };
77}
78function buildSelector(options, projectPrefix) {
79 let selector = core_1.strings.dasherize(options.name);
80 if (options.prefix) {
81 selector = `${options.prefix}-${selector}`;
82 }
83 else if (options.prefix === undefined && projectPrefix) {
84 selector = `${projectPrefix}-${selector}`;
85 }
86 return selector;
87}
88function default_1(options) {
89 return async (host) => {
90 const workspace = await workspace_1.getWorkspace(host);
91 const project = workspace.projects.get(options.project);
92 if (options.path === undefined && project) {
93 options.path = workspace_1.buildDefaultPath(project);
94 }
95 options.module = find_module_1.findModuleFromOptions(host, options);
96 const parsedPath = parse_name_1.parseName(options.path, options.name);
97 options.name = parsedPath.name;
98 options.path = parsedPath.path;
99 options.selector = options.selector || buildSelector(options, project && project.prefix || '');
100 validation_1.validateName(options.name);
101 validation_1.validateHtmlSelector(options.selector);
102 const templateSource = schematics_1.apply(schematics_1.url('./files'), [
103 options.skipTests ? schematics_1.filter(path => !path.endsWith('.spec.ts.template')) : schematics_1.noop(),
104 options.inlineStyle ? schematics_1.filter(path => !path.endsWith('.__style__.template')) : schematics_1.noop(),
105 options.inlineTemplate ? schematics_1.filter(path => !path.endsWith('.html.template')) : schematics_1.noop(),
106 schematics_1.applyTemplates({
107 ...core_1.strings,
108 'if-flat': (s) => options.flat ? '' : s,
109 ...options,
110 }),
111 !options.type ? schematics_1.forEach((file => {
112 if (!!file.path.match(new RegExp('..'))) {
113 return {
114 content: file.content,
115 path: file.path.replace('..', '.'),
116 };
117 }
118 else {
119 return file;
120 }
121 })) : schematics_1.noop(),
122 schematics_1.move(parsedPath.path),
123 ]);
124 return schematics_1.chain([
125 addDeclarationToNgModule(options),
126 schematics_1.mergeWith(templateSource),
127 options.lintFix ? lint_fix_1.applyLintFix(options.path) : schematics_1.noop(),
128 ]);
129 };
130}
131exports.default = default_1;