Skip to content

chore: generation paths are fully dynamic & supports --flat for compo… #401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import {provide} from 'angular2/core';
import {<%= classifiedModuleName %>} from './<%= dasherizedModuleName %>';


describe('<%= classifiedModuleName %> Component', () => {

beforeEachProviders((): any[] => []);
Expand Down
21 changes: 14 additions & 7 deletions addon/ng2/blueprints/component/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
var path = require('path');
var dynamicPathParser = require('../../utilities/dynamic-path-parser');

module.exports = {
description: '',

availableOptions: [
{ name: 'flat', type: Boolean, default: false, aliases: ['f'] }
],

normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName);
Expand All @@ -10,20 +15,22 @@ module.exports = {
return parsedPath.name;
},

locals: function () {
locals: function (options) {
return {
dynamicPath: this.dynamicPath.dir
dynamicPath: this.dynamicPath.dir.replace(this.dynamicPath.appRoot, ''),
flat: options.flat
};
},

fileMapTokens: function () {
fileMapTokens: function (options) {
// Return custom template variables here.
return {
__name__: () => {
return this.dynamicPath.name;
},
__path__: () => {
return this.dynamicPath.dir;
var dir = this.dynamicPath.dir;
if (!options.locals.flat) {
dir += path.sep + options.dasherizedModuleName;
}
return dir;
}
};
}
Expand Down
21 changes: 14 additions & 7 deletions addon/ng2/blueprints/directive/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
var path = require('path');
var dynamicPathParser = require('../../utilities/dynamic-path-parser');

module.exports = {
description: '',

availableOptions: [
{ name: 'flat', type: Boolean, default: false, aliases: ['f'] }
],

normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName);
Expand All @@ -10,20 +15,22 @@ module.exports = {
return parsedPath.name;
},

locals: function () {
locals: function (options) {
return {
dynamicPath: this.dynamicPath.dir
dynamicPath: this.dynamicPath.dir,
flat: options.flat
};
},

fileMapTokens: function () {
fileMapTokens: function (options) {
// Return custom template variables here.
return {
__name__: () => {
return this.dynamicPath.name;
},
__path__: () => {
return this.dynamicPath.dir;
var dir = this.dynamicPath.dir;
if (!options.locals.flat) {
dir += path.sep + options.dasherizedModuleName;
}
return dir;
}
};
}
Expand Down
23 changes: 16 additions & 7 deletions addon/ng2/blueprints/pipe/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
var path = require('path');
var dynamicPathParser = require('../../utilities/dynamic-path-parser');

module.exports = {
description: '',

availableOptions: [
{ name: 'flat', type: Boolean, default: false, aliases: ['f'] }
],

normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName);
Expand All @@ -10,18 +15,22 @@ module.exports = {
return parsedPath.name;
},

locals: function () {
return { dynamicPath: this.dynamicPath.dir };
locals: function (options) {
return {
dynamicPath: this.dynamicPath.dir,
flat: options.flat
};
},

fileMapTokens: function () {
fileMapTokens: function (options) {
// Return custom template variables here.
return {
__name__: () => {
return this.dynamicPath.name;
},
__path__: () => {
return this.dynamicPath.dir;
var dir = this.dynamicPath.dir;
if (!options.locals.flat) {
dir += path.sep + options.dasherizedModuleName;
}
return dir;
}
};
}
Expand Down
21 changes: 19 additions & 2 deletions addon/ng2/blueprints/route-config/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
var fs = require('fs-extra');
var path = require('path');
var dynamicPathParser = require('../../utilities/dynamic-path-parser');

var imports, routeDefinitions;

module.exports = {
description: 'Registers the route with the router.',

locals: function (options) {
return generateLocals.call(this, options);
normalizeEntityName: function () {
var parsedPath = dynamicPathParser(this.project, 'ignore');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the entity name here supposed to be ignore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no dynamically named items in this blueprint, so I just needed some text to pass in, route-config just generates a route-config.ts file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok. I suppose that is being postponed until we have more info regarding the style guide, yes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, what I've been adding is to put the underlining support in to allow the blueprints to align with the style guide


this.dynamicPath = parsedPath;
return parsedPath.name;
},

beforeInstall: function (options) {
Expand All @@ -17,6 +21,19 @@ module.exports = {
} catch (e) {
// doing nothing here
}
},

locals: function (options) {
return generateLocals.call(this, options);
},

fileMapTokens: function () {
// Return custom template variables here.
return {
__path__: () => {
return this.dynamicPath.dir;
}
};
}
};

Expand Down
23 changes: 16 additions & 7 deletions addon/ng2/blueprints/service/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
var path = require('path');
var dynamicPathParser = require('../../utilities/dynamic-path-parser');

module.exports = {
description: '',

availableOptions: [
{ name: 'flat', type: Boolean, default: false, aliases: ['f'] }
],

normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName);
Expand All @@ -10,18 +15,22 @@ module.exports = {
return parsedPath.name;
},

locals: function () {
return { dynamicPath: this.dynamicPath.dir };
locals: function (options) {
return {
dynamicPath: this.dynamicPath.dir,
flat: options.flat
};
},

fileMapTokens: function () {
fileMapTokens: function (options) {
// Return custom template variables here.
return {
__name__: () => {
return this.dynamicPath.name;
},
__path__: () => {
return this.dynamicPath.dir;
var dir = this.dynamicPath.dir;
if (!options.locals.flat) {
dir += path.sep + options.dasherizedModuleName;
}
return dir;
}
};
}
Expand Down
8 changes: 5 additions & 3 deletions addon/ng2/utilities/dynamic-path-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ var process = require('process');

module.exports = function dynamicPathParser(project, entityName) {
var projectRoot = project.root;
var appRoot = path.join('src', 'client', 'app');
var cwd = process.env.PWD;

var rootPath = path.join(projectRoot, 'src', 'client', 'app');
var rootPath = path.join(projectRoot, appRoot);

var outputPath = path.join(rootPath, entityName);

Expand All @@ -31,14 +32,15 @@ module.exports = function dynamicPathParser(project, entityName) {

if (outputPath.indexOf(rootPath) < 0) {
throw `Invalid path: "${entityName}" cannot be ` +
`above the "${path.join('src', 'app')}" directory`;
`above the "${appRoot}" directory`;
}

var adjustedPath = outputPath.replace(rootPath, '');
var adjustedPath = outputPath.replace(projectRoot, '');

var parsedPath = path.parse(adjustedPath);

parsedPath.dir = parsedPath.dir === path.sep ? '' : parsedPath.dir;
parsedPath.appRoot = appRoot

return parsedPath;
};
16 changes: 9 additions & 7 deletions tests/acceptance/dynamic-path-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var expect = require('chai').expect;
var path = require('path');
var dynamicPathParser = require('../../addon/ng2/utilities/dynamic-path-parser');

var appDir = `${path.sep}src${path.sep}client${path.sep}app`;

describe('dynamic path parser', () => {
var project;
var entityName = 'temp-name';
Expand All @@ -14,50 +16,50 @@ describe('dynamic path parser', () => {
it('parse from proj root dir', () => {
process.env.PWD = process.cwd();
var result = dynamicPathParser(project, entityName);
expect(result.dir).to.equal('');
expect(result.dir).to.equal(appDir);
expect(result.name).to.equal(entityName);
});

it('parse from proj src dir', () => {
process.env.PWD = path.join(process.cwd(), 'src');
var result = dynamicPathParser(project, entityName);
expect(result.dir).to.equal('');
expect(result.dir).to.equal(appDir);
expect(result.name).to.equal(entityName);
});

it(`parse from proj src${path.sep}client dir`, () => {
process.env.PWD = path.join(process.cwd(), 'src', 'client');
var result = dynamicPathParser(project, entityName);
expect(result.dir).to.equal('');
expect(result.dir).to.equal(appDir);
expect(result.name).to.equal(entityName);
});

it(`parse from proj src${path.sep}client${path.sep}app dir`, () => {
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app');
var result = dynamicPathParser(project, entityName);
expect(result.dir).to.equal('');
expect(result.dir).to.equal(appDir);
expect(result.name).to.equal(entityName);
});

it(`parse from proj src${path.sep}client${path.sep}app${path.sep}child-dir`, () => {
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app', 'child-dir');
var result = dynamicPathParser(project, entityName);
expect(result.dir).to.equal(`${path.sep}child-dir`);
expect(result.dir).to.equal(`${appDir}${path.sep}child-dir`);
expect(result.name).to.equal(entityName);
});

it(`parse from proj src${path.sep}client${path.sep}app${path.sep}child-dir w/ ..${path.sep}`, () => {
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app', 'child-dir');
var result = dynamicPathParser(project, '..' + path.sep + entityName);
expect(result.dir).to.equal('');
expect(result.dir).to.equal(appDir);
expect(result.name).to.equal(entityName);
});

it(`parse from proj src${path.sep}client${path.sep}app${path.sep}child-dir${path.sep}grand-child-dir w/ ..${path.sep}`,
() => {
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app', 'child-dir', 'grand-child-dir');
var result = dynamicPathParser(project, '..' + path.sep + entityName);
expect(result.dir).to.equal(`${path.sep}child-dir`);
expect(result.dir).to.equal(`${appDir}${path.sep}child-dir`);
expect(result.name).to.equal(entityName);
});
});