1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const baseEnv = {
...process.env,
CODE_SIGN_IDENTITY: 'iPhone Developer',
PROVISIONING_PROFILE: '5ad27f68-0e0f-41b6-a39c-350683afee4f',
};

const shell = function(cwd, cmd, opt) {
try {
console.log(cmd);
const opt_env = (opt && opt.env) || {};
const r = child_process.execSync(cmd, {
stdio: 'inherit',
cwd,
env: { ...baseEnv, ...opt_env },
...opt,
});
return r ? r.toString() : undefined;
} catch (e) {
if (opt && opt.noerror) {
console.log('ignore error', e.message);
} else throw e;
}
};

调用child_process.execSync时,若指定了env,切记一定要把继承来的process.env带上。
否则最重要的PATH变量就可能这么无意被清除了,导致子进程要调用shell命令时,发生各种找不到命令的奇怪错误:

Watchman was not found in PATH
Watchman: Watchman was not found in PATH. See https://facebook.github.io/watchman/docs/install.html for installation instructions