-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
173 lines (173 loc) · 7.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"use strict";
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const schematics_1 = require("@angular-devkit/schematics");
const posix_1 = require("node:path/posix");
const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
const ast_utils_1 = require("../utility/ast-utils");
const change_1 = require("../utility/change");
const find_module_1 = require("../utility/find-module");
const parse_name_1 = require("../utility/parse-name");
const validation_1 = require("../utility/validation");
const workspace_1 = require("../utility/workspace");
const schema_1 = require("./schema");
function buildRelativeModulePath(options, modulePath) {
const importModulePath = (0, posix_1.join)(options.path ?? '', options.flat ? '' : schematics_1.strings.dasherize(options.name), schematics_1.strings.dasherize(options.name) + options.typeSeparator + 'module');
return (0, find_module_1.buildRelativePath)(modulePath, importModulePath);
}
function addImportToNgModule(options) {
return (host) => {
if (!options.module) {
return host;
}
const modulePath = options.module;
const sourceText = host.readText(modulePath);
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
const relativePath = buildRelativeModulePath(options, modulePath);
const changes = (0, ast_utils_1.addImportToModule)(source, modulePath, schematics_1.strings.classify(`${options.name}Module`), relativePath);
const recorder = host.beginUpdate(modulePath);
for (const change of changes) {
if (change instanceof change_1.InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(recorder);
return host;
};
}
function addRouteDeclarationToNgModule(options, routingModulePath) {
return (host) => {
if (!options.route) {
return host;
}
if (!options.module) {
throw new Error('Module option required when creating a lazy loaded routing module.');
}
let path;
if (routingModulePath) {
path = routingModulePath;
}
else {
path = options.module;
}
const sourceText = host.readText(path);
const addDeclaration = (0, ast_utils_1.addRouteDeclarationToModule)(ts.createSourceFile(path, sourceText, ts.ScriptTarget.Latest, true), path, buildRoute(options, options.module));
const recorder = host.beginUpdate(path);
recorder.insertLeft(addDeclaration.pos, addDeclaration.toAdd);
host.commitUpdate(recorder);
return host;
};
}
function getRoutingModulePath(host, modulePath) {
const routingModulePath = modulePath.endsWith(find_module_1.ROUTING_MODULE_EXT) || modulePath.endsWith('-routing-module.ts')
? modulePath
: modulePath
.replace(find_module_1.MODULE_EXT, find_module_1.ROUTING_MODULE_EXT)
.replace('-module.ts', '-routing-module.ts');
return host.exists(routingModulePath) ? routingModulePath : undefined;
}
function buildRoute(options, modulePath) {
const relativeModulePath = buildRelativeModulePath(options, modulePath);
const moduleName = `${schematics_1.strings.classify(options.name)}Module`;
const loadChildren = `() => import('${relativeModulePath}').then(m => m.${moduleName})`;
return `{ path: '${options.route}', loadChildren: ${loadChildren} }`;
}
function default_1(options) {
return async (host) => {
if (options.path === undefined) {
options.path = await (0, workspace_1.createDefaultPath)(host, options.project);
}
if (options.module) {
try {
options.module = (0, find_module_1.findModuleFromOptions)(host, options);
}
catch {
options.module = (0, find_module_1.findModuleFromOptions)(host, {
...options,
moduleExt: '-module.ts',
routingModuleExt: '-routing-module.ts',
});
}
}
let routingModulePath;
const isLazyLoadedModuleGen = !!(options.route && options.module);
if (isLazyLoadedModuleGen) {
options.routingScope = schema_1.RoutingScope.Child;
routingModulePath = getRoutingModulePath(host, options.module);
}
const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
(0, validation_1.validateClassName)(schematics_1.strings.classify(options.name));
const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
options.routing || (isLazyLoadedModuleGen && routingModulePath)
? (0, schematics_1.noop)()
: (0, schematics_1.filter)((path) => !path.includes('-routing')),
(0, schematics_1.applyTemplates)({
...schematics_1.strings,
'if-flat': (s) => (options.flat ? '' : s),
lazyRoute: isLazyLoadedModuleGen,
lazyRouteWithoutRouteModule: isLazyLoadedModuleGen && !routingModulePath,
lazyRouteWithRouteModule: isLazyLoadedModuleGen && !!routingModulePath,
...options,
}),
(0, schematics_1.move)(parsedPath.path),
]);
const moduleDasherized = schematics_1.strings.dasherize(options.name);
const modulePath = `${!options.flat ? moduleDasherized + '/' : ''}${moduleDasherized}${options.typeSeparator}module.ts`;
const componentOptions = {
module: modulePath,
flat: options.flat,
name: options.name,
path: options.path,
project: options.project,
standalone: false,
};
return (0, schematics_1.chain)([
!isLazyLoadedModuleGen ? addImportToNgModule(options) : (0, schematics_1.noop)(),
addRouteDeclarationToNgModule(options, routingModulePath),
(0, schematics_1.mergeWith)(templateSource),
isLazyLoadedModuleGen ? (0, schematics_1.schematic)('component', componentOptions) : (0, schematics_1.noop)(),
]);
};
}