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

fix(@angular/cli): simplify import path if possible #6184

Merged
merged 1 commit into from
May 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you instead use path.normalize on importPath? It guarantees the path is always simplified.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sadly path.normalize will simplify import {Hello} from './hello/hello' to import {Hello} from 'hello/hello', which breaks the app...

Copy link
Contributor

Choose a reason for hiding this comment

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

Did not know that, you're right. Plus on windows it also replaces the slashes.


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