UNPKG

4.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 workspace_1 = require("../utility/workspace");
38function addDeclarationToNgModule(options) {
39 return (host) => {
40 if (options.skipImport || !options.module) {
41 return host;
42 }
43 const modulePath = options.module;
44 const text = host.read(modulePath);
45 if (text === null) {
46 throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
47 }
48 const sourceText = text.toString('utf-8');
49 const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
50 const pipePath = `/${options.path}/` +
51 (options.flat ? '' : core_1.strings.dasherize(options.name) + '/') +
52 core_1.strings.dasherize(options.name) +
53 '.pipe';
54 const relativePath = find_module_1.buildRelativePath(modulePath, pipePath);
55 const changes = ast_utils_1.addDeclarationToModule(source, modulePath, core_1.strings.classify(`${options.name}Pipe`), relativePath);
56 const recorder = host.beginUpdate(modulePath);
57 for (const change of changes) {
58 if (change instanceof change_1.InsertChange) {
59 recorder.insertLeft(change.pos, change.toAdd);
60 }
61 }
62 host.commitUpdate(recorder);
63 if (options.export) {
64 const text = host.read(modulePath);
65 if (text === null) {
66 throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
67 }
68 const sourceText = text.toString('utf-8');
69 const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
70 const exportRecorder = host.beginUpdate(modulePath);
71 const exportChanges = ast_utils_1.addExportToModule(source, modulePath, core_1.strings.classify(`${options.name}Pipe`), relativePath);
72 for (const change of exportChanges) {
73 if (change instanceof change_1.InsertChange) {
74 exportRecorder.insertLeft(change.pos, change.toAdd);
75 }
76 }
77 host.commitUpdate(exportRecorder);
78 }
79 return host;
80 };
81}
82function default_1(options) {
83 return async (host) => {
84 if (options.path === undefined) {
85 options.path = await workspace_1.createDefaultPath(host, options.project);
86 }
87 options.module = find_module_1.findModuleFromOptions(host, options);
88 const parsedPath = parse_name_1.parseName(options.path, options.name);
89 options.name = parsedPath.name;
90 options.path = parsedPath.path;
91 const templateSource = schematics_1.apply(schematics_1.url('./files'), [
92 options.skipTests ? schematics_1.filter((path) => !path.endsWith('.spec.ts.template')) : schematics_1.noop(),
93 schematics_1.applyTemplates({
94 ...core_1.strings,
95 'if-flat': (s) => (options.flat ? '' : s),
96 ...options,
97 }),
98 schematics_1.move(parsedPath.path),
99 ]);
100 return schematics_1.chain([
101 addDeclarationToNgModule(options),
102 schematics_1.mergeWith(templateSource),
103 options.lintFix ? lint_fix_1.applyLintFix(options.path) : schematics_1.noop(),
104 ]);
105 };
106}
107exports.default = default_1;