-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·78 lines (63 loc) · 1.94 KB
/
index.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
#! /usr/bin/env node
import * as child_process from 'node:child_process';
import yargs from 'yargs';
import confirm from '@inquirer/confirm';
import semverSort from 'semver/functions/sort.js';
/**
* Returns versions of a package between two versions
* @param {string} pkg
* @param {string} startVersion
* @param {string} endVersion
* @return {string[]}
*/
function getVersions(pkg, startVersion, endVersion) {
let versions = JSON.parse(
child_process.execSync(`npm view ${pkg} versions --json`),
).filter(isNotPreRelease);
versions = semverSort(versions);
const goodIndex = versions.indexOf(startVersion);
const badIndex = versions.indexOf(endVersion);
return versions.slice(goodIndex + 1, badIndex);
}
/**
* Returns true if the version is not a pre-release version, like 3.0.2-rc.1
* @param {string} version
* @return {boolean}
*/
function isNotPreRelease(version) {
return !version.includes('-');
}
/**
* Installs a specific version of a package using npm
* @param {string} version
* @param {string} installCommand - command to use when installing the package
*/
function installVersion(version, installCommand = 'npm install') {
child_process.execSync(`${installCommand} ${pkg}@${version}`);
}
function ask(version) {
return confirm({
message: `Installed version ${version}. Good?`,
});
}
const {
good,
bad,
package: pkg,
install: installCommand,
} = yargs(process.argv.slice(2)).parse();
const versionsToCheck = getVersions(pkg, good, bad);
let bisectStart = 0;
let bisectEnd = versionsToCheck.length - 1;
while (bisectStart !== bisectEnd) {
const mid = Math.floor((bisectStart + bisectEnd) / 2);
const midVersion = versionsToCheck[mid];
installVersion(midVersion, installCommand);
if (await ask(midVersion)) {
bisectStart = mid + 1;
} else {
bisectEnd = mid;
}
}
const culpritVersion = versionsToCheck[bisectStart];
console.info('\x1b[1m%s\x1b[0m', culpritVersion, 'is the first bad version.');