From 2908146b9fc6479ee2a55dfac1d7e8a2edd7d615 Mon Sep 17 00:00:00 2001 From: Elad Bezalel Date: Sun, 23 Jul 2023 14:11:31 +0300 Subject: [PATCH] fix(nx): try to find tsconfig.app.json then fallback (#14) * fix(nx): try to find tsconfig.app.json then fallback * fix(core): ignore root if statements traversal --- libs/core/src/true-affected.ts | 1 + libs/nx/src/nx.spec.ts | 174 +++++++++++++++++++++++---------- libs/nx/src/nx.ts | 5 +- 3 files changed, 129 insertions(+), 51 deletions(-) diff --git a/libs/core/src/true-affected.ts b/libs/core/src/true-affected.ts index e6882a4..acc306f 100644 --- a/libs/core/src/true-affected.ts +++ b/libs/core/src/true-affected.ts @@ -23,6 +23,7 @@ const ignoredRootNodeTypes = [ SyntaxKind.ExportDeclaration, SyntaxKind.ModuleDeclaration, SyntaxKind.ExpressionStatement, // iife, + SyntaxKind.IfStatement, ]; export const findRootNode = ( diff --git a/libs/nx/src/nx.spec.ts b/libs/nx/src/nx.spec.ts index 5eb4ed3..d95ed98 100644 --- a/libs/nx/src/nx.spec.ts +++ b/libs/nx/src/nx.spec.ts @@ -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'), + }), + ]) + ); + }); }); }); }); diff --git a/libs/nx/src/nx.ts b/libs/nx/src/nx.ts index fbff8e2..12a9001 100644 --- a/libs/nx/src/nx.ts +++ b/libs/nx/src/nx.ts @@ -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'); } }