diff --git a/addon/ng2/blueprints/route/index.js b/addon/ng2/blueprints/route/index.js index 780de6a19d32..43a8cca7431c 100644 --- a/addon/ng2/blueprints/route/index.js +++ b/addon/ng2/blueprints/route/index.js @@ -6,12 +6,10 @@ const dynamicPathParser = require('../../utilities/dynamic-path-parser'); const stringUtils = require('ember-cli-string-utils'); const Blueprint = require('ember-cli/lib/models/blueprint'); - function _regexEscape(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); } - function _insertImport(content, symbolName, fileName) { // Check if an import from the same file is there. const importRegex = new RegExp('' + @@ -123,7 +121,6 @@ module.exports = { } this._addRouteToParent(options); - this._verifyParentRoute(options); }, afterUninstall: function (options) { @@ -229,16 +226,15 @@ module.exports = { // Insert the import statement. let content = fs.readFileSync(parentFile, 'utf-8'); - content = _insertImport(content, `${jsComponentName}Component`, - `./${options.isLazyRoute ? '+' : ''}${stringUtils.dasherize(base)}`); + content = _insertImport(content, `${jsComponentName}Component`, `./${options.isLazyRoute ? '+' : ''}${stringUtils.dasherize(base)}`); let defaultReg = options.default ? ', useAsDefault: true' : ''; let routePath = options.path || `/${base}`; - let route = '{' + let route = '{ ' + `path: '${routePath}', ` + `component: ${jsComponentName}Component` + defaultReg - + '}'; + + ' }'; // Add the route configuration. content = _addRoutes(content); @@ -256,7 +252,7 @@ module.exports = { content = content.replace(/(@Component\(\{)([\s\S\n]*?)(\n\}\))/m, function(_, prefix, json, suffix) { const m = json.match(/(^\s+directives:\s*\[)([\s\S\n]*)(\]\s*,?.*$)/m); if (m) { - if (m[2].indexOf('ROUTER_DIRECTIVES') != -1) { + if (m[2].indexOf('ROUTER_DIRECTIVES') !== -1) { // Already there. return _; } @@ -314,6 +310,7 @@ module.exports = { } else { // There's no template, look for the HTML file. const htmlFile = parentFile.replace(/\.ts$/, '.html'); + if (!fs.existsSync(htmlFile)) { // eslint-disable-next-line no-console console.log('Cannot find HTML: ' + htmlFile); @@ -333,45 +330,5 @@ module.exports = { }); fs.writeFileSync(parentFile, content, 'utf-8'); - }, - - _verifyParentRoute: function() { - const parsedPath = this.dynamicPath; - const parentFile = this._findParentRouteFile(parsedPath.dir); - if (!parentFile) { - return; - } - - const gParentDir = path.dirname(path.dirname(parentFile)); - const gParentFile = this._findParentRouteFile(gParentDir); - - if (!gParentFile) { - return; - } - - let parentComponentName = path.basename(parsedPath.dir); - if (parentComponentName[0] == '+') parentComponentName = parentComponentName.substr(1); - const jsComponentName = stringUtils.classify(parentComponentName); - const routeRegex = new RegExp(`^\\s*\\{.*component: ${jsComponentName}.*` - + '\\},?\\s*\\n?', 'm'); - - let content = fs.readFileSync(gParentFile, 'utf-8'); - const m = content.match(routeRegex); - if (m) { - // Replace `path: '/blah'` with the proper `path: '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/blah/...'`. - let json = m[0].replace(/(path:\s*['"])([^'"]+?)(['"])/, function(m, prefix, value, suffix) { - // If the path isn't ending with `...`, add it (with a URL separator). - if (!value.match(/\.\.\.$/)) { - if (!value.match(/\/$/)) { - value += '/'; - } - value += '...'; - } - return prefix + value + suffix; - }); - content = content.substr(0, m.index) + json + content.substr(m.index + m[0].length); - } - - fs.writeFileSync(gParentFile, content, 'utf-8'); } -}; +}; \ No newline at end of file diff --git a/tests/acceptance/generate-route.spec.js b/tests/acceptance/generate-route.spec.js index ce665427a56d..19d4f16289b7 100644 --- a/tests/acceptance/generate-route.spec.js +++ b/tests/acceptance/generate-route.spec.js @@ -72,8 +72,8 @@ describe('Acceptance: ng generate route', function () { const myOtherRouteContent = fs.readFileSync(path.join(testPath, '+my-route/+my-other/my-other.component.ts'), 'utf-8'); const myThirdRouteContent = fs.readFileSync(path.join(testPath, '+my-route/+my-other/+my-third/my-third.component.ts'), 'utf-8'); - expect(appContent).to.match(/@Routes\(\[[\s\S]+\/\+my-route\/\.\.\.[\s\S]+\]\)/m); - expect(myRouteContent).to.match(/@Routes\(\[[\s\S]+\/my-other\/\.\.\.[\s\S]+\]\)/m); + expect(appContent).to.match(/@Routes\(\[[\s\S]+\/\+my-route[\s\S]+\]\)/m); + expect(myRouteContent).to.match(/@Routes\(\[[\s\S]+\/my-other[\s\S]+\]\)/m); expect(myOtherRouteContent).to.match(/@Routes\(\[[\s\S]+\/my-third[^\.][\s\S]+\]\)/m); expect(myThirdRouteContent).to.not.include('@Routes'); }); @@ -102,4 +102,4 @@ describe('Acceptance: ng generate route', function () { expect(afterGenerateParentHtml).to.equal(unmodifiedParentComponentHtml); }); }); -}); +}); \ No newline at end of file