-
Notifications
You must be signed in to change notification settings - Fork 21
/
Process.ts
82 lines (70 loc) · 2.72 KB
/
Process.ts
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
82
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import {ChildProcess, spawn} from 'child_process';
import type ProjectWorkspace from './project_workspace';
import type {LoginShell} from './project_workspace';
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return
const isLoginShell = (arg: any): arg is LoginShell => arg && typeof arg.path === 'string' && Array.isArray(arg.args);
/**
* Spawns and returns a Jest process with specific args
*
* @param {string[]} args
* @returns {ChildProcess}
*/
export const createProcess = (workspace: ProjectWorkspace, args: string[]): ChildProcess => {
const runtimeExecutable = [workspace.jestCommandLine, ...args];
// If a path to configuration file was defined, push it to runtimeArgs
if (workspace.pathToConfig) {
runtimeExecutable.push('--config');
runtimeExecutable.push(workspace.pathToConfig);
}
const env = {...process.env, ...(workspace.nodeEnv ?? {})};
const cmd = runtimeExecutable.join(' ');
const spawnCommandLine = () => {
const spawnOptions = {
cwd: workspace.rootPath,
env,
shell: typeof workspace.shell === 'string' && workspace.shell ? workspace.shell : true,
// for non-windows: run in detached mode so the process will be the group leader and any subsequent process spawned
// within can be later killed as a group to prevent orphan processes.
// see https://nodejs.org/api/child_process.html#child_process_options_detached
detached: process.platform !== 'win32',
};
if (workspace.debug) {
// eslint-disable-next-line no-console
console.log(`spawning process with command=${cmd}`, 'options:', spawnOptions);
}
return spawn(cmd, [], spawnOptions);
};
const spawnLoginShell = (shell: LoginShell) => {
const spawnOptions = {
cwd: workspace.rootPath,
env,
detached: process.platform !== 'win32',
};
if (workspace.debug) {
// eslint-disable-next-line no-console
console.log(
`spawning login-shell "${shell.path} ${shell.args.join(' ')}" for command=${cmd}`,
'options:',
spawnOptions
);
}
const child = spawn(shell.path, shell.args, spawnOptions);
child.stdin.write(`${cmd} \nexit $?\n`);
return child;
};
if (isLoginShell(workspace.shell)) {
if (process.platform === 'win32') {
console.error('currently login-shell is only supported for non-windown platforms');
} else {
return spawnLoginShell(workspace.shell);
}
}
return spawnCommandLine();
};