-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathshell.js
executable file
·85 lines (76 loc) · 2.54 KB
/
shell.js
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
83
84
85
// @flow
/* eslint-disable no-underscore-dangle */
import path from 'path';
import shell from 'shelljs';
import split from 'split';
import execa from 'execa';
import { mainStory, chalk } from 'storyboard';
import type { StoryT } from 'storyboard';
const cp = (src: string, dst: string, { story = mainStory }: { story?: StoryT } = {}) => {
story.debug(`Copying ${chalk.cyan.bold(src)} -> ${chalk.cyan.bold(dst)}...`);
shell.cp('-rf', path.normalize(src), path.normalize(dst));
};
const mv = (src: string, dst: string, { story = mainStory }: { story?: StoryT } = {}) => {
story.debug(`Moving ${chalk.cyan.bold(src)} -> ${chalk.cyan.bold(dst)}...`);
shell.mv('-rf', path.normalize(src), path.normalize(dst));
};
type ExecOptions = {|
story?: StoryT,
createChildStory?: boolean,
logLevel?: *,
errorLogLevel?: string,
cwd?: string,
bareLogs?: boolean,
|};
type ExecResult = {
code: number,
stdout: string,
stderr: string,
};
const exec = async (cmd: string, {
story = mainStory,
createChildStory = true,
logLevel = 'info',
errorLogLevel = 'error',
bareLogs = false,
cwd,
}: ExecOptions = {}): Promise<ExecResult> => {
let title = `Run cmd ${chalk.green.bold(cmd)}`;
if (cwd) title += ` at ${chalk.green(cwd)}`;
const ownStory = createChildStory
? story.child({ title, level: logLevel })
: story || mainStory;
try {
return await _exec(cmd, { cwd, story: ownStory, errorLogLevel, bareLogs });
} finally {
if (createChildStory) ownStory.close();
}
};
const _exec = async (cmd, { cwd, story, errorLogLevel, bareLogs }) => {
try {
const prefix = bareLogs ? '' : '| ';
const cmdName = cmd.split(' ')[0].slice(0, 10);
const child = execa.shell(cmd, {
cwd: cwd || '.',
// Workaround for Node.js bug: https://github.com/nodejs/node/issues/10836
// See also: https://github.com/yarnpkg/yarn/issues/2462
stdio: process.platform === 'win32' ? ['ignore', 'pipe', 'pipe'] : undefined,
});
child.stdout.pipe(split()).on('data', (line) => {
story.info(cmdName, `${prefix}${line}`);
});
child.stderr.pipe(split()).on('data', (line) => {
if (line) story[errorLogLevel](cmdName, `${prefix}${line}`);
});
const { code, stdout, stderr } = await child;
if (code !== 0) throw new Error(`Command returned non-zero exit code: ${cmd} [${code}]`);
return { code, stdout, stderr };
} catch (err) {
story[errorLogLevel](`Command '${cmd}' failed at ${cwd || '\'.\''}`, { attach: err });
throw new Error(`Command failed: ${cmd}`);
}
};
export {
cp, mv,
exec,
};