Skip to content

Commit 4b8f613

Browse files
committed
feat: add features in get-dependent-files.ts
add logic to filter out spec file associated with the given component unit and all index files while getting all dependent files add utlity function to get all the files (.html/stylesheets/.spec.ts) associated with the given component unit change the constructor method of the class ModuleResolver (add 'rootPath' as an additional parameter)
1 parent 36c0ee5 commit 4b8f613

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

addon/ng2/utilities/get-dependent-files.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ export function hasIndexFile(dirPath: string): Promise<Boolean> {
7474
});
7575
}
7676

77+
/**
78+
* Function to get all the templates, stylesheets, and spec files of a given component unit
79+
*
80+
* @param fileName
81+
*
82+
* @return absolute paths of '.html/.css/.sass/.spec.ts' files associated with the given file.
83+
*
84+
*/
85+
export function getAllCorrespondingFiles(fileName: string): Promise<string[]> {
86+
let fileDirName = path.dirname(fileName);
87+
let componentName = path.basename(fileName).split('.')[0];
88+
const globSearch = denodeify(glob);
89+
return globSearch(path.join(fileDirName, `${componentName}.*`), { nodir: true })
90+
.then((files: string[]) => {
91+
return files.filter((file) => {
92+
return (path.basename(file) !== 'index.ts');
93+
});
94+
});
95+
}
96+
7797
/**
7898
* Returns a map of all dependent file/s' path with their moduleSpecifier object
7999
* (specifierText, pos, end)
@@ -86,7 +106,7 @@ export function hasIndexFile(dirPath: string): Promise<Boolean> {
86106
*/
87107
export function getDependentFiles(fileName: string, rootPath: string): Promise<ModuleMap> {
88108
const globSearch = denodeify(glob);
89-
return globSearch(path.join(rootPath, '**/*.*.ts'), { nodir: true })
109+
return globSearch(path.join(rootPath, '**/*.ts'), { nodir: true })
90110
.then((files: string[]) => Promise.all(files.map(file => createTsSourceFile(file)))
91111
.then((tsFiles: ts.SourceFile[]) => tsFiles.map(file => getImportClauses(file)))
92112
.then((moduleSpecifiers: ModuleImport[][]) => {

addon/ng2/utilities/module-resolver.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ import * as dependentFilesUtils from './get-dependent-files';
77
import { Promise } from 'es6-promise';
88
import { Change, ReplaceChange } from './change';
99

10-
// The root directory of Angular Project.
11-
const ROOT_PATH = path.resolve('src/app');
12-
1310
/**
1411
* Rewrites import module of dependent files when the file is moved.
1512
* Also, rewrites export module of related index file of the given file.
1613
*/
1714
export class ModuleResolver {
1815

19-
constructor(public oldFilePath: string, public newFilePath: string) {}
16+
constructor(public oldFilePath: string, public newFilePath: string, public rootPath: string) {}
2017

2118
/**
2219
* Changes are applied from the bottom of a file to the top.
@@ -54,10 +51,19 @@ export class ModuleResolver {
5451
* @return {Promise<Change[]>}
5552
*/
5653
resolveDependentFiles(): Promise<Change[]> {
57-
return dependentFilesUtils.getDependentFiles(this.oldFilePath, ROOT_PATH)
54+
return dependentFilesUtils.getDependentFiles(this.oldFilePath, this.rootPath)
5855
.then((files: dependentFilesUtils.ModuleMap) => {
5956
let changes: Change[] = [];
60-
Object.keys(files).forEach(file => {
57+
let fileBaseName = path.basename(this.oldFilePath, '.ts');
58+
// Filter out the spec file associated with to-be-promoted component unit.
59+
let relavantFiles = Object.keys(files).filter((file) => {
60+
if (path.extname(path.basename(file, '.ts')) === '.spec') {
61+
return path.basename(path.basename(file, '.ts'), '.spec') !== fileBaseName;
62+
} else {
63+
return true;
64+
}
65+
});
66+
relavantFiles.forEach(file => {
6167
let tempChanges: ReplaceChange[] = files[file]
6268
.map(specifier => {
6369
let componentName = path.basename(this.oldFilePath, '.ts');

tests/acceptance/get-dependent-files.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ describe('Get Dependent Files: ', () => {
2424
'baz.html': '<h1> Hello </h1>'
2525
},
2626
'bar.component.ts': `import * from './baz/baz.component'
27-
import * from '../foo'`
27+
import * from '../foo'`,
28+
'bar.component.spec.ts': ''
2829
},
2930
'foo-baz': {
30-
'no-module.component.ts': ''
31+
'no-module.component.ts': '',
32+
'no-module.component.spec.ts': 'import * from "../bar/bar.component";'
3133
},
3234
'empty-dir': {}
3335
}
@@ -109,6 +111,7 @@ describe('Get Dependent Files: ', () => {
109111
.then((contents: dependentFilesUtils.ModuleMap) => {
110112
let bazFile = path.join(rootPath, 'bar/baz/baz.component.ts');
111113
let fooFile = path.join(rootPath, 'foo/foo.component.ts');
114+
let noModuleSpecFile = path.join(rootPath, 'foo-baz/no-module.component.spec.ts');
112115
let expectedContents: dependentFilesUtils.ModuleMap = {};
113116
expectedContents[bazFile] = [{
114117
specifierText: '../bar.component',
@@ -120,6 +123,11 @@ describe('Get Dependent Files: ', () => {
120123
pos: 85,
121124
end: 108
122125
}];
126+
expectedContents[noModuleSpecFile] = [{
127+
specifierText: '../bar/bar.component',
128+
pos: 13,
129+
end: 36
130+
}];
123131
assert.deepEqual(contents, expectedContents);
124132
});
125133
});

tests/acceptance/module-resolver.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ describe('ModuleResolver', () => {
2222
'baz': {
2323
'baz.component.ts': `import * from "../bar.component"
2424
import * from '../../foo-baz/qux/quux/foobar/foobar.component'
25-
`
25+
`,
26+
'baz.component.spec.ts': 'import * from "./baz.component";'
2627
},
2728
'bar.component.ts': `import * from './baz/baz.component'
2829
import * from '../foo/foo.component'`,
@@ -64,7 +65,7 @@ describe('ModuleResolver', () => {
6465
it('when there is no index.ts in oldPath', () => {
6566
let oldFilePath = path.join(rootPath, 'bar/baz/baz.component.ts');
6667
let newFilePath = path.join(rootPath, 'foo');
67-
let resolver = new ModuleResolver(oldFilePath, newFilePath);
68+
let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath);
6869
return resolver.resolveDependentFiles()
6970
.then((changes) => resolver.applySortedChangePromise(changes))
7071
.then(() => dependentFilesUtils.createTsSourceFile(barFile))
@@ -93,7 +94,7 @@ describe('ModuleResolver', () => {
9394
it('when no files are importing the given file', () => {
9495
let oldFilePath = path.join(rootPath, 'foo-baz/foo-baz.component.ts');
9596
let newFilePath = path.join(rootPath, 'bar');
96-
let resolver = new ModuleResolver(oldFilePath, newFilePath);
97+
let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath);
9798
return resolver.resolveDependentFiles()
9899
.then((changes) => resolver.applySortedChangePromise(changes))
99100
.then(() => resolver.resolveOwnImports())
@@ -108,7 +109,7 @@ describe('ModuleResolver', () => {
108109
it('when oldPath and newPath both do not have index.ts', () => {
109110
let oldFilePath = path.join(rootPath, 'bar/baz/baz.component.ts');
110111
let newFilePath = path.join(rootPath, 'foo-baz');
111-
let resolver = new ModuleResolver(oldFilePath, newFilePath);
112+
let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath);
112113
return resolver.resolveDependentFiles()
113114
.then((changes) => resolver.applySortedChangePromise(changes))
114115
.then(() => dependentFilesUtils.createTsSourceFile(barFile))
@@ -137,7 +138,7 @@ describe('ModuleResolver', () => {
137138
it('when there are multiple spaces between symbols and specifier', () => {
138139
let oldFilePath = path.join(rootPath, 'foo-baz/qux/quux/foobar/foobar.component.ts');
139140
let newFilePath = path.join(rootPath, 'foo');
140-
let resolver = new ModuleResolver(oldFilePath, newFilePath);
141+
let resolver = new ModuleResolver(oldFilePath, newFilePath, rootPath);
141142
return resolver.resolveDependentFiles()
142143
.then((changes) => resolver.applySortedChangePromise(changes))
143144
.then(() => dependentFilesUtils.createTsSourceFile(fooQuxFile))

0 commit comments

Comments
 (0)