Skip to content
55 changes: 6 additions & 49 deletions addon/ng2/blueprints/route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('' +
Expand Down Expand Up @@ -123,7 +121,6 @@ module.exports = {
}

this._addRouteToParent(options);
this._verifyParentRoute(options);
},

afterUninstall: function (options) {
Expand Down Expand Up @@ -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);
Expand All @@ -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 _;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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: '/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');
}
};
};
6 changes: 3 additions & 3 deletions tests/acceptance/generate-route.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down Expand Up @@ -102,4 +102,4 @@ describe('Acceptance: ng generate route', function () {
expect(afterGenerateParentHtml).to.equal(unmodifiedParentComponentHtml);
});
});
});
});