/
perf-init.js
52 lines (45 loc) · 1.56 KB
/
perf-init.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
/* eslint-disable no-console */
const { accessSync, constants } = require('fs');
const path = require('path');
const execa = require('execa');
const { removeSync } = require('fs-extra');
const repoWithBranch = process.argv[2];
const TARGET_DIR = path.resolve(__dirname, '..', 'perf');
const VALID_REPO = /^([^/\s#]+\/[^/\s#]+)(#([^/\s#]+))?$/;
if (process.argv.length !== 3 || !VALID_REPO.test(repoWithBranch)) {
console.error(
'You need to provide a GitHub repo in the form <username>/<repo> or <username>/<repo>#branch, e.g. ' +
'"npm run perf:init rollup/rollup"'
);
process.exit(1);
}
console.error(`Cleaning up '${TARGET_DIR}'...`);
removeSync(TARGET_DIR);
const [, repo, , branch] = VALID_REPO.exec(repoWithBranch);
setupNewRepo(repo, branch).catch(error => {
console.error(error.message);
process.exit(1);
});
function execWithOutput(command, args) {
const call = execa(command, args);
call.stderr.pipe(process.stderr);
return call;
}
async function setupNewRepo(repo, branch) {
const gitArgs = ['clone', '--depth', 1, '--progress'];
if (branch) {
console.error(`Cloning branch "${branch}" of "${repo}"...`);
gitArgs.push('--branch', branch);
} else {
console.error(`Cloning "${repo}"...`);
}
gitArgs.push(`https://github.com/${repo}.git`, TARGET_DIR);
await execWithOutput('git', gitArgs);
try {
accessSync(path.resolve(TARGET_DIR, 'rollup.config.js'), constants.R_OK);
} catch (e) {
throw new Error('The repository needs to have a file "rollup.config.js" at the top level.');
}
process.chdir(TARGET_DIR);
await execWithOutput('npm', ['install']);
}