-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
66 lines (57 loc) · 1.65 KB
/
utils.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
const chalk = require('chalk');
const semver = require('semver');
const fse = require('fs-extra');
const { exec } = require('child_process');
const { removeComponents } = require('./config');
function getAppName(appName) {
if (!appName) return '';
let outText = '';
const wordArr = appName.split(/(\s)|_/g).filter((f) => f !== ' ');
for (let i in wordArr) {
if (i > 0) {
outText += `-${wordArr[i]}`;
} else {
outText += wordArr[i];
}
}
return outText.replace(/(\s)/g, '');
}
const verifyNodeVersion = () => {
const unsupportedNodeVersion = !semver.satisfies(
// Coerce strings with metadata (i.e. `15.0.0-nightly`).
semver.coerce(process.version),
'>=14'
);
if (unsupportedNodeVersion) {
console.log(
chalk.yellow(
`You are using Node ${process.version} so the project will be bootstrapped with an old unsupported version of tools.\n\n` +
`Please update to Node 14 or higher for a better, fully supported experience.\n`
)
);
// Fall back to the latest supported react-scripts on Node 4
process.exit(1);
}
console.log(chalk.green(`Using Node ${process.version}.`));
};
const selectYarnOrNpm = () =>
new Promise((resolve) => {
exec('yarn --version', (error, stdout, stderr) => {
if (error) {
return resolve('npm');
}
return resolve('yarn');
});
});
const removeFoldersAndFiles = ({ path, template }) => {
const pathsToRemove = removeComponents[template](path);
pathsToRemove.forEach((_path) => {
fse.removeSync(_path);
});
};
module.exports = {
getAppName,
removeFoldersAndFiles,
selectYarnOrNpm,
verifyNodeVersion
};