-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-commands): added nx commands for affected, projects with task…
…, project info and etc
- Loading branch information
1 parent
167761f
commit dcb8191
Showing
7 changed files
with
140 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './lib/nx'; | ||
export * from './lib/nx.command'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
jest.mock('node:child_process'); | ||
jest.mock('node:console', () => { | ||
return { | ||
error: jest.fn().mockImplementation(), | ||
info: jest.fn().mockImplementation(), | ||
log: jest.fn().mockImplementation(), | ||
warn: jest.fn().mockImplementation() | ||
}; | ||
}); | ||
|
||
import * as cp from 'node:child_process'; | ||
|
||
import { NxCommands } from './nx.command'; | ||
|
||
describe('Nx Commands ---', () => { | ||
let execSyncSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
execSyncSpy = jest.spyOn(cp, 'execSync'); | ||
expect(execSyncSpy).toBeDefined(); | ||
execSyncSpy.mockReturnValue('TEST'); | ||
expect(execSyncSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
afterEach(() => { | ||
execSyncSpy.mockRestore(); | ||
}); | ||
|
||
describe('should test getAffected ---', () => { | ||
test.each(['', 'TEST'])('for "%s"', async nx => { | ||
execSyncSpy.mockReturnValue(nx); | ||
const affected = await NxCommands.getAffected('main'); | ||
expect(affected).toEqual([nx || undefined]); | ||
expect(execSyncSpy).toHaveBeenCalled(); | ||
expect(execSyncSpy).toHaveBeenCalledTimes(1); | ||
expect(execSyncSpy).toHaveBeenCalledWith( | ||
'nx show projects --affected --base=origin/main' | ||
); | ||
}); | ||
}); | ||
|
||
describe('should test getProjectsWithTask ---', () => { | ||
test.each(['', 'TEST'])('for "%s"', nx => { | ||
execSyncSpy.mockReturnValue(nx); | ||
const projects = NxCommands.getProjectsWithTask('build'); | ||
expect(projects).toEqual([nx]); | ||
expect(execSyncSpy).toHaveBeenCalled(); | ||
expect(execSyncSpy).toHaveBeenCalledTimes(1); | ||
expect(execSyncSpy).toHaveBeenCalledWith( | ||
'nx show projects --with-target build' | ||
); | ||
}); | ||
|
||
test('for multiple', () => { | ||
execSyncSpy.mockReturnValue('TEST1\nTEST2\nTEST3\nTEST4'); | ||
const projects = NxCommands.getProjectsWithTask('publish'); | ||
expect(projects).toEqual(['TEST1', 'TEST2', 'TEST3', 'TEST4']); | ||
expect(execSyncSpy).toHaveBeenCalled(); | ||
expect(execSyncSpy).toHaveBeenCalledTimes(1); | ||
expect(execSyncSpy).toHaveBeenCalledWith( | ||
'nx show projects --with-target publish' | ||
); | ||
}); | ||
}); | ||
|
||
describe('should test getProjectInfo ---', () => { | ||
test.each(['{}', '{"test":"test"}'])('for "%s"', nx => { | ||
execSyncSpy.mockReturnValue(nx); | ||
const projectInfo = NxCommands.getProjectInfo('test-project'); | ||
expect(projectInfo).toBeDefined(); | ||
expect(execSyncSpy).toHaveBeenCalled(); | ||
expect(execSyncSpy).toHaveBeenCalledTimes(1); | ||
expect(execSyncSpy).toHaveBeenCalledWith( | ||
'nx show project test-project --json' | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { ProjectConfiguration } from '@nx/devkit'; | ||
import { execSync } from 'node:child_process'; | ||
import { info } from 'node:console'; | ||
|
||
export abstract class NxCommands { | ||
static async getAffected(dest: string): Promise<string[]> { | ||
const affected = execSync( | ||
`nx show projects --affected --base=origin/${dest}` | ||
) | ||
.toString() | ||
.trim() | ||
.split('\n') | ||
.filter(project => project !== null && project.length !== 0); | ||
info(`\n\t\tAffected projects Count: ${affected.length}\n`); | ||
return affected; | ||
} | ||
|
||
static getProjectsWithTask(executionTask: string): string[] { | ||
return execSync(`nx show projects --with-target ${executionTask}`) | ||
.toString() | ||
.trim() | ||
.split('\n'); | ||
} | ||
|
||
static getProjectInfo(project: string): ProjectConfiguration { | ||
return JSON.parse( | ||
execSync(`nx show project ${project} --json`).toString().trim() | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.