UNPKG

2.63 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 schematics_1 = require("@angular-devkit/schematics");
11const add_declaration_to_ng_module_1 = require("../utility/add-declaration-to-ng-module");
12const find_module_1 = require("../utility/find-module");
13const parse_name_1 = require("../utility/parse-name");
14const validation_1 = require("../utility/validation");
15const workspace_1 = require("../utility/workspace");
16function buildSelector(options, projectPrefix) {
17 let selector = options.name;
18 if (options.prefix) {
19 selector = `${options.prefix}-${selector}`;
20 }
21 else if (options.prefix === undefined && projectPrefix) {
22 selector = `${projectPrefix}-${selector}`;
23 }
24 return schematics_1.strings.camelize(selector);
25}
26function default_1(options) {
27 return async (host) => {
28 const workspace = await (0, workspace_1.getWorkspace)(host);
29 const project = workspace.projects.get(options.project);
30 if (!project) {
31 throw new schematics_1.SchematicsException(`Project "${options.project}" does not exist.`);
32 }
33 if (options.path === undefined) {
34 options.path = (0, workspace_1.buildDefaultPath)(project);
35 }
36 options.module = (0, find_module_1.findModuleFromOptions)(host, options);
37 const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
38 options.name = parsedPath.name;
39 options.path = parsedPath.path;
40 options.selector = options.selector || buildSelector(options, project.prefix || '');
41 (0, validation_1.validateHtmlSelector)(options.selector);
42 const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
43 options.skipTests ? (0, schematics_1.filter)((path) => !path.endsWith('.spec.ts.template')) : (0, schematics_1.noop)(),
44 (0, schematics_1.applyTemplates)({
45 ...schematics_1.strings,
46 'if-flat': (s) => (options.flat ? '' : s),
47 ...options,
48 }),
49 (0, schematics_1.move)(parsedPath.path),
50 ]);
51 return (0, schematics_1.chain)([
52 (0, add_declaration_to_ng_module_1.addDeclarationToNgModule)({
53 type: 'directive',
54 ...options,
55 }),
56 (0, schematics_1.mergeWith)(templateSource),
57 ]);
58 };
59}
60exports.default = default_1;