diff --git a/addon/ng2/utilities/get-dependent-files.ts b/addon/ng2/utilities/get-dependent-files.ts index 6f38efd4dfcf..3ec30d607ca1 100644 --- a/addon/ng2/utilities/get-dependent-files.ts +++ b/addon/ng2/utilities/get-dependent-files.ts @@ -74,6 +74,26 @@ export function hasIndexFile(dirPath: string): Promise { }); } +/** + * Function to get all the templates, stylesheets, and spec files of a given component unit + * + * @param fileName + * + * @return absolute paths of '.html/.css/.sass/.spec.ts' files associated with the given file. + * + */ +export function getAllCorrespondingFiles(fileName: string): Promise { + let fileDirName = path.dirname(fileName); + let componentName = path.basename(fileName).split('.')[0]; + const globSearch = denodeify(glob); + return globSearch(path.join(fileDirName, `${componentName}.*`), { nodir: true }) + .then((files: string[]) => { + return files.filter((file) => { + return (path.basename(file) !== 'index.ts'); + }); + }); +} + /** * Returns a map of all dependent file/s' path with their moduleSpecifier object * (specifierText, pos, end) @@ -86,7 +106,7 @@ export function hasIndexFile(dirPath: string): Promise { */ export function getDependentFiles(fileName: string, rootPath: string): Promise { const globSearch = denodeify(glob); - return globSearch(path.join(rootPath, '**/*.*.ts'), { nodir: true }) + return globSearch(path.join(rootPath, '**/*.ts'), { nodir: true }) .then((files: string[]) => Promise.all(files.map(file => createTsSourceFile(file))) .then((tsFiles: ts.SourceFile[]) => tsFiles.map(file => getImportClauses(file))) .then((moduleSpecifiers: ModuleImport[][]) => { diff --git a/addon/ng2/utilities/module-resolver.ts b/addon/ng2/utilities/module-resolver.ts index b20820cfb38d..d02c97d54404 100644 --- a/addon/ng2/utilities/module-resolver.ts +++ b/addon/ng2/utilities/module-resolver.ts @@ -7,16 +7,13 @@ import * as dependentFilesUtils from './get-dependent-files'; import { Promise } from 'es6-promise'; import { Change, ReplaceChange } from './change'; -// The root directory of Angular Project. -const ROOT_PATH = path.resolve('src/app'); - /** * Rewrites import module of dependent files when the file is moved. * Also, rewrites export module of related index file of the given file. */ export class ModuleResolver { - constructor(public oldFilePath: string, public newFilePath: string) {} + constructor(public oldFilePath: string, public newFilePath: string, public rootPath: string) {} /** * Changes are applied from the bottom of a file to the top. @@ -54,10 +51,19 @@ export class ModuleResolver { * @return {Promise} */ resolveDependentFiles(): Promise { - return dependentFilesUtils.getDependentFiles(this.oldFilePath, ROOT_PATH) + return dependentFilesUtils.getDependentFiles(this.oldFilePath, this.rootPath) .then((files: dependentFilesUtils.ModuleMap) => { let changes: Change[] = []; - Object.keys(files).forEach(file => { + let fileBaseName = path.basename(this.oldFilePath, '.ts'); + // Filter out the spec file associated with to-be-promoted component unit. + let relavantFiles = Object.keys(files).filter((file) => { + if (path.extname(path.basename(file, '.ts')) === '.spec') { + return path.basename(path.basename(file, '.ts'), '.spec') !== fileBaseName; + } else { + return true; + } + }); + relavantFiles.forEach(file => { let tempChanges: ReplaceChange[] = files[file] .map(specifier => { let componentName = path.basename(this.oldFilePath, '.ts'); diff --git a/tests/acceptance/get-dependent-files.spec.ts b/tests/acceptance/get-dependent-files.spec.ts index ff36244a0971..220133e93bb2 100644 --- a/tests/acceptance/get-dependent-files.spec.ts +++ b/tests/acceptance/get-dependent-files.spec.ts @@ -24,10 +24,12 @@ describe('Get Dependent Files: ', () => { 'baz.html': '

Hello

' }, 'bar.component.ts': `import * from './baz/baz.component' - import * from '../foo'` + import * from '../foo'`, + 'bar.component.spec.ts': '' }, 'foo-baz': { - 'no-module.component.ts': '' + 'no-module.component.ts': '', + 'no-module.component.spec.ts': 'import * from "../bar/bar.component";' }, 'empty-dir': {} } @@ -109,6 +111,7 @@ describe('Get Dependent Files: ', () => { .then((contents: dependentFilesUtils.ModuleMap) => { let bazFile = path.join(rootPath, 'bar/baz/baz.component.ts'); let fooFile = path.join(rootPath, 'foo/foo.component.ts'); + let noModuleSpecFile = path.join(rootPath, 'foo-baz/no-module.component.spec.ts'); let expectedContents: dependentFilesUtils.ModuleMap = {}; expectedContents[bazFile] = [{ specifierText: '../bar.component', @@ -120,6 +123,11 @@ describe('Get Dependent Files: ', () => { pos: 85, end: 108 }]; + expectedContents[noModuleSpecFile] = [{ + specifierText: '../bar/bar.component', + pos: 13, + end: 36 + }]; assert.deepEqual(contents, expectedContents); }); }); diff --git a/tests/acceptance/module-resolver.spec.ts b/tests/acceptance/module-resolver.spec.ts index bb154354adfe..cff22f2a2787 100644 --- a/tests/acceptance/module-resolver.spec.ts +++ b/tests/acceptance/module-resolver.spec.ts @@ -22,7 +22,8 @@ describe('ModuleResolver', () => { 'baz': { 'baz.component.ts': `import * from "../bar.component" import * from '../../foo-baz/qux/quux/foobar/foobar.component' - ` + `, + 'baz.component.spec.ts': 'import * from "./baz.component";' }, 'bar.component.ts': `import * from './baz/baz.component' import * from '../foo/foo.component'`, @@ -64,7 +65,7 @@ describe('ModuleResolver', () => { it('when there is no index.ts in oldPath', () => { let oldFilePath = path.join(rootPath, 'bar/baz/baz.component.ts'); let newFilePath = path.join(rootPath, 'foo'); - let resolver = new ModuleResolver(oldFilePath, newFilePath); + let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath); return resolver.resolveDependentFiles() .then((changes) => resolver.applySortedChangePromise(changes)) .then(() => dependentFilesUtils.createTsSourceFile(barFile)) @@ -93,7 +94,7 @@ describe('ModuleResolver', () => { it('when no files are importing the given file', () => { let oldFilePath = path.join(rootPath, 'foo-baz/foo-baz.component.ts'); let newFilePath = path.join(rootPath, 'bar'); - let resolver = new ModuleResolver(oldFilePath, newFilePath); + let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath); return resolver.resolveDependentFiles() .then((changes) => resolver.applySortedChangePromise(changes)) .then(() => resolver.resolveOwnImports()) @@ -108,7 +109,7 @@ describe('ModuleResolver', () => { it('when oldPath and newPath both do not have index.ts', () => { let oldFilePath = path.join(rootPath, 'bar/baz/baz.component.ts'); let newFilePath = path.join(rootPath, 'foo-baz'); - let resolver = new ModuleResolver(oldFilePath, newFilePath); + let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath); return resolver.resolveDependentFiles() .then((changes) => resolver.applySortedChangePromise(changes)) .then(() => dependentFilesUtils.createTsSourceFile(barFile)) @@ -137,7 +138,7 @@ describe('ModuleResolver', () => { it('when there are multiple spaces between symbols and specifier', () => { let oldFilePath = path.join(rootPath, 'foo-baz/qux/quux/foobar/foobar.component.ts'); let newFilePath = path.join(rootPath, 'foo'); - let resolver = new ModuleResolver(oldFilePath, newFilePath); + let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath); return resolver.resolveDependentFiles() .then((changes) => resolver.applySortedChangePromise(changes)) .then(() => dependentFilesUtils.createTsSourceFile(fooQuxFile))