forked from enzymejs/enzyme
-
Notifications
You must be signed in to change notification settings - Fork 2
/
env.js
executable file
·156 lines (141 loc) · 4.95 KB
/
env.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
const path = require('path');
const fs = require('fs');
const spawn = require('child_process').spawn;
const rimraf = require('rimraf');
const semver = require('semver');
const promisify = (fn) => new Promise((res, rej) => {
const done = (err, val) => (err ? rej(err) : res(val));
fn(done);
});
const getFile = (fpath) => promisify((cb) => fs.readFile(fpath, 'utf8', cb));
// const getFiles = fpath => promisify(cb => fs.readdir(fpath, cb));
const getJSON = (fpath) => getFile(fpath).then((json) => JSON.parse(json));
const writeFile = (fpath, src) => promisify((cb) => {
console.log('writeFile', fpath, src);
if (process.env.DEBUG) {
cb();
} else {
fs.writeFile(fpath, src, cb);
}
});
const writeJSON = (fpath, json, pretty = false) => writeFile(
fpath,
(pretty ? JSON.stringify(json, null, 2) : JSON.stringify(json)) + '\n'
);
const primraf = (fpath) => promisify((cb) => {
console.log('rimraf', fpath);
if (process.env.DEBUG) {
cb();
} else {
rimraf(fpath, cb);
}
});
const run = (cmd, ...args) => promisify((cb) => {
console.log(cmd + ' ' + args.join(' '));
const child = spawn(
process.env.DEBUG ? 'echo' : cmd,
process.env.DEBUG ? [cmd].concat(args) : args,
{ stdio: 'inherit' }
);
child.on('exit', cb);
});
// This script is executed with a single argument, indicating the version of
// react and adapters etc. that we want to set ourselves up for testing.
// should be "14" for "enzyme-adapter-react-14", "15.4" for "enzyme-adapter-react-15.4", etc.
const version = process.argv[2];
// This script will do the following:
//
// 1. remove / uninstall all relevant modules
// 2. find the adapter for the passed in version
// 3. get the package.json for the adapter
// 4. add the adapter to the dev-deps for enzyme-test-suite package
// 5. call lerna bootstrap to link all the packages
// 6. install all of the package's peer deps at the top level
const root = process.cwd();
function getAdapter(reactVersion) {
if (semver.intersects(reactVersion, '0.13.x')) {
return '13';
}
if (semver.intersects(reactVersion, '0.14.x')) {
return '14';
}
if (semver.intersects(reactVersion, '^15.0.0-0')) {
if (semver.intersects(reactVersion, '>= 15.5')) {
return '15';
}
return '15.4';
}
if (semver.intersects(reactVersion, '^16.0.0-0')) {
if (semver.intersects(reactVersion, '>= 16.4')) {
return '16';
}
if (semver.intersects(reactVersion, '~16.3')) {
return '16.3';
}
if (semver.intersects(reactVersion, '~16.2')) {
return '16.2';
}
if (semver.intersects(reactVersion, '~16.0 || ~16.1')) {
return '16.1';
}
}
return null;
}
const reactVersion = version < 15 ? '0.' + version : version;
const adapterVersion = process.env.ADAPTER || getAdapter(reactVersion) || version;
const adapterName = `enzyme-adapter-react-${adapterVersion}`;
const adapterPackageJsonPath = path.join(root, 'packages', adapterName, 'package.json');
const testPackageJsonPath = path.join(root, 'packages', 'enzyme-test-suite', 'package.json');
if (!fs.statSync(adapterPackageJsonPath)) {
throw new Error('Adapter not found: "' + adapterName + '"');
}
const packagesToRemove = [
'react',
'react-dom',
'react-addons-test-utils',
'react-test-renderer',
'create-react-class',
].map((s) => `./node_modules/${s}`);
const additionalDirsToRemove = [
'node_modules/.bin/npm',
'node_modules/.bin/npm.cmd',
];
const rmrfs = []
.concat(packagesToRemove)
.concat(additionalDirsToRemove);
Promise.resolve()
.then(() => Promise.all(rmrfs.map((s) => primraf(s))))
.then(() => run('npm', 'i'))
.then(() => Promise.all([
getJSON(adapterPackageJsonPath),
getJSON(testPackageJsonPath),
]))
.then(([adapterJson, testJson]) => {
const peerDeps = adapterJson.peerDependencies;
const installs = Object.keys(peerDeps)
.filter((key) => !key.startsWith('enzyme'))
.map((key) => `${key}@${key.startsWith('react') ? reactVersion : peerDeps[key]}`);
if (process.env.RENDERER) {
// eslint-disable-next-line no-param-reassign
adapterJson.dependencies['react-test-renderer'] = process.env.RENDERER;
}
// eslint-disable-next-line no-param-reassign
testJson.dependencies[adapterName] = adapterJson.version;
return writeJSON(adapterPackageJsonPath, adapterJson, true).then(() => Promise.all([
// npm install the peer deps at the root
run('npm', 'i', '--no-save', ...installs),
// add the adapter to the dependencies of the test suite
writeJSON(testPackageJsonPath, testJson, true),
]));
})
.then(() => run('lerna', 'bootstrap', '--hoist=\'react*\''))
.then(() => getJSON(testPackageJsonPath))
.then((testJson) => {
// now that we've lerna bootstrapped, we can remove the adapter from the
// package.json so there is no diff
// eslint-disable-next-line no-param-reassign
delete testJson.dependencies[adapterName];
return writeJSON(testPackageJsonPath, testJson, true);
})
.catch((err) => console.error(err));