-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
/
setup.js
104 lines (93 loc) · 2.73 KB
/
setup.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
const execa = require('execa');
const fs = require('fs-extra');
const path = require('path');
const tempy = require('tempy');
const ReactScripts = require('./scripts');
module.exports = class TestSetup {
constructor(fixtureName, templateDirectory, { pnp = true } = {}) {
this.fixtureName = fixtureName;
this.templateDirectory = templateDirectory;
this.testDirectory = null;
this._scripts = null;
this.setup = this.setup.bind(this);
this.teardown = this.teardown.bind(this);
this.isLocal = !(process.env.CI && process.env.CI !== 'false');
this.settings = { pnp: pnp && !this.isLocal };
}
async setup() {
await this.teardown();
this.testDirectory = tempy.directory();
await fs.copy(
path.resolve(__dirname, '..', 'template'),
this.testDirectory
);
await fs.copy(this.templateDirectory, this.testDirectory);
await fs.remove(path.resolve(this.testDirectory, 'test.partial.js'));
await fs.remove(path.resolve(this.testDirectory, '.disable-pnp'));
const packageJson = await fs.readJson(
path.resolve(this.testDirectory, 'package.json')
);
const shouldInstallScripts = !this.isLocal;
if (shouldInstallScripts) {
packageJson.dependencies = Object.assign({}, packageJson.dependencies, {
'react-scripts': 'latest',
});
}
packageJson.scripts = Object.assign({}, packageJson.scripts, {
start: 'react-scripts start',
build: 'react-scripts build',
test: 'react-scripts test',
});
packageJson.license = packageJson.license || 'UNLICENSED';
await fs.writeJson(
path.resolve(this.testDirectory, 'package.json'),
packageJson
);
await execa(
'yarnpkg',
[
'install',
this.settings.pnp ? '--enable-pnp' : null,
'--mutex',
'network',
].filter(Boolean),
{
cwd: this.testDirectory,
}
);
if (!shouldInstallScripts) {
await fs.ensureSymlink(
path.resolve(
path.resolve(
__dirname,
'../../../..',
'packages',
'react-scripts',
'bin',
'react-scripts.js'
)
),
path.join(this.testDirectory, 'node_modules', '.bin', 'react-scripts')
);
await execa('yarnpkg', ['link', 'react-scripts'], {
cwd: this.testDirectory,
});
}
}
get scripts() {
if (this.testDirectory == null) {
return null;
}
if (this._scripts == null) {
this._scripts = new ReactScripts(this.testDirectory);
}
return this._scripts;
}
async teardown() {
if (this.testDirectory != null) {
await fs.remove(this.testDirectory);
this.testDirectory = null;
this._scripts = null;
}
}
};