fix(schematics): fix apps:affected to handle non-ts files
This commit is contained in:
parent
3c55f34ca1
commit
e3feb8ac5e
@ -1,7 +1,7 @@
|
||||
import { newApp, newLib, newProject, runCLI, updateFile } from '../utils';
|
||||
|
||||
describe('Nrwl Workspace', () => {
|
||||
fit(
|
||||
it(
|
||||
'should work',
|
||||
() => {
|
||||
newProject();
|
||||
|
||||
@ -137,6 +137,22 @@ describe('Calculates Dependencies Between Apps and Libs', () => {
|
||||
|
||||
expect(deps).toEqual({ app1: ['app1', 'lib1', 'lib2'], lib1: ['lib1'], lib2: ['lib2'] });
|
||||
});
|
||||
|
||||
it('should handle non-ts files', () => {
|
||||
const deps = dependencies(
|
||||
'nrwl',
|
||||
[
|
||||
{
|
||||
name: 'app1',
|
||||
files: ['index.html'],
|
||||
isApp: true
|
||||
}
|
||||
],
|
||||
() => null
|
||||
);
|
||||
|
||||
expect(deps).toEqual({ app1: ['app1'] });
|
||||
});
|
||||
});
|
||||
|
||||
describe('affectedApps', () => {
|
||||
@ -222,5 +238,29 @@ describe('Calculates Dependencies Between Apps and Libs', () => {
|
||||
|
||||
expect(affected).toEqual(['app1', 'app2']);
|
||||
});
|
||||
|
||||
it('should handle slashes', () => {
|
||||
const affected = affectedApps(
|
||||
'nrwl',
|
||||
[
|
||||
{
|
||||
name: 'app1',
|
||||
files: ['one\\app1.ts', 'two/app1.ts'],
|
||||
isApp: true
|
||||
}
|
||||
],
|
||||
file => {
|
||||
switch (file) {
|
||||
case 'one/app1.ts':
|
||||
return '';
|
||||
case 'two/app1.ts':
|
||||
return '';
|
||||
}
|
||||
},
|
||||
['one/app1.ts', 'two/app1.ts']
|
||||
);
|
||||
|
||||
expect(affected).toEqual(['app1']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import * as ts from 'typescript';
|
||||
import * as path from 'path';
|
||||
|
||||
export type Project = { name: string; isApp: boolean; files: string[] };
|
||||
|
||||
@ -8,6 +9,8 @@ export function affectedApps(
|
||||
fileRead: (s: string) => string,
|
||||
touchedFiles: string[]
|
||||
): string[] {
|
||||
projects = normalizeProjects(projects);
|
||||
touchedFiles = normalizeFiles(touchedFiles);
|
||||
const deps = dependencies(npmScope, projects, fileRead);
|
||||
|
||||
const touchedProjects = touchedFiles.map(f => {
|
||||
@ -24,6 +27,19 @@ export function affectedApps(
|
||||
.map(p => p.name);
|
||||
}
|
||||
|
||||
function normalizeProjects(projects: Project[]) {
|
||||
return projects.map(p => {
|
||||
return {
|
||||
...p,
|
||||
files: normalizeFiles(p.files)
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeFiles(files: string[]): string[] {
|
||||
return files.map(f => f.replace(/[\\\/]+/g, '/'));
|
||||
}
|
||||
|
||||
export function dependencies(
|
||||
npmScope: string,
|
||||
projects: Project[],
|
||||
@ -70,8 +86,10 @@ class Deps {
|
||||
}
|
||||
|
||||
private processFile(projectName: string, filePath: string): void {
|
||||
const tsFile = ts.createSourceFile(filePath, this.fileRead(filePath), ts.ScriptTarget.Latest, true);
|
||||
this.processNode(projectName, tsFile);
|
||||
if (path.extname(filePath) === '.ts') {
|
||||
const tsFile = ts.createSourceFile(filePath, this.fileRead(filePath), ts.ScriptTarget.Latest, true);
|
||||
this.processNode(projectName, tsFile);
|
||||
}
|
||||
}
|
||||
|
||||
private processNode(projectName: string, node: ts.Node): void {
|
||||
|
||||
@ -80,7 +80,7 @@ function allFilesInDir(dirName: string): string[] {
|
||||
fs.readdirSync(dirName).forEach(c => {
|
||||
const child = path.join(dirName, c);
|
||||
try {
|
||||
if (!fs.statSync(child).isDirectory() && path.extname(child) === '.ts') {
|
||||
if (!fs.statSync(child).isDirectory()) {
|
||||
res.push(child);
|
||||
} else if (fs.statSync(child).isDirectory()) {
|
||||
res = [...res, ...allFilesInDir(child)];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user