UNPKG

5.05 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 addDeclarationToNgModule(options) {
21 return (host) => {
22 if (options.skipImport || !options.module) {
23 return host;
24 }
25 const modulePath = options.module;
26 const text = host.read(modulePath);
27 if (text === null) {
28 throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
29 }
30 const sourceText = text.toString('utf-8');
31 const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
32 const directivePath = `/${options.path}/`
33 + (options.flat ? '' : core_1.strings.dasherize(options.name) + '/')
34 + core_1.strings.dasherize(options.name)
35 + '.directive';
36 const relativePath = find_module_1.buildRelativePath(modulePath, directivePath);
37 const classifiedName = core_1.strings.classify(`${options.name}Directive`);
38 const declarationChanges = ast_utils_1.addDeclarationToModule(source, modulePath, classifiedName, relativePath);
39 const declarationRecorder = host.beginUpdate(modulePath);
40 for (const change of declarationChanges) {
41 if (change instanceof change_1.InsertChange) {
42 declarationRecorder.insertLeft(change.pos, change.toAdd);
43 }
44 }
45 host.commitUpdate(declarationRecorder);
46 if (options.export) {
47 // Need to refresh the AST because we overwrote the file in the host.
48 const text = host.read(modulePath);
49 if (text === null) {
50 throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
51 }
52 const sourceText = text.toString('utf-8');
53 const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
54 const exportRecorder = host.beginUpdate(modulePath);
55 const exportChanges = ast_utils_1.addExportToModule(source, modulePath, core_1.strings.classify(`${options.name}Directive`), 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 return host;
64 };
65}
66function buildSelector(options, projectPrefix) {
67 let selector = options.name;
68 if (options.prefix) {
69 selector = `${options.prefix}-${selector}`;
70 }
71 else if (options.prefix === undefined && projectPrefix) {
72 selector = `${projectPrefix}-${selector}`;
73 }
74 return core_1.strings.camelize(selector);
75}
76function default_1(options) {
77 return async (host) => {
78 const workspace = await workspace_1.getWorkspace(host);
79 const project = workspace.projects.get(options.project);
80 if (!project) {
81 throw new schematics_1.SchematicsException(`Invalid project name (${options.project})`);
82 }
83 if (options.path === undefined) {
84 options.path = workspace_1.buildDefaultPath(project);
85 }
86 options.module = find_module_1.findModuleFromOptions(host, options);
87 const parsedPath = parse_name_1.parseName(options.path, options.name);
88 options.name = parsedPath.name;
89 options.path = parsedPath.path;
90 options.selector = options.selector || buildSelector(options, project.prefix || '');
91 validation_1.validateHtmlSelector(options.selector);
92 const templateSource = schematics_1.apply(schematics_1.url('./files'), [
93 options.skipTests ? schematics_1.filter(path => !path.endsWith('.spec.ts.template')) : schematics_1.noop(),
94 schematics_1.applyTemplates({
95 ...core_1.strings,
96 'if-flat': (s) => options.flat ? '' : s,
97 ...options,
98 }),
99 schematics_1.move(parsedPath.path),
100 ]);
101 return schematics_1.chain([
102 addDeclarationToNgModule(options),
103 schematics_1.mergeWith(templateSource),
104 options.lintFix ? lint_fix_1.applyLintFix(options.path) : schematics_1.noop(),
105 ]);
106 };
107}
108exports.default = default_1;