From 6334ed690af810798f4634b186b4052cf2b9e81f Mon Sep 17 00:00:00 2001 From: Andrew Wong Date: Sun, 6 Aug 2017 13:25:13 -0400 Subject: [PATCH 1/3] fix(@angular/cli): fix import path for component when importing into a module This is similar to 86021a0910495e61ec8bc539001b3755e928173d. Fix #7135 --- packages/@angular/cli/blueprints/component/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/@angular/cli/blueprints/component/index.ts b/packages/@angular/cli/blueprints/component/index.ts index e618d41470bc..0d46d747b1be 100644 --- a/packages/@angular/cli/blueprints/component/index.ts +++ b/packages/@angular/cli/blueprints/component/index.ts @@ -238,9 +238,11 @@ export default Blueprint.extend({ const returns: Array = []; 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 normalizeRelativeDir = componentDir.startsWith('.') ? componentDir : `./${componentDir}`; - const importPath = componentDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`; + const fullGeneratePath = path.join(this.project.root, this.generatePath); + const moduleDir = path.parse(this.pathToModule).dir; + const relativeDir = path.relative(moduleDir, fullGeneratePath); + const normalizeRelativeDir = relativeDir.startsWith('.') ? relativeDir : `./${relativeDir}`; + const importPath = relativeDir ? `${normalizeRelativeDir}/${fileName}` : `./${fileName}`; if (!options.skipImport) { if (options.dryRun) { From 5e35aff372af595a1ee7c804ce43cbc58483b4fb Mon Sep 17 00:00:00 2001 From: Andrew Wong Date: Tue, 8 Aug 2017 01:22:45 -0400 Subject: [PATCH 2/3] test(@angular/cli): Add tests for running 'generate' in a subdirectory (#7135) --- .../e2e/tests/generate/component/component-module.ts | 11 ++++++++++- .../e2e/tests/generate/directive/directive-module.ts | 11 ++++++++++- tests/e2e/tests/generate/guard/guard-module.ts | 12 +++++++++++- tests/e2e/tests/generate/module/module-import.ts | 12 +++++++++++- tests/e2e/tests/generate/pipe/pipe-module.ts | 11 ++++++++++- tests/e2e/tests/generate/service/service-module.ts | 11 ++++++++++- 6 files changed, 62 insertions(+), 6 deletions(-) diff --git a/tests/e2e/tests/generate/component/component-module.ts b/tests/e2e/tests/generate/component/component-module.ts index 4cbda4b5d07e..e742a1ecabeb 100644 --- a/tests/e2e/tests/generate/component/component-module.ts +++ b/tests/e2e/tests/generate/component/component-module.ts @@ -1,15 +1,24 @@ +import * as fs from 'fs-extra'; 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'); + const root = process.cwd(); + const modulePath = join(root, 'src', 'app', 'app.module.ts'); + + fs.mkdirSync('./src/app/sub-dir'); return ng('generate', 'component', 'test-component', '--module', 'app.module.ts') .then(() => expectFileToMatch(modulePath, /import { TestComponentComponent } from '.\/test-component\/test-component.component'/)) + .then(() => process.chdir(join(root, 'src', 'app'))) + .then(() => ng('generate', 'component', 'test-component2', '--module', 'app.module.ts')) + .then(() => expectFileToMatch(modulePath, + /import { TestComponent2Component } from '.\/test-component2\/test-component2.component'/)) + // Try to run the unit tests. .then(() => ng('build')); } diff --git a/tests/e2e/tests/generate/directive/directive-module.ts b/tests/e2e/tests/generate/directive/directive-module.ts index c95637762896..2667c390c910 100644 --- a/tests/e2e/tests/generate/directive/directive-module.ts +++ b/tests/e2e/tests/generate/directive/directive-module.ts @@ -1,15 +1,24 @@ +import * as fs from 'fs-extra'; 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'); + const root = process.cwd(); + const modulePath = join(root, 'src', 'app', 'app.module.ts'); + + fs.mkdirSync('./src/app/sub-dir'); return ng('generate', 'directive', 'test-directive', '--module', 'app.module.ts') .then(() => expectFileToMatch(modulePath, /import { TestDirectiveDirective } from '.\/test-directive.directive'/)) + .then(() => process.chdir(join(root, 'src', 'app'))) + .then(() => ng('generate', 'directive', 'test-directive2', '--module', 'app.module.ts')) + .then(() => expectFileToMatch(modulePath, + /import { TestDirective2Directive } from '.\/test-directive2.directive'/)) + // Try to run the unit tests. .then(() => ng('build')); } diff --git a/tests/e2e/tests/generate/guard/guard-module.ts b/tests/e2e/tests/generate/guard/guard-module.ts index 7ead4bdd903d..8718fdd700ae 100644 --- a/tests/e2e/tests/generate/guard/guard-module.ts +++ b/tests/e2e/tests/generate/guard/guard-module.ts @@ -1,15 +1,25 @@ +import * as fs from 'fs-extra'; 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'); + const root = process.cwd(); + const modulePath = join(root, 'src', 'app', 'app.module.ts'); + + fs.mkdirSync('./src/app/sub-dir'); return ng('generate', 'guard', 'test-guard', '--module', 'app.module.ts') .then(() => expectFileToMatch(modulePath, /import { TestGuardGuard } from '.\/test-guard.guard'/)) .then(() => expectFileToMatch(modulePath, /providers:\s*\[TestGuardGuard\]/m)) + + .then(() => process.chdir(join(root, 'src', 'app'))) + .then(() => ng('generate', 'guard', 'test-guard2', '--module', 'app.module.ts')) + .then(() => expectFileToMatch(modulePath, + /import { TestGuard2Guard } from '.\/test-guard2.guard'/)) + .then(() => ng('build')); } diff --git a/tests/e2e/tests/generate/module/module-import.ts b/tests/e2e/tests/generate/module/module-import.ts index 2f2f2104fe9c..48515249c20b 100644 --- a/tests/e2e/tests/generate/module/module-import.ts +++ b/tests/e2e/tests/generate/module/module-import.ts @@ -1,12 +1,16 @@ +import * as fs from 'fs-extra'; 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'); + const root = process.cwd(); + const modulePath = join(root, 'src', 'app', 'app.module.ts'); const subModulePath = join('src', 'app', 'sub', 'sub.module.ts'); const deepSubModulePath = join('src', 'app', 'sub', 'deep', 'deep.module.ts'); + fs.mkdirSync('./src/app/sub-dir'); + return Promise.resolve() .then(() => ng('generate', 'module', 'sub')) .then(() => ng('generate', 'module', 'sub/deep')) @@ -40,4 +44,10 @@ export default function () { .then(() => expectFileToMatch(deepSubModulePath, /import { Test6Module } from '.\/..\/..\/test6\/test6.module'/)) .then(() => expectFileToMatch(deepSubModulePath, /imports: \[(.|\s)*Test6Module(.|\s)*\]/m))); + + .then(() => process.chdir(join(root, 'src', 'app'))) + .then(() => ng('generate', 'module', 'test7', '--module', 'app.module.ts'))) + .then(() => expectFileToMatch(modulePath, + /import { Test7Module } from '.\/test7\/test7.module'/)) + .then(() => expectFileToMatch(modulePath, /imports: \[(.|\s)*Test7Module(.|\s)*\]/m)) } diff --git a/tests/e2e/tests/generate/pipe/pipe-module.ts b/tests/e2e/tests/generate/pipe/pipe-module.ts index 65d938e0223a..c77d2d056943 100644 --- a/tests/e2e/tests/generate/pipe/pipe-module.ts +++ b/tests/e2e/tests/generate/pipe/pipe-module.ts @@ -1,15 +1,24 @@ +import * as fs from 'fs-extra'; 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'); + const root = process.cwd(); + const modulePath = join(root, 'src', 'app', 'app.module.ts'); + + fs.mkdirSync('./src/app/sub-dir'); return ng('generate', 'pipe', 'test-pipe', '--module', 'app.module.ts') .then(() => expectFileToMatch(modulePath, /import { TestPipePipe } from '.\/test-pipe.pipe'/)) + .then(() => process.chdir(join(root, 'src', 'app'))) + .then(() => ng('generate', 'pipe', 'test-pipe2', '--module', 'app.module.ts')) + .then(() => expectFileToMatch(modulePath, + /import { TestPipe2Pipe } from '.\/test-pipe2.pipe'/)) + // Try to run the unit tests. .then(() => ng('build')); } diff --git a/tests/e2e/tests/generate/service/service-module.ts b/tests/e2e/tests/generate/service/service-module.ts index e13ea17f04e1..27723e9b6e69 100644 --- a/tests/e2e/tests/generate/service/service-module.ts +++ b/tests/e2e/tests/generate/service/service-module.ts @@ -1,15 +1,24 @@ +import * as fs from 'fs-extra'; 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'); + const root = process.cwd(); + const modulePath = join(root, 'src', 'app', 'app.module.ts'); + + fs.mkdirSync('./src/app/sub-dir'); return ng('generate', 'service', 'test-service', '--module', 'app.module.ts') .then(() => expectFileToMatch(modulePath, /import { TestServiceService } from '.\/test-service.service'/)) + .then(() => process.chdir(join(root, 'src', 'app'))) + .then(() => ng('generate', 'service', 'test-service2', '--module', 'app.module.ts')) + .then(() => expectFileToMatch(modulePath, + /import { TestService2Service } from '.\/test-service2.service'/)) + // Try to run the unit tests. .then(() => ng('build')); } From 0b0931b7dfba8d02d8621f0d83d331b24acf3f0a Mon Sep 17 00:00:00 2001 From: Andrew Wong Date: Tue, 8 Aug 2017 01:22:45 -0400 Subject: [PATCH 3/3] test(@angular/cli): Add more tests for running 'generate' in a subdirectory (#7135) --- tests/e2e/tests/generate/component/component-module.ts | 5 +++++ tests/e2e/tests/generate/directive/directive-module.ts | 5 +++++ tests/e2e/tests/generate/guard/guard-module.ts | 5 +++++ tests/e2e/tests/generate/module/module-import.ts | 6 ++++++ tests/e2e/tests/generate/pipe/pipe-module.ts | 5 +++++ tests/e2e/tests/generate/service/service-module.ts | 5 +++++ 6 files changed, 31 insertions(+) diff --git a/tests/e2e/tests/generate/component/component-module.ts b/tests/e2e/tests/generate/component/component-module.ts index e742a1ecabeb..92064359d785 100644 --- a/tests/e2e/tests/generate/component/component-module.ts +++ b/tests/e2e/tests/generate/component/component-module.ts @@ -19,6 +19,11 @@ export default function() { .then(() => expectFileToMatch(modulePath, /import { TestComponent2Component } from '.\/test-component2\/test-component2.component'/)) + .then(() => process.chdir(join(root, 'src', 'app', 'sub-dir'))) + .then(() => ng('generate', 'component', 'test-component3', '--module', 'app.module.ts')) + .then(() => expectFileToMatch(modulePath, + /import { TestComponent3Component } from '.\/test-component3\/test-component3.component'/)) + // Try to run the unit tests. .then(() => ng('build')); } diff --git a/tests/e2e/tests/generate/directive/directive-module.ts b/tests/e2e/tests/generate/directive/directive-module.ts index 2667c390c910..62ad00f373ef 100644 --- a/tests/e2e/tests/generate/directive/directive-module.ts +++ b/tests/e2e/tests/generate/directive/directive-module.ts @@ -19,6 +19,11 @@ export default function() { .then(() => expectFileToMatch(modulePath, /import { TestDirective2Directive } from '.\/test-directive2.directive'/)) + .then(() => process.chdir(join(root, 'src', 'app', 'sub-dir'))) + .then(() => ng('generate', 'directive', 'test-directive3', '--module', 'app.module.ts')) + .then(() => expectFileToMatch(modulePath, + /import { TestDirective3Directive } from '.\/test-directive3.directive'/)) + // Try to run the unit tests. .then(() => ng('build')); } diff --git a/tests/e2e/tests/generate/guard/guard-module.ts b/tests/e2e/tests/generate/guard/guard-module.ts index 8718fdd700ae..d1f96d247255 100644 --- a/tests/e2e/tests/generate/guard/guard-module.ts +++ b/tests/e2e/tests/generate/guard/guard-module.ts @@ -21,5 +21,10 @@ export default function() { .then(() => expectFileToMatch(modulePath, /import { TestGuard2Guard } from '.\/test-guard2.guard'/)) + .then(() => process.chdir(join(root, 'src', 'app', 'sub-dir'))) + .then(() => ng('generate', 'guard', 'test-guard3', '--module', 'app.module.ts')) + .then(() => expectFileToMatch(modulePath, + /import { TestGuard3Guard } from '.\/test-guard3.guard'/)) + .then(() => ng('build')); } diff --git a/tests/e2e/tests/generate/module/module-import.ts b/tests/e2e/tests/generate/module/module-import.ts index 48515249c20b..f103d1e0be0a 100644 --- a/tests/e2e/tests/generate/module/module-import.ts +++ b/tests/e2e/tests/generate/module/module-import.ts @@ -50,4 +50,10 @@ export default function () { .then(() => expectFileToMatch(modulePath, /import { Test7Module } from '.\/test7\/test7.module'/)) .then(() => expectFileToMatch(modulePath, /imports: \[(.|\s)*Test7Module(.|\s)*\]/m)) + + .then(() => process.chdir(join(root, 'src', 'app', 'sub-dir'))) + .then(() => ng('generate', 'module', 'test8', '--module', 'app.module.ts'))) + .then(() => expectFileToMatch(modulePath, + /import { Test8Module } from '.\/test8\/test8.module'/)) + .then(() => expectFileToMatch(modulePath, /imports: \[(.|\s)*Test8Module(.|\s)*\]/m)) } diff --git a/tests/e2e/tests/generate/pipe/pipe-module.ts b/tests/e2e/tests/generate/pipe/pipe-module.ts index c77d2d056943..ecfed407ad8a 100644 --- a/tests/e2e/tests/generate/pipe/pipe-module.ts +++ b/tests/e2e/tests/generate/pipe/pipe-module.ts @@ -19,6 +19,11 @@ export default function() { .then(() => expectFileToMatch(modulePath, /import { TestPipe2Pipe } from '.\/test-pipe2.pipe'/)) + .then(() => process.chdir(join(root, 'src', 'app', 'sub-dir'))) + .then(() => ng('generate', 'pipe', 'test-pipe3', '--module', 'app.module.ts')) + .then(() => expectFileToMatch(modulePath, + /import { TestPipe3Pipe } from '.\/test-pipe3.pipe'/)) + // Try to run the unit tests. .then(() => ng('build')); } diff --git a/tests/e2e/tests/generate/service/service-module.ts b/tests/e2e/tests/generate/service/service-module.ts index 27723e9b6e69..cc13e337ed23 100644 --- a/tests/e2e/tests/generate/service/service-module.ts +++ b/tests/e2e/tests/generate/service/service-module.ts @@ -19,6 +19,11 @@ export default function() { .then(() => expectFileToMatch(modulePath, /import { TestService2Service } from '.\/test-service2.service'/)) + .then(() => process.chdir(join(root, 'src', 'app', 'sub-dir'))) + .then(() => ng('generate', 'service', 'test-service3', '--module', 'app.module.ts')) + .then(() => expectFileToMatch(modulePath, + /import { TestService3Service } from '.\/test-service3.service'/)) + // Try to run the unit tests. .then(() => ng('build')); }