1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const core_1 = require("@angular-devkit/core");
|
11 | const schematics_1 = require("@angular-devkit/schematics");
|
12 | const ts = require("../third_party/github.com/Microsoft/TypeScript/lib/typescript");
|
13 | const ast_utils_1 = require("../utility/ast-utils");
|
14 | const change_1 = require("../utility/change");
|
15 | const find_module_1 = require("../utility/find-module");
|
16 | const lint_fix_1 = require("../utility/lint-fix");
|
17 | const parse_name_1 = require("../utility/parse-name");
|
18 | const workspace_1 = require("../utility/workspace");
|
19 | function addDeclarationToNgModule(options) {
|
20 | return (host) => {
|
21 | if (options.skipImport || !options.module) {
|
22 | return host;
|
23 | }
|
24 | const modulePath = options.module;
|
25 | const text = host.read(modulePath);
|
26 | if (text === null) {
|
27 | throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
|
28 | }
|
29 | const sourceText = text.toString('utf-8');
|
30 | const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
|
31 | const pipePath = `/${options.path}/`
|
32 | + (options.flat ? '' : core_1.strings.dasherize(options.name) + '/')
|
33 | + core_1.strings.dasherize(options.name)
|
34 | + '.pipe';
|
35 | const relativePath = find_module_1.buildRelativePath(modulePath, pipePath);
|
36 | const changes = ast_utils_1.addDeclarationToModule(source, modulePath, core_1.strings.classify(`${options.name}Pipe`), relativePath);
|
37 | const recorder = host.beginUpdate(modulePath);
|
38 | for (const change of changes) {
|
39 | if (change instanceof change_1.InsertChange) {
|
40 | recorder.insertLeft(change.pos, change.toAdd);
|
41 | }
|
42 | }
|
43 | host.commitUpdate(recorder);
|
44 | if (options.export) {
|
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 exportRecorder = host.beginUpdate(modulePath);
|
52 | const exportChanges = ast_utils_1.addExportToModule(source, modulePath, core_1.strings.classify(`${options.name}Pipe`), relativePath);
|
53 | for (const change of exportChanges) {
|
54 | if (change instanceof change_1.InsertChange) {
|
55 | exportRecorder.insertLeft(change.pos, change.toAdd);
|
56 | }
|
57 | }
|
58 | host.commitUpdate(exportRecorder);
|
59 | }
|
60 | return host;
|
61 | };
|
62 | }
|
63 | function default_1(options) {
|
64 | return async (host) => {
|
65 | if (options.path === undefined) {
|
66 | options.path = await workspace_1.createDefaultPath(host, options.project);
|
67 | }
|
68 | options.module = find_module_1.findModuleFromOptions(host, options);
|
69 | const parsedPath = parse_name_1.parseName(options.path, options.name);
|
70 | options.name = parsedPath.name;
|
71 | options.path = parsedPath.path;
|
72 | const templateSource = schematics_1.apply(schematics_1.url('./files'), [
|
73 | options.skipTests ? schematics_1.filter(path => !path.endsWith('.spec.ts.template')) : schematics_1.noop(),
|
74 | schematics_1.applyTemplates({
|
75 | ...core_1.strings,
|
76 | 'if-flat': (s) => options.flat ? '' : s,
|
77 | ...options,
|
78 | }),
|
79 | schematics_1.move(parsedPath.path),
|
80 | ]);
|
81 | return schematics_1.chain([
|
82 | addDeclarationToNgModule(options),
|
83 | schematics_1.mergeWith(templateSource),
|
84 | options.lintFix ? lint_fix_1.applyLintFix(options.path) : schematics_1.noop(),
|
85 | ]);
|
86 | };
|
87 | }
|
88 | exports.default = default_1;
|