|
1 |
| -'use strict'; |
2 |
| - |
3 |
| -const fs = require('fs'); |
4 |
| -const path = require('path'); |
5 |
| -const dynamicPathParser = require('../../utilities/dynamic-path-parser'); |
6 |
| -const stringUtils = require('ember-cli-string-utils'); |
7 |
| -const Blueprint = require('ember-cli/lib/models/blueprint'); |
8 |
| - |
9 |
| - |
10 |
| -function _regexEscape(str) { |
11 |
| - return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); |
12 |
| -} |
13 |
| - |
14 |
| - |
15 |
| -function _insertImport(content, symbolName, fileName) { |
16 |
| - // Check if an import from the same file is there. |
17 |
| - const importRegex = new RegExp('' + |
18 |
| - /^(import\s+\{)/.source + // 1. prefix |
19 |
| - /(.*?)/.source + // 2. current imports |
20 |
| - `(\\} from '${_regexEscape(fileName)}';)` + // 3. suffix |
21 |
| - '\\n', 'm' |
22 |
| - ); |
23 |
| - |
24 |
| - const m = content.match(importRegex); |
25 |
| - if (m) { |
26 |
| - // console.log(m[2], symbolName, m[2].); |
27 |
| - if (m[2].match(new RegExp(`\\b${_regexEscape(symbolName)}\\b`))) { |
28 |
| - // Already in the symbol list. |
29 |
| - return content; |
30 |
| - } |
31 |
| - |
32 |
| - return content.substr(0, m.index) + m[1] + m[2] + ', ' + symbolName + m[3] + '\n' |
33 |
| - + content.substr(m.index + m[0].length); |
34 |
| - } |
35 |
| - |
36 |
| - const importTemplate = `import { ${symbolName} } from '${fileName}';`; |
37 |
| - // Find the last import and add an import to it. |
38 |
| - content = content.replace(/(import.+)\n(?!import)/m, function (f, m1) { |
39 |
| - return `${m1}\n${importTemplate}\n`; |
40 |
| - }); |
41 |
| - |
42 |
| - return content; |
43 |
| -} |
44 |
| - |
45 |
| -function _removeImport(content, symbolName, fileName) { |
46 |
| - const importRegex = new RegExp('' + |
47 |
| - /^(import\s+\{)/.source + // prefix |
48 |
| - /(.*?),?/.source + // symbolsPre |
49 |
| - '\\b' + _regexEscape(symbolName) + '\\b' + // symbol |
50 |
| - ',?' + '(.*?)' + // symbolsPost |
51 |
| - `(} from '${_regexEscape(fileName)}'\\s*;)` + // suffix |
52 |
| - '\\n', 'm' |
53 |
| - ); |
54 |
| - |
55 |
| - return content.replace(importRegex, function(_, prefix, symbolsPre, symbol, symbolsPost, suffix) { |
56 |
| - if (symbolsPre == '' && symbolsPost == '') { |
57 |
| - // Nothing before or after, remove the line. |
58 |
| - return ''; |
59 |
| - } |
60 |
| - if (symbolsPre == '') { |
61 |
| - // Nothing before. |
62 |
| - return prefix + symbolsPost + suffix; |
63 |
| - } |
64 |
| - if (symbolsPost == '') { |
65 |
| - // Nothing after. |
66 |
| - return prefix + symbolsPre + suffix; |
67 |
| - } |
68 |
| - // Something before and after, add a `,`. |
69 |
| - return prefix + symbolsPre + ',' + symbolsPost + suffix; |
70 |
| - }); |
71 |
| -} |
72 |
| - |
73 |
| -function _addRoutes(content) { |
74 |
| - // If an annotation is already there, just ignore this. |
75 |
| - if (content.indexOf('@Routes') !== -1) { |
76 |
| - return content; |
77 |
| - } |
78 |
| - |
79 |
| - // Add the imports. |
80 |
| - content = _insertImport(content, 'Routes', '@angular/router'); |
81 |
| - content = _insertImport(content, 'ROUTER_DIRECTIVES', '@angular/router'); |
82 |
| - |
83 |
| - // Add the router config. |
84 |
| - const m = content.match(/(@Component\(\{[\s\S\n]*?}\)\n)(\s*export class)/m); |
85 |
| - if (!m) { |
86 |
| - // No component. |
87 |
| - // eslint-disable-next-line no-console |
88 |
| - console.warn('No component annotation was found...'); |
89 |
| - return content; |
90 |
| - } |
91 |
| - |
92 |
| - content = content.substr(0, m.index) + m[1] + '@Routes([\n])\n' |
93 |
| - + m[2] + content.substr(m.index + m[0].length); |
94 |
| - |
95 |
| - return content; |
96 |
| -} |
97 |
| - |
98 |
| - |
99 | 1 | module.exports = {
|
100 |
| - description: 'Generates a route and a template.', |
101 |
| - |
102 |
| - availableOptions: [ |
103 |
| - { name: 'skip-router-generation', type: Boolean, default: false, aliases: ['srg'] }, |
104 |
| - { name: 'default', type: Boolean, default: false }, |
105 |
| - { name: 'lazy', type: Boolean, default: true }, |
106 |
| - { name: 'inline-template', type: Boolean, default: false, aliases: ['it'] }, |
107 |
| - { name: 'inline-style', type: Boolean, default: false, aliases: ['is'] }, |
108 |
| - { name: 'prefix', type: Boolean, default: true }, |
109 |
| - { name: 'path', type: String } |
110 |
| - ], |
111 |
| - |
112 |
| - beforeInstall: function(options) { |
113 |
| - options.route = true; |
114 |
| - if (options.lazy) { |
115 |
| - options.isLazyRoute = true; |
116 |
| - } |
117 |
| - return Blueprint.load(path.join(__dirname, '..', 'component')).install(options); |
118 |
| - }, |
119 |
| - |
120 |
| - afterInstall: function (options) { |
121 |
| - if (options.dryRun || options.skipRouterGeneration) { |
122 |
| - return; |
123 |
| - } |
124 |
| - |
125 |
| - this._addRouteToParent(options); |
126 |
| - this._verifyParentRoute(options); |
127 |
| - }, |
128 |
| - |
129 |
| - afterUninstall: function (options) { |
130 |
| - this._removeRouteFromParent(options); |
131 |
| - }, |
132 |
| - |
133 |
| - normalizeEntityName: function (entityName) { |
134 |
| - var parsedPath = dynamicPathParser(this.project, entityName); |
135 |
| - |
136 |
| - // If a specified route starts with `+` remove it as it'd break convention. |
137 |
| - if (parsedPath.name[0] === '+') { |
138 |
| - var index = entityName.lastIndexOf(parsedPath.name); |
139 |
| - entityName = entityName.substr(0, index) + entityName.substr(index + 1); |
140 |
| - } |
141 |
| - |
142 |
| - this.dynamicPath = parsedPath; |
143 |
| - |
144 |
| - //leave the entity name intact for component generation |
145 |
| - return entityName; |
146 |
| - }, |
147 |
| - |
148 |
| - locals: function () { |
149 |
| - return { |
150 |
| - dynamicPath: this.dynamicPath.dir.replace(this.dynamicPath.appRoot, '') |
151 |
| - }; |
152 |
| - }, |
153 |
| - |
154 |
| - fileMapTokens: function (options) { |
155 |
| - // Return custom template variables here. |
156 |
| - return { |
157 |
| - __path__: () => { |
158 |
| - var dir = this.dynamicPath.dir; |
159 |
| - if (!options.locals.flat) { |
160 |
| - dir += path.sep + options.dasherizedModuleName; |
161 |
| - } |
162 |
| - return dir; |
163 |
| - } |
164 |
| - }; |
165 |
| - }, |
166 |
| - |
167 |
| - _findParentRouteFile: function(dir) { |
168 |
| - const parentDir = path.isAbsolute(dir) ? dir : path.join(this.project.root, dir); |
169 |
| - // Remove the `+` if it's the first character. |
170 |
| - const routeName = path.basename(dir).substr(path.basename(dir)[0] == '+' ? 1 : 0); |
171 |
| - let parentFile = path.join(parentDir, `${routeName}.component.ts`); |
172 |
| - |
173 |
| - if (parentDir == path.join(this.project.root, this.dynamicPath.appRoot)) { |
174 |
| - parentFile = path.join(parentDir, this.project.name() + '.component.ts'); |
175 |
| - } |
176 |
| - |
177 |
| - if (fs.existsSync(parentFile)) { |
178 |
| - return parentFile; |
179 |
| - } |
180 |
| - |
181 |
| - // Try without the .component. Old routes won't have it, or users might rename it. |
182 |
| - parentFile = path.join(parentDir, `${path.basename(dir)}.ts`); |
183 |
| - if (fs.existsSync(parentFile)) { |
184 |
| - return parentFile; |
185 |
| - } |
186 |
| - |
187 |
| - return null; |
188 |
| - }, |
189 |
| - |
190 |
| - _removeRouteFromParent: function(options) { |
191 |
| - const parsedPath = this.dynamicPath; |
192 |
| - const parentFile = this._findParentRouteFile(this.dynamicPath.dir); |
193 |
| - if (!parentFile) { |
194 |
| - return; |
195 |
| - } |
196 |
| - |
197 |
| - const jsComponentName = stringUtils.classify(options.entity.name); |
198 |
| - const base = parsedPath.base; |
199 |
| - |
200 |
| - let content = fs.readFileSync(parentFile, 'utf-8'); |
201 |
| - content = _removeImport(content, `${jsComponentName}Component`, |
202 |
| - `./${options.isLazyRoute ? '+' : ''}${base}`); |
203 |
| - |
204 |
| - const route = new RegExp(`^\\s*\\{.*name: '${jsComponentName}'.*component: ${jsComponentName}.*` |
205 |
| - + '\\},?\\s*\\n?', 'm'); |
206 |
| - content = content.replace(route, ''); |
207 |
| - |
208 |
| - fs.writeFileSync(parentFile, content, 'utf-8'); |
209 |
| - }, |
210 |
| - |
211 |
| - _addRouteToParent: function(options) { |
212 |
| - const parsedPath = this.dynamicPath; |
213 |
| - const parentFile = this._findParentRouteFile(this.dynamicPath.dir); |
214 |
| - if (!parentFile) { |
215 |
| - return; |
216 |
| - } |
217 |
| - |
218 |
| - let isAppComponent = false; |
219 |
| - let appComponentFile = |
220 |
| - path.join(this.project.root, |
221 |
| - this.dynamicPath.dir, |
222 |
| - this.project.name() + '.component.ts'); |
223 |
| - if (parentFile == appComponentFile) { |
224 |
| - isAppComponent = true; |
225 |
| - } |
226 |
| - |
227 |
| - const jsComponentName = stringUtils.classify(options.entity.name); |
228 |
| - const base = parsedPath.base; |
229 |
| - |
230 |
| - // Insert the import statement. |
231 |
| - let content = fs.readFileSync(parentFile, 'utf-8'); |
232 |
| - let lazyRoutePrefix = '+'; |
233 |
| - if (this.project.ngConfig && |
234 |
| - this.project.ngConfig.defaults && |
235 |
| - this.project.ngConfig.defaults.lazyRoutePrefix !== undefined) { |
236 |
| - lazyRoutePrefix = this.project.ngConfig.defaults.lazyRoutePrefix; |
237 |
| - } |
238 |
| - content = _insertImport(content, `${jsComponentName}Component`, |
239 |
| - `./${options.isLazyRoute ? lazyRoutePrefix : ''}${stringUtils.dasherize(base)}`); |
240 |
| - |
241 |
| - let defaultReg = options.default ? ', useAsDefault: true' : ''; |
242 |
| - let routePath = options.path || `/${base}`; |
243 |
| - let route = '{' |
244 |
| - + `path: '${routePath}', ` |
245 |
| - + `component: ${jsComponentName}Component` |
246 |
| - + defaultReg |
247 |
| - + '}'; |
248 |
| - |
249 |
| - // Add the route configuration. |
250 |
| - content = _addRoutes(content); |
251 |
| - content = content.replace(/(@Routes\(\[\s*\n)([\s\S\n]*?)(^\s*\]\))/m, function(_, m1, m2, m3) { |
252 |
| - if (m2.length) { |
253 |
| - // Add a `,` if there's none. |
254 |
| - m2 = m2.replace(/([^,])(\s*)\n$/, function (_, a1, a2) { |
255 |
| - return a1 + ',\n' + a2; |
256 |
| - }); |
257 |
| - } |
258 |
| - return m1 + m2 + ` ${route}\n` + m3; |
259 |
| - }); |
260 |
| - |
261 |
| - // Add the directive. |
262 |
| - content = content.replace(/(@Component\(\{)([\s\S\n]*?)(\n\}\))/m, function(_, prefix, json, suffix) { |
263 |
| - const m = json.match(/(^\s+directives:\s*\[)([\s\S\n]*)(\]\s*,?.*$)/m); |
264 |
| - if (m) { |
265 |
| - if (m[2].indexOf('ROUTER_DIRECTIVES') != -1) { |
266 |
| - // Already there. |
267 |
| - return _; |
268 |
| - } |
269 |
| - |
270 |
| - // There's a directive already, but no ROUTER_DIRECTIVES. |
271 |
| - return prefix + |
272 |
| - json.replace(/(^\s+directives:\s*\[)([\s\S\n]*)(^\]\s*,?.*$)/m, function(_, prefix, d, suffix) { |
273 |
| - return prefix + d + (d ? ',' : '') + 'ROUTER_DIRECTIVES' + suffix; |
274 |
| - }) + suffix; |
275 |
| - } else { |
276 |
| - // There's no directive already. |
277 |
| - return prefix + json + ',\n directives: [ROUTER_DIRECTIVES]' + suffix; |
278 |
| - } |
279 |
| - }); |
280 |
| - |
281 |
| - // Add the provider, only on the APP itself. |
282 |
| - if (isAppComponent) { |
283 |
| - content = _insertImport(content, 'ROUTER_DIRECTIVES', '@angular/router'); |
284 |
| - content = _insertImport(content, 'ROUTER_PROVIDERS', '@angular/router'); |
285 |
| - content = content.replace(/(@Component\(\{)([\s\S\n]*?)(\n\}\))/m, function (_, prefix, json, suffix) { |
286 |
| - const m = json.match(/(^\s+providers:\s*\[)([\s\S\n]*)(\]\s*,?.*$)/m); |
287 |
| - if (m) { |
288 |
| - if (m[2].indexOf('ROUTER_PROVIDERS') != -1) { |
289 |
| - // Already there. |
290 |
| - return _; |
291 |
| - } |
292 |
| - |
293 |
| - // There's a directive already, but no ROUTER_PROVIDERS. |
294 |
| - return prefix + |
295 |
| - json.replace(/(^\s+providers:\s*\[)([\s\S\n]*)(^\]\s*,?.*$)/m, function (_, prefix, d, suffix) { |
296 |
| - return prefix + d + (d ? ',' : '') + 'ROUTER_PROVIDERS' + suffix; |
297 |
| - }) + suffix; |
298 |
| - } else { |
299 |
| - // There's no directive already. |
300 |
| - return prefix + json + ',\n providers: [ROUTER_PROVIDERS]' + suffix; |
301 |
| - } |
302 |
| - }); |
303 |
| - } |
304 |
| - |
305 |
| - // Change the template. |
306 |
| - content = content.replace(/(@Component\(\{)([\s\S\n]*?)(\}\))/m, function(_, prefix, json, suffix) { |
307 |
| - const m = json.match(/(^\s+template:\s*\[)([\s\S\n]*)(\]\s*,?.*$)/m); |
308 |
| - |
309 |
| - if (m) { |
310 |
| - if (m[2].indexOf('<router-outlet></router-outlet>') != -1) { |
311 |
| - // Already there. |
312 |
| - return _; |
313 |
| - } |
314 |
| - |
315 |
| - // There's a template already, but no <router-outlet>. |
316 |
| - return prefix + |
317 |
| - json.replace(/(^\s+template:\s*`)([\s\S\n]*?)(`,?.*$)/m, function(_, prefix, t, suffix) { |
318 |
| - return prefix + t + '<router-outlet></router-outlet>' + suffix; |
319 |
| - }) + suffix; |
320 |
| - } else { |
321 |
| - // There's no template, look for the HTML file. |
322 |
| - const htmlFile = parentFile.replace(/\.ts$/, '.html'); |
323 |
| - if (!fs.existsSync(htmlFile)) { |
324 |
| - // eslint-disable-next-line no-console |
325 |
| - console.log('Cannot find HTML: ' + htmlFile); |
326 |
| - |
327 |
| - // Can't be found, exit early. |
328 |
| - return _; |
329 |
| - } |
330 |
| - |
331 |
| - let html = fs.readFileSync(htmlFile, 'utf-8'); |
332 |
| - if (html.indexOf('<router-outlet></router-outlet>') == -1) { |
333 |
| - html += '\n<router-outlet></router-outlet>'; |
334 |
| - |
335 |
| - fs.writeFileSync(htmlFile, html, 'utf-8'); |
336 |
| - } |
337 |
| - return _; |
338 |
| - } |
339 |
| - }); |
340 |
| - |
341 |
| - fs.writeFileSync(parentFile, content, 'utf-8'); |
342 |
| - }, |
343 |
| - |
344 |
| - _verifyParentRoute: function() { |
345 |
| - const parsedPath = this.dynamicPath; |
346 |
| - const parentFile = this._findParentRouteFile(parsedPath.dir); |
347 |
| - if (!parentFile) { |
348 |
| - return; |
349 |
| - } |
350 |
| - |
351 |
| - const gParentDir = path.dirname(path.dirname(parentFile)); |
352 |
| - const gParentFile = this._findParentRouteFile(gParentDir); |
353 |
| - |
354 |
| - if (!gParentFile) { |
355 |
| - return; |
356 |
| - } |
357 |
| - |
358 |
| - let parentComponentName = path.basename(parsedPath.dir); |
359 |
| - if (parentComponentName[0] == '+') parentComponentName = parentComponentName.substr(1); |
360 |
| - const jsComponentName = stringUtils.classify(parentComponentName); |
361 |
| - const routeRegex = new RegExp(`^\\s*\\{.*component: ${jsComponentName}.*` |
362 |
| - + '\\},?\\s*\\n?', 'm'); |
363 |
| - |
364 |
| - let content = fs.readFileSync(gParentFile, 'utf-8'); |
365 |
| - const m = content.match(routeRegex); |
366 |
| - if (m) { |
367 |
| - // Replace `path: '/blah'` with the proper `path: '/blah/...'`. |
368 |
| - let json = m[0].replace(/(path:\s*['"])([^'"]+?)(['"])/, function(m, prefix, value, suffix) { |
369 |
| - // If the path isn't ending with `...`, add it (with a URL separator). |
370 |
| - if (!value.match(/\.\.\.$/)) { |
371 |
| - if (!value.match(/\/$/)) { |
372 |
| - value += '/'; |
373 |
| - } |
374 |
| - value += '...'; |
375 |
| - } |
376 |
| - return prefix + value + suffix; |
377 |
| - }); |
378 |
| - content = content.substr(0, m.index) + json + content.substr(m.index + m[0].length); |
379 |
| - } |
| 2 | + description: '', |
380 | 3 |
|
381 |
| - fs.writeFileSync(gParentFile, content, 'utf-8'); |
| 4 | + install: function () { |
| 5 | + throw 'Due to changes in the router, route generation has been temporarily disabled'; |
382 | 6 | }
|
383 | 7 | };
|
0 commit comments