UNPKG

5.94 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 */
9var __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}));
16var __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});
21var __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};
28Object.defineProperty(exports, "__esModule", { value: true });
29const core_1 = require("@angular-devkit/core");
30const schematics_1 = require("@angular-devkit/schematics");
31const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
32const ast_utils_1 = require("../utility/ast-utils");
33const change_1 = require("../utility/change");
34const find_module_1 = require("../utility/find-module");
35const lint_fix_1 = require("../utility/lint-fix");
36const parse_name_1 = require("../utility/parse-name");
37const validation_1 = require("../utility/validation");
38const workspace_1 = require("../utility/workspace");
39function addDeclarationToNgModule(options) {
40 return (host) => {
41 if (options.skipImport || !options.module) {
42 return host;
43 }
44 const modulePath = options.module;
45 const text = host.read(modulePath);
46 if (text === null) {
47 throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
48 }
49 const sourceText = text.toString('utf-8');
50 const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
51 const directivePath = `/${options.path}/` +
52 (options.flat ? '' : core_1.strings.dasherize(options.name) + '/') +
53 core_1.strings.dasherize(options.name) +
54 '.directive';
55 const relativePath = find_module_1.buildRelativePath(modulePath, directivePath);
56 const classifiedName = core_1.strings.classify(`${options.name}Directive`);
57 const declarationChanges = ast_utils_1.addDeclarationToModule(source, modulePath, classifiedName, relativePath);
58 const declarationRecorder = host.beginUpdate(modulePath);
59 for (const change of declarationChanges) {
60 if (change instanceof change_1.InsertChange) {
61 declarationRecorder.insertLeft(change.pos, change.toAdd);
62 }
63 }
64 host.commitUpdate(declarationRecorder);
65 if (options.export) {
66 // Need to refresh the AST because we overwrote the file in the host.
67 const text = host.read(modulePath);
68 if (text === null) {
69 throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
70 }
71 const sourceText = text.toString('utf-8');
72 const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
73 const exportRecorder = host.beginUpdate(modulePath);
74 const exportChanges = ast_utils_1.addExportToModule(source, modulePath, core_1.strings.classify(`${options.name}Directive`), relativePath);
75 for (const change of exportChanges) {
76 if (change instanceof change_1.InsertChange) {
77 exportRecorder.insertLeft(change.pos, change.toAdd);
78 }
79 }
80 host.commitUpdate(exportRecorder);
81 }
82 return host;
83 };
84}
85function buildSelector(options, projectPrefix) {
86 let selector = options.name;
87 if (options.prefix) {
88 selector = `${options.prefix}-${selector}`;
89 }
90 else if (options.prefix === undefined && projectPrefix) {
91 selector = `${projectPrefix}-${selector}`;
92 }
93 return core_1.strings.camelize(selector);
94}
95function default_1(options) {
96 return async (host) => {
97 const workspace = await workspace_1.getWorkspace(host);
98 const project = workspace.projects.get(options.project);
99 if (!project) {
100 throw new schematics_1.SchematicsException(`Project "${options.project}" does not exist.`);
101 }
102 if (options.path === undefined) {
103 options.path = workspace_1.buildDefaultPath(project);
104 }
105 options.module = find_module_1.findModuleFromOptions(host, options);
106 const parsedPath = parse_name_1.parseName(options.path, options.name);
107 options.name = parsedPath.name;
108 options.path = parsedPath.path;
109 options.selector = options.selector || buildSelector(options, project.prefix || '');
110 validation_1.validateHtmlSelector(options.selector);
111 const templateSource = schematics_1.apply(schematics_1.url('./files'), [
112 options.skipTests ? schematics_1.filter((path) => !path.endsWith('.spec.ts.template')) : schematics_1.noop(),
113 schematics_1.applyTemplates({
114 ...core_1.strings,
115 'if-flat': (s) => (options.flat ? '' : s),
116 ...options,
117 }),
118 schematics_1.move(parsedPath.path),
119 ]);
120 return schematics_1.chain([
121 addDeclarationToNgModule(options),
122 schematics_1.mergeWith(templateSource),
123 options.lintFix ? lint_fix_1.applyLintFix(options.path) : schematics_1.noop(),
124 ]);
125 };
126}
127exports.default = default_1;