Skip to content

Commit

Permalink
fix(@angular/cli): simplify import path if possible (#6184)
Browse files Browse the repository at this point in the history
Fixes #6183
  • Loading branch information
cexbrayat authored and filipesilva committed May 9, 2017
1 parent 1cc979f commit 2d42a58
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 25 deletions.
3 changes: 2 additions & 1 deletion packages/@angular/cli/blueprints/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ export default Blueprint.extend({
const className = stringUtils.classify(`${options.entity.name}Component`);
const fileName = stringUtils.dasherize(`${options.entity.name}.component`);
const componentDir = path.relative(path.dirname(this.pathToModule), this.generatePath);
const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`;
const normalizeRelativeDir = componentDir.startsWith('.') ? componentDir : `./${componentDir}`;
const importPath = componentDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`;

if (!options.skipImport) {
if (options.dryRun) {
Expand Down
3 changes: 2 additions & 1 deletion packages/@angular/cli/blueprints/directive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ export default Blueprint.extend({
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}`;
const normalizeRelativeDir = relativeDir.startsWith('.') ? relativeDir : `./${relativeDir}`;
const importPath = relativeDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`;

if (!options.skipImport) {
if (options.dryRun) {
Expand Down
3 changes: 2 additions & 1 deletion packages/@angular/cli/blueprints/guard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export default Blueprint.extend({
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}`;
const normalizeRelativeDir = relativeDir.startsWith('.') ? relativeDir : `./${relativeDir}`;
const importPath = relativeDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`;
returns.push(
astUtils.addProviderToModule(this.pathToModule, className, importPath)
.then((change: any) => change.apply(NodeHost)));
Expand Down
3 changes: 2 additions & 1 deletion packages/@angular/cli/blueprints/pipe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export default Blueprint.extend({
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}`;
const normalizeRelativeDir = relativeDir.startsWith('.') ? relativeDir : `./${relativeDir}`;
const importPath = relativeDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`;

if (!options.skipImport) {
if (options.dryRun) {
Expand Down
3 changes: 2 additions & 1 deletion packages/@angular/cli/blueprints/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ export default Blueprint.extend({
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}`;
const normalizeRelativeDir = relativeDir.startsWith('.') ? relativeDir : `./${relativeDir}`;
const importPath = relativeDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`;
returns.push(
astUtils.addProviderToModule(this.pathToModule, className, importPath)
.then((change: any) => change.apply(NodeHost)));
Expand Down
8 changes: 4 additions & 4 deletions tests/acceptance/generate-component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ describe('Acceptance: ng generate component', function () {
.then(() => ng(['generate', 'component', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazComponent.*from '.\/..\/baz\/baz.component';/);
expect(content).matches(/import.*BazComponent.*from '..\/baz\/baz.component';/);
expect(content).matches(/declarations:\s+\[BazComponent]/m);
});
});
Expand All @@ -281,7 +281,7 @@ describe('Acceptance: ng generate component', function () {
.then(() => ng(['generate', 'component', 'baz', '--module', path.join('foo', 'foo')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazComponent.*from '.\/..\/baz\/baz.component';/);
expect(content).matches(/import.*BazComponent.*from '..\/baz\/baz.component';/);
expect(content).matches(/declarations:\s+\[BazComponent]/m);
});
});
Expand All @@ -295,7 +295,7 @@ describe('Acceptance: ng generate component', function () {
.then(() => ng(['generate', 'component', 'baz', '--module', 'foo']))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazComponent.*from '.\/..\/baz\/baz.component';/);
expect(content).matches(/import.*BazComponent.*from '..\/baz\/baz.component';/);
expect(content).matches(/declarations:\s+\[BazComponent]/m);
});
});
Expand All @@ -310,7 +310,7 @@ describe('Acceptance: ng generate component', function () {
.then(() => ng(['generate', 'component', 'baz', '--module', path.join('foo', 'bar')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazComponent.*from '.\/..\/..\/baz\/baz.component';/);
expect(content).matches(/import.*BazComponent.*from '..\/..\/baz\/baz.component';/);
expect(content).matches(/declarations:\s+\[BazComponent]/m);
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/acceptance/generate-directive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe('Acceptance: ng generate directive', function () {
.then(() => ng(['generate', 'directive', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazDirective.*from '.\/..\/baz.directive';/);
expect(content).matches(/import.*BazDirective.*from '..\/baz.directive';/);
expect(content).matches(/declarations:\s+\[BazDirective]/m);
});
});
Expand All @@ -236,7 +236,7 @@ describe('Acceptance: ng generate directive', function () {
.then(() => ng(['generate', 'directive', 'baz', '--module', path.join('foo', 'foo')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazDirective.*from '.\/..\/baz.directive';/);
expect(content).matches(/import.*BazDirective.*from '..\/baz.directive';/);
expect(content).matches(/declarations:\s+\[BazDirective]/m);
});
});
Expand All @@ -250,7 +250,7 @@ describe('Acceptance: ng generate directive', function () {
.then(() => ng(['generate', 'directive', 'baz', '--module', 'foo']))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazDirective.*from '.\/..\/baz.directive';/);
expect(content).matches(/import.*BazDirective.*from '..\/baz.directive';/);
expect(content).matches(/declarations:\s+\[BazDirective]/m);
});
});
Expand All @@ -265,7 +265,7 @@ describe('Acceptance: ng generate directive', function () {
.then(() => ng(['generate', 'directive', 'baz', '--module', path.join('foo', 'bar')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazDirective.*from '.\/..\/..\/baz.directive';/);
expect(content).matches(/import.*BazDirective.*from '..\/..\/baz.directive';/);
expect(content).matches(/declarations:\s+\[BazDirective]/m);
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/acceptance/generate-guard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('Acceptance: ng generate guard', function () {
.then(() => ng(['generate', 'guard', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).to.matches(/import.*BazGuard.*from '.\/..\/baz.guard';/);
expect(content).to.matches(/import.*BazGuard.*from '..\/baz.guard';/);
expect(content).to.matches(/providers:\s*\[BazGuard\]/m);
});
});
Expand All @@ -223,7 +223,7 @@ describe('Acceptance: ng generate guard', function () {
.then(() => ng(['generate', 'guard', 'baz', '--module', path.join('foo', 'foo')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).to.matches(/import.*BazGuard.*from '.\/..\/baz.guard';/);
expect(content).to.matches(/import.*BazGuard.*from '..\/baz.guard';/);
expect(content).to.matches(/providers:\s*\[BazGuard\]/m);
});
});
Expand All @@ -237,7 +237,7 @@ describe('Acceptance: ng generate guard', function () {
.then(() => ng(['generate', 'guard', 'baz', '--module', 'foo']))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).to.matches(/import.*BazGuard.*from '.\/..\/baz.guard';/);
expect(content).to.matches(/import.*BazGuard.*from '..\/baz.guard';/);
expect(content).to.matches(/providers:\s*\[BazGuard\]/m);
});
});
Expand All @@ -252,7 +252,7 @@ describe('Acceptance: ng generate guard', function () {
.then(() => ng(['generate', 'guard', 'baz', '--module', path.join('foo', 'bar')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).to.matches(/import.*BazGuard.*from '.\/..\/..\/baz.guard';/);
expect(content).to.matches(/import.*BazGuard.*from '..\/..\/baz.guard';/);
expect(content).to.matches(/providers:\s*\[BazGuard\]/m);
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/acceptance/generate-pipe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe('Acceptance: ng generate pipe', function () {
.then(() => ng(['generate', 'pipe', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazPipe.*from '.\/..\/baz.pipe';/);
expect(content).matches(/import.*BazPipe.*from '..\/baz.pipe';/);
expect(content).matches(/declarations:\s+\[BazPipe]/m);
});
});
Expand All @@ -214,7 +214,7 @@ describe('Acceptance: ng generate pipe', function () {
.then(() => ng(['generate', 'pipe', 'baz', '--module', path.join('foo', 'foo')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazPipe.*from '.\/..\/baz.pipe';/);
expect(content).matches(/import.*BazPipe.*from '..\/baz.pipe';/);
expect(content).matches(/declarations:\s+\[BazPipe]/m);
});
});
Expand All @@ -228,7 +228,7 @@ describe('Acceptance: ng generate pipe', function () {
.then(() => ng(['generate', 'pipe', 'baz', '--module', 'foo']))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazPipe.*from '.\/..\/baz.pipe';/);
expect(content).matches(/import.*BazPipe.*from '..\/baz.pipe';/);
expect(content).matches(/declarations:\s+\[BazPipe]/m);
});
});
Expand All @@ -243,7 +243,7 @@ describe('Acceptance: ng generate pipe', function () {
.then(() => ng(['generate', 'pipe', 'baz', '--module', path.join('foo', 'bar')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).matches(/import.*BazPipe.*from '.\/..\/..\/baz.pipe';/);
expect(content).matches(/import.*BazPipe.*from '..\/..\/baz.pipe';/);
expect(content).matches(/declarations:\s+\[BazPipe]/m);
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/acceptance/generate-service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('Acceptance: ng generate service', function () {
.then(() => ng(['generate', 'service', 'baz', '--module', path.join('foo', 'foo.module.ts')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).to.matches(/import.*BazService.*from '.\/..\/baz.service';/);
expect(content).to.matches(/import.*BazService.*from '..\/baz.service';/);
expect(content).to.matches(/providers:\s*\[BazService\]/m);
});
});
Expand All @@ -224,7 +224,7 @@ describe('Acceptance: ng generate service', function () {
.then(() => ng(['generate', 'service', 'baz', '--module', path.join('foo', 'foo')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).to.matches(/import.*BazService.*from '.\/..\/baz.service';/);
expect(content).to.matches(/import.*BazService.*from '..\/baz.service';/);
expect(content).to.matches(/providers:\s*\[BazService\]/m);
});
});
Expand All @@ -238,7 +238,7 @@ describe('Acceptance: ng generate service', function () {
.then(() => ng(['generate', 'service', 'baz', '--module', 'foo']))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).to.matches(/import.*BazService.*from '.\/..\/baz.service';/);
expect(content).to.matches(/import.*BazService.*from '..\/baz.service';/);
expect(content).to.matches(/providers:\s*\[BazService\]/m);
});
});
Expand All @@ -253,7 +253,7 @@ describe('Acceptance: ng generate service', function () {
.then(() => ng(['generate', 'service', 'baz', '--module', path.join('foo', 'bar')]))
.then(() => readFile(modulePath, 'utf-8'))
.then(content => {
expect(content).to.matches(/import.*BazService.*from '.\/..\/..\/baz.service';/);
expect(content).to.matches(/import.*BazService.*from '..\/..\/baz.service';/);
expect(content).to.matches(/providers:\s*\[BazService\]/m);
});
});
Expand Down

0 comments on commit 2d42a58

Please sign in to comment.