-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin.js
More file actions
71 lines (62 loc) · 1.98 KB
/
bin.js
File metadata and controls
71 lines (62 loc) · 1.98 KB
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
#!/usr/bin/env node
const which = require("which-pm-runs");
const semver = require("semver");
const argv = process.argv.slice(2);
if (argv.length === 0) {
console.error(
"Please specify the package manager: only-allow3 <npm|pnpm|yarn|bun|deno>",
);
process.exit(1);
}
let [pm, version] = argv[0].split("@");
if (["npm", "yarn", "pnpm", "bun", "deno"].indexOf(pm) === -1) {
console.error(
`"${pm}" is not a valid package manager. Valid package managers are: npm, pnpm, yarn, bun, or deno.`,
);
process.exit(1);
}
const current = which();
function notes(pm, cmd, link) {
console.warn("-".repeat(process.stdout.columns));
console.warn(
`This project requires to use "${cmd}" as the package manager.
Please use \`${pm}${cmd ? " " + cmd : ""}\` to install this project.
If you didn't have ${pm}, please learn more at ${link}.`,
);
console.warn("-".repeat(process.stdout.columns));
}
if (current.name !== pm) {
switch (pm) {
case "npm":
notes("npm", "install", "https://docs.npmjs.com/cli/");
break;
case "pnpm":
notes("pnpm", "install", "https://pnpm.io/");
break;
case "yarn":
notes("yarn", "", "https://yarnpkg.com/");
break;
case "bun":
notes("bun", "install", "https://bun.sh/");
break;
case "deno":
notes("deno", "install", "https://deno.com/");
break;
}
process.exit(1);
}
if (!version) process.exit(0);
if (semver.valid(version)) {
version = "^" + semver.coerce(version);
} else if (!semver.validRange(version)) {
console.error(`Invalid version range: ${version}`);
process.exit(1);
}
if (semver.satisfies(current.version, version)) process.exit(0);
console.error("-".repeat(process.stdout.columns));
console.error(
`The version of ${pm}(${current.version}) doesn't satisfies the requirements (${pm}@${version}).
Please update to at least ${semver.coerce(semver.minVersion(version))} (${semver.coerce(semver.validRange(version))
}).`,
);
console.error("-".repeat(process.stdout.columns));