From 8c57136a32e26cc09907960d0edd9a61d7b56eb7 Mon Sep 17 00:00:00 2001 From: cexbrayat Date: Thu, 4 May 2017 17:08:08 +0200 Subject: [PATCH] fix(@angular/cli): simplify import path if possible Fixes #6183 --- packages/@angular/cli/blueprints/component/index.ts | 3 ++- packages/@angular/cli/blueprints/directive/index.ts | 3 ++- packages/@angular/cli/blueprints/guard/index.ts | 3 ++- packages/@angular/cli/blueprints/pipe/index.ts | 3 ++- packages/@angular/cli/blueprints/service/index.ts | 3 ++- tests/acceptance/generate-component.spec.js | 8 ++++---- tests/acceptance/generate-directive.spec.js | 8 ++++---- tests/acceptance/generate-guard.spec.js | 8 ++++---- tests/acceptance/generate-pipe.spec.js | 8 ++++---- tests/acceptance/generate-service.spec.js | 8 ++++---- 10 files changed, 30 insertions(+), 25 deletions(-) diff --git a/packages/@angular/cli/blueprints/component/index.ts b/packages/@angular/cli/blueprints/component/index.ts index ff501944d21b..0ef29c9245ea 100644 --- a/packages/@angular/cli/blueprints/component/index.ts +++ b/packages/@angular/cli/blueprints/component/index.ts @@ -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) { diff --git a/packages/@angular/cli/blueprints/directive/index.ts b/packages/@angular/cli/blueprints/directive/index.ts index 7de1252489f3..99e0bdc7e749 100644 --- a/packages/@angular/cli/blueprints/directive/index.ts +++ b/packages/@angular/cli/blueprints/directive/index.ts @@ -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) { diff --git a/packages/@angular/cli/blueprints/guard/index.ts b/packages/@angular/cli/blueprints/guard/index.ts index 5b3c8c233bcf..c700994dd178 100644 --- a/packages/@angular/cli/blueprints/guard/index.ts +++ b/packages/@angular/cli/blueprints/guard/index.ts @@ -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))); diff --git a/packages/@angular/cli/blueprints/pipe/index.ts b/packages/@angular/cli/blueprints/pipe/index.ts index 9e440d4cc0a5..abbcab5b385e 100644 --- a/packages/@angular/cli/blueprints/pipe/index.ts +++ b/packages/@angular/cli/blueprints/pipe/index.ts @@ -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) { diff --git a/packages/@angular/cli/blueprints/service/index.ts b/packages/@angular/cli/blueprints/service/index.ts index 23f5cbd34049..3d200bc3130e 100644 --- a/packages/@angular/cli/blueprints/service/index.ts +++ b/packages/@angular/cli/blueprints/service/index.ts @@ -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))); diff --git a/tests/acceptance/generate-component.spec.js b/tests/acceptance/generate-component.spec.js index d8be2a6cd0ee..59c4b17eb83e 100644 --- a/tests/acceptance/generate-component.spec.js +++ b/tests/acceptance/generate-component.spec.js @@ -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); }); }); @@ -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); }); }); @@ -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); }); }); @@ -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); }); }); diff --git a/tests/acceptance/generate-directive.spec.js b/tests/acceptance/generate-directive.spec.js index 66eae61abbda..14f0b4c77513 100644 --- a/tests/acceptance/generate-directive.spec.js +++ b/tests/acceptance/generate-directive.spec.js @@ -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); }); }); @@ -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); }); }); @@ -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); }); }); @@ -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); }); }); diff --git a/tests/acceptance/generate-guard.spec.js b/tests/acceptance/generate-guard.spec.js index 90a5be081563..a1d8052a06a6 100644 --- a/tests/acceptance/generate-guard.spec.js +++ b/tests/acceptance/generate-guard.spec.js @@ -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); }); }); @@ -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); }); }); @@ -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); }); }); @@ -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); }); }); diff --git a/tests/acceptance/generate-pipe.spec.js b/tests/acceptance/generate-pipe.spec.js index 96f113f35552..0b98f789a484 100644 --- a/tests/acceptance/generate-pipe.spec.js +++ b/tests/acceptance/generate-pipe.spec.js @@ -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); }); }); @@ -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); }); }); @@ -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); }); }); @@ -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); }); }); diff --git a/tests/acceptance/generate-service.spec.js b/tests/acceptance/generate-service.spec.js index 69895443301d..7850fcd7a59d 100644 --- a/tests/acceptance/generate-service.spec.js +++ b/tests/acceptance/generate-service.spec.js @@ -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); }); }); @@ -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); }); }); @@ -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); }); }); @@ -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); }); });