forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscripts.js
113 lines (103 loc) · 2.46 KB
/
scripts.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const execa = require('execa');
const getPort = require('get-port');
const os = require('os');
const stripAnsi = require('strip-ansi');
const waitForLocalhost = require('wait-for-localhost');
function stripYarn(output) {
let lines = output.split('\n');
let runIndex = lines.findIndex(line => line.match(/^yarn run/));
if (runIndex !== -1) {
lines.splice(0, runIndex + 2);
lines = lines.filter(line => !line.match(/^info Visit.*yarnpkg/));
}
return lines.join('\n');
}
function execaSafe(...args) {
return execa(...args)
.then(({ stdout, stderr, ...rest }) => ({
fulfilled: true,
rejected: false,
stdout: stripYarn(stripAnsi(stdout)),
stderr: stripYarn(stripAnsi(stderr)),
...rest,
}))
.catch(err => ({
fulfilled: false,
rejected: true,
reason: err,
stdout: '',
stderr: stripYarn(
stripAnsi(
err.message
.split(os.EOL)
.slice(2)
.join(os.EOL)
)
),
}));
}
module.exports = class ReactScripts {
constructor(root) {
this.root = root;
}
async start({ smoke = false, env = {} } = {}) {
const port = await getPort();
const options = {
cwd: this.root,
env: Object.assign(
{},
{
CI: 'false',
FORCE_COLOR: '0',
BROWSER: 'none',
PORT: port,
},
env
),
};
if (smoke) {
return await execaSafe('yarnpkg', ['start', '--smoke-test'], options);
}
const startProcess = execa('yarnpkg', ['start'], options);
await waitForLocalhost({ port });
return {
port,
done() {
startProcess.kill('SIGKILL');
},
};
}
async build({ env = {} } = {}) {
return await execaSafe('yarnpkg', ['build'], {
cwd: this.root,
env: Object.assign({}, { CI: 'false', FORCE_COLOR: '0' }, env),
});
}
async serve() {
const port = await getPort();
const serveProcess = execa(
'yarnpkg',
['serve', '--', '-p', port, '-s', 'build/'],
{
cwd: this.root,
}
);
await waitForLocalhost({ port });
return {
port,
done() {
serveProcess.kill('SIGKILL');
},
};
}
async test({ jestEnvironment = 'jsdom', env = {} } = {}) {
return await execaSafe(
'yarnpkg',
['test', '--env', jestEnvironment, '--ci'],
{
cwd: this.root,
env: Object.assign({}, { CI: 'true' }, env),
}
);
}
};