-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexecutor.ts
More file actions
66 lines (62 loc) · 2.02 KB
/
executor.ts
File metadata and controls
66 lines (62 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { ExecutorContext } from '@nx/devkit';
import { executeProcess } from '../../internal/execute-process.js';
import { normalizeContext } from '../internal/context.js';
import type { AutorunCommandExecutorOptions } from './schema.js';
import { parseAutorunExecutorOptions } from './utils.js';
export type ExecutorOutput = {
success: boolean;
command?: string;
error?: Error;
};
/* eslint-disable-next-line max-lines-per-function */
export default async function runAutorunExecutor(
terminalAndExecutorOptions: AutorunCommandExecutorOptions,
context: ExecutorContext,
): Promise<ExecutorOutput> {
const { objectToCliArgs, formatCommandStatus, logger, stringifyError } =
await import('@code-pushup/utils');
const normalizedContext = normalizeContext(context);
const cliArgumentObject = parseAutorunExecutorOptions(
terminalAndExecutorOptions,
normalizedContext,
);
const { command: cliCommand } = terminalAndExecutorOptions;
const { verbose = false, dryRun, bin, ...restArgs } = cliArgumentObject;
logger.setVerbose(verbose);
const command = bin ? `node` : 'npx';
const positionals = [
bin ?? '@code-pushup/cli',
...(cliCommand ? [cliCommand] : []),
];
const args = [...positionals, ...objectToCliArgs(restArgs)];
const executorEnvVariables = {
...(verbose && { CP_VERBOSE: 'true' }),
};
const commandString = formatCommandStatus([command, ...args].join(' '), {
cwd: context.cwd,
env: executorEnvVariables,
});
if (dryRun) {
logger.warn(`DryRun execution of: ${commandString}`);
} else {
try {
logger.debug(`With env vars: ${executorEnvVariables}`);
await executeProcess({
command,
args,
...(context.cwd ? { cwd: context.cwd } : {}),
});
} catch (error) {
logger.error(stringifyError(error));
return {
success: false,
command: commandString,
error: error instanceof Error ? error : new Error(`${error}`),
};
}
}
return {
success: true,
command: commandString,
};
}