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';
|
import { newApp, newLib, newProject, runCLI, updateFile } from '../utils';
|
||||||
|
|
||||||
describe('Nrwl Workspace', () => {
|
describe('Nrwl Workspace', () => {
|
||||||
fit(
|
it(
|
||||||
'should work',
|
'should work',
|
||||||
() => {
|
() => {
|
||||||
newProject();
|
newProject();
|
||||||
|
|||||||
@ -137,6 +137,22 @@ describe('Calculates Dependencies Between Apps and Libs', () => {
|
|||||||
|
|
||||||
expect(deps).toEqual({ app1: ['app1', 'lib1', 'lib2'], lib1: ['lib1'], lib2: ['lib2'] });
|
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', () => {
|
describe('affectedApps', () => {
|
||||||
@ -222,5 +238,29 @@ describe('Calculates Dependencies Between Apps and Libs', () => {
|
|||||||
|
|
||||||
expect(affected).toEqual(['app1', 'app2']);
|
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 ts from 'typescript';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
export type Project = { name: string; isApp: boolean; files: string[] };
|
export type Project = { name: string; isApp: boolean; files: string[] };
|
||||||
|
|
||||||
@ -8,6 +9,8 @@ export function affectedApps(
|
|||||||
fileRead: (s: string) => string,
|
fileRead: (s: string) => string,
|
||||||
touchedFiles: string[]
|
touchedFiles: string[]
|
||||||
): string[] {
|
): string[] {
|
||||||
|
projects = normalizeProjects(projects);
|
||||||
|
touchedFiles = normalizeFiles(touchedFiles);
|
||||||
const deps = dependencies(npmScope, projects, fileRead);
|
const deps = dependencies(npmScope, projects, fileRead);
|
||||||
|
|
||||||
const touchedProjects = touchedFiles.map(f => {
|
const touchedProjects = touchedFiles.map(f => {
|
||||||
@ -24,6 +27,19 @@ export function affectedApps(
|
|||||||
.map(p => p.name);
|
.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(
|
export function dependencies(
|
||||||
npmScope: string,
|
npmScope: string,
|
||||||
projects: Project[],
|
projects: Project[],
|
||||||
@ -70,9 +86,11 @@ class Deps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private processFile(projectName: string, filePath: string): void {
|
private processFile(projectName: string, filePath: string): void {
|
||||||
|
if (path.extname(filePath) === '.ts') {
|
||||||
const tsFile = ts.createSourceFile(filePath, this.fileRead(filePath), ts.ScriptTarget.Latest, true);
|
const tsFile = ts.createSourceFile(filePath, this.fileRead(filePath), ts.ScriptTarget.Latest, true);
|
||||||
this.processNode(projectName, tsFile);
|
this.processNode(projectName, tsFile);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private processNode(projectName: string, node: ts.Node): void {
|
private processNode(projectName: string, node: ts.Node): void {
|
||||||
if (node.kind === ts.SyntaxKind.ImportDeclaration) {
|
if (node.kind === ts.SyntaxKind.ImportDeclaration) {
|
||||||
|
|||||||
@ -80,7 +80,7 @@ function allFilesInDir(dirName: string): string[] {
|
|||||||
fs.readdirSync(dirName).forEach(c => {
|
fs.readdirSync(dirName).forEach(c => {
|
||||||
const child = path.join(dirName, c);
|
const child = path.join(dirName, c);
|
||||||
try {
|
try {
|
||||||
if (!fs.statSync(child).isDirectory() && path.extname(child) === '.ts') {
|
if (!fs.statSync(child).isDirectory()) {
|
||||||
res.push(child);
|
res.push(child);
|
||||||
} else if (fs.statSync(child).isDirectory()) {
|
} else if (fs.statSync(child).isDirectory()) {
|
||||||
res = [...res, ...allFilesInDir(child)];
|
res = [...res, ...allFilesInDir(child)];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user