Skip to content

Commit

Permalink
fix(nx): try to find tsconfig.app.json then fallback (#14)
Browse files Browse the repository at this point in the history
* fix(nx): try to find tsconfig.app.json then fallback

* fix(core): ignore root if statements traversal
  • Loading branch information
EladBezalel authored Jul 23, 2023
1 parent 68bccb3 commit 2908146
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 51 deletions.
1 change: 1 addition & 0 deletions libs/core/src/true-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const ignoredRootNodeTypes = [
SyntaxKind.ExportDeclaration,
SyntaxKind.ModuleDeclaration,
SyntaxKind.ExpressionStatement, // iife,
SyntaxKind.IfStatement,
];

export const findRootNode = (
Expand Down
174 changes: 124 additions & 50 deletions libs/nx/src/nx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,64 +160,138 @@ describe('nx', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
});

it('should return tsconfig.lib.json if projectType is library', async () => {
jest.spyOn(fs.promises, 'readFile').mockImplementation((pathLike) => {
const path = pathLike.toString();

if (path.endsWith('proj1/project.json')) {
return Promise.resolve(
JSON.stringify({
describe('projectType is library', () => {
it('should return tsconfig.lib.json', async () => {
jest
.spyOn(fs.promises, 'readFile')
.mockImplementation((pathLike) => {
const path = pathLike.toString();

if (path.endsWith('proj1/project.json')) {
return Promise.resolve(
JSON.stringify({
name: 'proj1',
sourceRoot: 'proj1/src',
projectType: 'library',
})
);
}

return Promise.reject('File not found');
});

const cwd = 'libs/nx/src/__fixtures__/nx-project';
const projects = await getNxTrueAffectedProjects(cwd);

expect(projects).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'proj1',
sourceRoot: 'proj1/src',
projectType: 'library',
})
);
}

return Promise.reject('File not found');
tsConfig: expect.stringContaining('proj1/tsconfig.lib.json'),
}),
])
);
});

const cwd = 'libs/nx/src/__fixtures__/nx-project';
const projects = await getNxTrueAffectedProjects(cwd);

expect(projects).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'proj1',
tsConfig: expect.stringContaining('proj1/tsconfig.lib.json'),
}),
])
);
it('should return tsconfig.json if tsconfig.lib.json was not found', async () => {
jest.spyOn(fs, 'existsSync').mockReturnValueOnce(false);
jest
.spyOn(fs.promises, 'readFile')
.mockImplementation((pathLike) => {
const path = pathLike.toString();

if (path.endsWith('proj1/project.json')) {
return Promise.resolve(
JSON.stringify({
name: 'proj1',
sourceRoot: 'proj1/src',
projectType: 'library',
})
);
}

return Promise.reject('File not found');
});

const cwd = 'libs/nx/src/__fixtures__/nx-project';
const projects = await getNxTrueAffectedProjects(cwd);

expect(projects).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'proj1',
tsConfig: expect.stringContaining('proj1/tsconfig.json'),
}),
])
);
});
});

it('should return tsconfig.json if projectType is application', async () => {
jest.spyOn(fs.promises, 'readFile').mockImplementation((pathLike) => {
const path = pathLike.toString();

if (path.endsWith('proj1/project.json')) {
return Promise.resolve(
JSON.stringify({
describe('projectType is application', () => {
it('should return tsconfig.app.json if projectType is application', async () => {
jest
.spyOn(fs.promises, 'readFile')
.mockImplementation((pathLike) => {
const path = pathLike.toString();

if (path.endsWith('proj1/project.json')) {
return Promise.resolve(
JSON.stringify({
name: 'proj1',
sourceRoot: 'proj1/src',
projectType: 'application',
})
);
}

return Promise.reject('File not found');
});

const cwd = 'libs/nx/src/__fixtures__/nx-project';
const projects = await getNxTrueAffectedProjects(cwd);

expect(projects).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'proj1',
sourceRoot: 'proj1/src',
projectType: 'application',
})
);
}

return Promise.reject('File not found');
tsConfig: expect.stringContaining('proj1/tsconfig.app.json'),
}),
])
);
});

const cwd = 'libs/nx/src/__fixtures__/nx-project';
const projects = await getNxTrueAffectedProjects(cwd);

expect(projects).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'proj1',
tsConfig: expect.stringContaining('proj1/tsconfig.json'),
}),
])
);
it('should return tsconfig.json if tsconfig.app.json was not found', async () => {
jest.spyOn(fs, 'existsSync').mockReturnValueOnce(false);
jest
.spyOn(fs.promises, 'readFile')
.mockImplementation((pathLike) => {
const path = pathLike.toString();

if (path.endsWith('proj1/project.json')) {
return Promise.resolve(
JSON.stringify({
name: 'proj1',
sourceRoot: 'proj1/src',
projectType: 'application',
})
);
}

return Promise.reject('File not found');
});

const cwd = 'libs/nx/src/__fixtures__/nx-project';
const projects = await getNxTrueAffectedProjects(cwd);

expect(projects).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'proj1',
tsConfig: expect.stringContaining('proj1/tsconfig.json'),
}),
])
);
});
});
});
});
Expand Down
5 changes: 4 additions & 1 deletion libs/nx/src/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ export async function getNxTrueAffectedProjects(

if (project.projectType === 'library') {
tsConfig = join(projectRoot, 'tsconfig.lib.json');
} else if (!tsConfig || !existsSync(resolve(cwd, tsConfig))) {
tsConfig = join(projectRoot, 'tsconfig.app.json');
}
if (!tsConfig || !existsSync(resolve(cwd, tsConfig))) {

if (!existsSync(resolve(cwd, tsConfig))) {
tsConfig = join(projectRoot, 'tsconfig.json');
}
}
Expand Down

0 comments on commit 2908146

Please sign in to comment.