-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,035 additions
and
33 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Kill Process Executor | ||
|
||
This executor is used to kill a process by PID, command of file. | ||
|
||
#### @org/build-env:kill-process | ||
|
||
## Usage | ||
|
||
// project.json | ||
|
||
```json | ||
{ | ||
"name": "my-project", | ||
"targets": { | ||
"stop-verdaccio-env": { | ||
"executor": "@org/build-env:kill-process" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
By default, the Nx plugin will derive the options from the executor config. | ||
|
||
The following things happen: | ||
|
||
- ??? | ||
|
||
```jsonc | ||
{ | ||
"name": "my-project", | ||
"targets": { | ||
"stop-verdaccio-env": { | ||
"executor": "@org/build-env:kill-process", | ||
"options": { | ||
"filePath": "verdaccio-pid.json", | ||
"verbose": true, | ||
"progress": false | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Show what will be executed without actually executing it: | ||
|
||
`nx run my-project:stop-verdaccio-env --dryRun` | ||
|
||
## Options | ||
|
||
| Name | type | description | | ||
|--------------| --------- |--------------------------------------------------------------------| | ||
| **filePath** | `string` | Path to the file containing the PID of the process | | ||
| **dryRun** | `boolean` | To debug the executor, dry run the command without real execution. | |
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,35 @@ | ||
import {type ExecutorContext, logger} from '@nx/devkit'; | ||
// eslint-disable-next-line n/no-sync | ||
import type {KillProcessExecutorOptions} from './schema'; | ||
import {join} from "node:path"; | ||
import {killProcessFromPid} from "../../internal/utils/process"; | ||
|
||
export type ExecutorOutput = { | ||
success: boolean; | ||
command?: string; | ||
error?: Error; | ||
}; | ||
|
||
export default async function runKillProcessExecutor( | ||
terminalAndExecutorOptions: KillProcessExecutorOptions, | ||
context: ExecutorContext, | ||
) { | ||
|
||
const {projectName} = context; | ||
const {workspaceRoot} = { | ||
...terminalAndExecutorOptions, | ||
workspaceRoot: join('tmp', 'environments', projectName) | ||
}; | ||
|
||
logger.info(`Execute @org/stop-verdaccio-env:kill-process with options: ${JSON.stringify(terminalAndExecutorOptions, null, 2)}`); | ||
try { | ||
const envResult = await killProcessFromPid(join(workspaceRoot, 'verdaccio-registry.json')); | ||
logger.info(`envResult: ${JSON.stringify(['envResult'], null, 2)}`); | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
return Promise.resolve({ | ||
success: true, | ||
command: '????????', | ||
} satisfies ExecutorOutput); | ||
} |
114 changes: 114 additions & 0 deletions
114
tools/build-env/src/executors/kill-process/executor.unit.test.ts
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,114 @@ | ||
import {ExecutorContext, logger} from '@nx/devkit'; | ||
// eslint-disable-next-line n/no-sync | ||
import {execSync} from 'node:child_process'; | ||
import {afterEach, beforeEach, expect, vi} from 'vitest'; | ||
import runKillProcessExecutor from './executor'; | ||
|
||
vi.mock('node:child_process', async () => { | ||
const actual = await vi.importActual('node:child_process'); | ||
|
||
return { | ||
...actual, | ||
// eslint-disable-next-line n/no-sync | ||
execSync: vi.fn((command: string) => { | ||
if (command.includes('THROW_ERROR')) { | ||
throw new Error(command); | ||
} | ||
}), | ||
}; | ||
}); | ||
|
||
describe('runAutorunExecutor', () => { | ||
const envSpy = vi.spyOn(process, 'env', 'get'); | ||
const loggerInfoSpy = vi.spyOn(logger, 'info'); | ||
const loggerWarnSpy = vi.spyOn(logger, 'warn'); | ||
|
||
beforeEach(() => { | ||
envSpy.mockReturnValue({}); | ||
}); | ||
afterEach(() => { | ||
loggerWarnSpy.mockReset(); | ||
loggerInfoSpy.mockReset(); | ||
envSpy.mockReset().mockReturnValue({}); | ||
}); | ||
|
||
it('should call execSync with stop-verdaccio command and return result', async () => { | ||
const output = await runKillProcessExecutor({}, {} as ExecutorContext); | ||
expect(output.success).toBe(true); | ||
expect(output.command).toMatch('npx @org/cli stop-verdaccio'); | ||
// eslint-disable-next-line n/no-sync | ||
expect(execSync).toHaveBeenCalledWith( | ||
expect.stringContaining('npx @org/cli stop-verdaccio'), | ||
{ cwd: '/test' }, | ||
); | ||
}); | ||
|
||
it('should normalize context', async () => { | ||
const output = await runKillProcessExecutor( | ||
{}, | ||
{ | ||
...{} as ExecutorContext, | ||
cwd: 'cwd-form-context', | ||
}, | ||
); | ||
expect(output.success).toBe(true); | ||
expect(output.command).toMatch('utils'); | ||
// eslint-disable-next-line n/no-sync | ||
expect(execSync).toHaveBeenCalledWith(expect.stringContaining('utils'), { | ||
cwd: 'cwd-form-context', | ||
}); | ||
}); | ||
|
||
it('should process executorOptions', async () => { | ||
const output = await runKillProcessExecutor( | ||
{ workspaceRoot: '.' }, | ||
{} as ExecutorContext, | ||
); | ||
expect(output.success).toBe(true); | ||
expect(output.command).toMatch('--persist.filename="REPORT"'); | ||
}); | ||
|
||
it('should create command from context, options and arguments', async () => { | ||
envSpy.mockReturnValue({ CP_PROJECT: 'CLI' }); | ||
const output = await runKillProcessExecutor( | ||
{ workspaceRoot: '.' }, | ||
{} as ExecutorContext | ||
); | ||
expect(output.command).toMatch('--persist.filename="REPORT"'); | ||
expect(output.command).toMatch( | ||
'--persist.format="md" --persist.format="json"', | ||
); | ||
expect(output.command).toMatch('--upload.project="CLI"'); | ||
}); | ||
|
||
it('should log information if verbose is set', async () => { | ||
const output = await runKillProcessExecutor( | ||
{ verbose: true }, | ||
{ ...{} as ExecutorContext, cwd: '<CWD>' }, | ||
); | ||
// eslint-disable-next-line n/no-sync | ||
expect(execSync).toHaveBeenCalledTimes(1); | ||
|
||
expect(output.command).toMatch('--verbose'); | ||
expect(loggerWarnSpy).toHaveBeenCalledTimes(0); | ||
expect(loggerInfoSpy).toHaveBeenCalledTimes(2); | ||
expect(loggerInfoSpy).toHaveBeenCalledWith( | ||
expect.stringContaining('Run stop-verdaccio executor'), | ||
); | ||
expect(loggerInfoSpy).toHaveBeenCalledWith( | ||
expect.stringContaining('Command: npx @org/cli stop-verdaccio'), | ||
); | ||
}); | ||
|
||
it('should log command if dryRun is set', async () => { | ||
await runKillProcessExecutor({ dryRun: true }, {} as ExecutorContext); | ||
|
||
expect(loggerInfoSpy).toHaveBeenCalledTimes(0); | ||
expect(loggerWarnSpy).toHaveBeenCalledTimes(1); | ||
expect(loggerWarnSpy).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'DryRun execution of: npx @org/cli stop-verdaccio --dryRun', | ||
), | ||
); | ||
}); | ||
}); |
Oops, something went wrong.