-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexecutor.ts
More file actions
81 lines (77 loc) · 2.43 KB
/
executor.ts
File metadata and controls
81 lines (77 loc) · 2.43 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import type { ExecutorContext } from '@nx/devkit';
import { executeProcess } from '../../internal/execute-process.js';
import { normalizeContext } from '../internal/context.js';
import type { CliCommandExecutorOptions } from './schema.js';
import { parseCliExecutorOptions } from './utils.js';
export type ExecutorOutput = {
success: boolean;
command?: string;
error?: Error;
};
/* eslint-disable-next-line max-lines-per-function */
export default async function runCliExecutor(
terminalAndExecutorOptions: CliCommandExecutorOptions,
context: ExecutorContext,
): Promise<ExecutorOutput> {
const { objectToCliArgs, formatCommandStatus, logger, stringifyError } =
await import('@code-pushup/utils');
const normalizedContext = normalizeContext(context);
const {
command: cliCommand,
verbose = false,
dryRun,
env: executorEnv,
bin,
projectPrefix, // @TODO do not forward to CLI. Handle in plugin logic only
...restArgs
} = parseCliExecutorOptions(terminalAndExecutorOptions, normalizedContext);
// this sets `CP_VERBOSE=true` on process.env
logger.setVerbose(verbose);
const command = bin ? `node` : 'npx';
const args = [
bin ?? '@code-pushup/cli',
...(cliCommand ? [cliCommand] : []),
...objectToCliArgs(restArgs),
];
const loggedEnvVars = {
...executorEnv,
...(verbose && { CP_VERBOSE: 'true' }),
};
const commandString = formatCommandStatus([command, ...args].join(' '), {
cwd: context.cwd,
env: loggedEnvVars,
});
if (dryRun) {
logger.warn(`DryRun execution of: ${commandString}`);
} else {
try {
logger.debug(`Run CLI with env vars: ${JSON.stringify(loggedEnvVars)}`);
await executeProcess({
command,
args,
...(context.cwd ? { cwd: context.cwd } : {}),
...(executorEnv && Object.keys(executorEnv).length > 0
? {
env: {
// if env is undefined, executeProcess extends process.env by default
...process.env,
// we don't pass `CP_VERBOSE=true` as it is handled inside logger.setVerbose
...executorEnv,
},
}
: {}),
});
} catch (error) {
logger.error(stringifyError(error));
return {
success: false,
command: commandString,
error: error instanceof Error ? error : new Error(`${error}`),
};
}
}
return {
success: true,
command: commandString,
};
}