Skip to content
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

feat(generate): add ability to specify module for import #3811

Merged
merged 1 commit into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions packages/angular-cli/blueprints/component/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const Blueprint = require('../../ember-cli/lib/models/blueprint');
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
Expand All @@ -19,15 +20,27 @@ module.exports = {
{ name: 'spec', type: Boolean },
{ name: 'view-encapsulation', type: String, aliases: ['ve'] },
{ name: 'change-detection', type: String, aliases: ['cd'] },
{ name: 'skip-import', type: Boolean, default: false }
{ name: 'skip-import', type: Boolean, default: false },
{ name: 'module', type: String, aliases: ['m'] }
],

beforeInstall: function(options) {
try {
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
} catch(e) {
if (!options.skipImport) {
throw `Error locating module for declaration\n\t${e}`;
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
const parsedPath = dynamicPathParser(this.project, modulePath);
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);

if (!fs.existsSync(this.pathToModule)) {
throw 'Module specified does not exist';
}
} else {
try {
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
} catch(e) {
if (!options.skipImport) {
throw `Error locating module for declaration\n\t${e}`;
}
}
}
},
Expand Down
25 changes: 19 additions & 6 deletions packages/angular-cli/blueprints/directive/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
const stringUtils = require('ember-cli-string-utils');
Expand All @@ -15,15 +16,27 @@ module.exports = {
{ name: 'flat', type: Boolean, default: true },
{ name: 'prefix', type: String, default: null },
{ name: 'spec', type: Boolean },
{ name: 'skip-import', type: Boolean, default: false }
{ name: 'skip-import', type: Boolean, default: false },
{ name: 'module', type: String, aliases: ['m'] }
],

beforeInstall: function(options) {
try {
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
} catch(e) {
if (!options.skipImport) {
throw `Error locating module for declaration\n\t${e}`;
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
const parsedPath = dynamicPathParser(this.project, modulePath);
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);

if (!fs.existsSync(this.pathToModule)) {
throw ' ';
}
} else {
try {
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
} catch(e) {
if (!options.skipImport) {
throw `Error locating module for declaration\n\t${e}`;
}
}
}
},
Expand Down
25 changes: 19 additions & 6 deletions packages/angular-cli/blueprints/pipe/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
const stringUtils = require('ember-cli-string-utils');
Expand All @@ -14,15 +15,27 @@ module.exports = {
availableOptions: [
{ name: 'flat', type: Boolean, default: true },
{ name: 'spec', type: Boolean },
{ name: 'skip-import', type: Boolean, default: false }
{ name: 'skip-import', type: Boolean, default: false },
{ name: 'module', type: String, aliases: ['m'] }
],

beforeInstall: function(options) {
try {
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
} catch(e) {
if (!options.skipImport) {
throw `Error locating module for declaration\n\t${e}`;
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
const parsedPath = dynamicPathParser(this.project, modulePath);
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);

if (!fs.existsSync(this.pathToModule)) {
throw 'Module specified does not exist';
}
} else {
try {
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
} catch(e) {
if (!options.skipImport) {
throw `Error locating module for declaration\n\t${e}`;
}
}
}
},
Expand Down
43 changes: 39 additions & 4 deletions packages/angular-cli/blueprints/service/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
const Blueprint = require('../../ember-cli/lib/models/blueprint');
const NodeHost = require('@angular-cli/ast-tools').NodeHost;
const stringUtils = require('ember-cli-string-utils');
const astUtils = require('../../utilities/ast-utils');
const getFiles = Blueprint.prototype.files;

module.exports = {
description: '',

availableOptions: [
{ name: 'flat', type: Boolean, default: true },
{ name: 'spec', type: Boolean }
{ name: 'spec', type: Boolean },
{ name: 'module', type: String, aliases: ['m'] }
],

beforeInstall: function(options) {
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
const parsedPath = dynamicPathParser(this.project, modulePath);
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);

if (!fs.existsSync(this.pathToModule)) {
throw 'Module specified does not exist';
}
}
},

normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName);

Expand Down Expand Up @@ -54,8 +72,25 @@ module.exports = {
};
},

afterInstall() {
const warningMessage = 'Service is generated but not provided, it must be provided to be used';
this._writeStatusToUI(chalk.yellow, 'WARNING', warningMessage);
afterInstall(options) {
const returns = [];

if (!this.pathToModule) {
const warningMessage = 'Service is generated but not provided, it must be provided to be used';
this._writeStatusToUI(chalk.yellow, 'WARNING', warningMessage);
} else {
const className = stringUtils.classify(`${options.entity.name}Service`);
const fileName = stringUtils.dasherize(`${options.entity.name}.service`);
const fullGeneratePath = path.join(this.project.root, this.generatePath);
const moduleDir = path.parse(this.pathToModule).dir;
const relativeDir = path.relative(moduleDir, fullGeneratePath);
const importPath = relativeDir ? `./${relativeDir}/${fileName}` : `./${fileName}`;
returns.push(
astUtils.addProviderToModule(this.pathToModule, className, importPath)
.then(change => change.apply(NodeHost)));
this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule));
}

return Promise.all(returns);
}
};
9 changes: 9 additions & 0 deletions tests/e2e/tests/generate/component/component-module-fail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';


export default function() {
return Promise.resolve()
.then(() => expectToFail(() =>
ng('generate', 'component', 'test-component', '--module', 'app.moduleXXX.ts')));
}
15 changes: 15 additions & 0 deletions tests/e2e/tests/generate/component/component-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {join} from 'path';
import {ng} from '../../../utils/process';
import {expectFileToMatch} from '../../../utils/fs';


export default function() {
const modulePath = join('src', 'app', 'app.module.ts');

return ng('generate', 'component', 'test-component', '--module', 'app.module.ts')
.then(() => expectFileToMatch(modulePath,
/import { TestComponentComponent } from '.\/test-component\/test-component.component'/))

// Try to run the unit tests.
.then(() => ng('build'));
}
14 changes: 14 additions & 0 deletions tests/e2e/tests/generate/directive/directive-basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {ng} from '../../../utils/process';
import {join} from 'path';
import {expectFileToExist} from '../../../utils/fs';


export default function() {
const directiveDir = join('src', 'app');
return ng('generate', 'directive', 'test-directive')
.then(() => expectFileToExist(join(directiveDir, 'test-directive.directive.ts')))
.then(() => expectFileToExist(join(directiveDir, 'test-directive.directive.spec.ts')))

// Try to run the unit tests.
.then(() => ng('test', '--single-run'));
}
8 changes: 8 additions & 0 deletions tests/e2e/tests/generate/directive/directive-module-fail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';

export default function() {
return Promise.resolve()
.then(() => expectToFail(() =>
ng('generate', 'directive', 'test-directive', '--module', 'app.moduleXXX.ts')));
}
15 changes: 15 additions & 0 deletions tests/e2e/tests/generate/directive/directive-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {join} from 'path';
import {ng} from '../../../utils/process';
import {expectFileToMatch} from '../../../utils/fs';


export default function() {
const modulePath = join('src', 'app', 'app.module.ts');

return ng('generate', 'directive', 'test-directive', '--module', 'app.module.ts')
.then(() => expectFileToMatch(modulePath,
/import { TestDirectiveDirective } from '.\/test-directive.directive'/))

// Try to run the unit tests.
.then(() => ng('build'));
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {join} from 'path';
import {ng} from '../../utils/process';
import {expectFileToExist} from '../../utils/fs';
import {ng} from '../../../utils/process';

import {expectFileToExist} from '../../../utils/fs';

export default function() {
// Create the pipe in the same directory.
Expand Down
9 changes: 9 additions & 0 deletions tests/e2e/tests/generate/pipe/pipe-module-fail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';


export default function() {
return Promise.resolve()
.then(() => expectToFail(() =>
ng('generate', 'pipe', 'test-pipe', '--module', 'app.moduleXXX.ts')));
}
15 changes: 15 additions & 0 deletions tests/e2e/tests/generate/pipe/pipe-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {join} from 'path';
import {ng} from '../../../utils/process';
import {expectFileToMatch} from '../../../utils/fs';


export default function() {
const modulePath = join('src', 'app', 'app.module.ts');

return ng('generate', 'pipe', 'test-pipe', '--module', 'app.module.ts')
.then(() => expectFileToMatch(modulePath,
/import { TestPipePipe } from '.\/test-pipe.pipe'/))

// Try to run the unit tests.
.then(() => ng('build'));
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {join} from 'path';
import {ng} from '../../utils/process';
import {expectFileToExist} from '../../utils/fs';
import {ng} from '../../../utils/process';
import {expectFileToExist} from '../../../utils/fs';


export default function() {
Expand Down
9 changes: 9 additions & 0 deletions tests/e2e/tests/generate/service/service-module-fail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';


export default function() {
return Promise.resolve()
.then(() => expectToFail(() =>
ng('generate', 'service', 'test-service', '--module', 'app.moduleXXX.ts')));
}
15 changes: 15 additions & 0 deletions tests/e2e/tests/generate/service/service-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {join} from 'path';
import {ng} from '../../../utils/process';
import {expectFileToMatch} from '../../../utils/fs';


export default function() {
const modulePath = join('src', 'app', 'app.module.ts');

return ng('generate', 'service', 'test-service', '--module', 'app.module.ts')
.then(() => expectFileToMatch(modulePath,
/import { TestServiceService } from '.\/test-service.service'/))

// Try to run the unit tests.
.then(() => ng('build'));
}