-
Notifications
You must be signed in to change notification settings - Fork 30
/
bin.js
58 lines (52 loc) · 2.12 KB
/
bin.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
#!/usr/bin/env node
const whichPMRuns = require('which-pm-runs')
const availablePMList = ['npm', 'cnpm', 'pnpm', 'yarn', 'bun']
function box(s) {
const lines = s.trim().split("\n")
const width = lines.reduce((a, b) => Math.max(a, b.length), 0)
const surround = x => '║ \x1b[0m' + x.padEnd(width) + '\x1b[31m ║'
const bar = '═'.repeat(width)
const top = '\x1b[31m╔═══' + bar + '═══╗'
const pad = surround('')
const bottom = '╚═══' + bar + '═══╝\x1b[0m'
return [top, pad, ...lines.map(surround), pad, bottom].join('\n')
}
const argv = process.argv.slice(2)
if (argv.length === 0) {
console.log(`Please specify the wanted package manager: only-allow <${availablePMList.join('|')}>`)
process.exit(1)
}
const wantedPM = argv[0]
if (!availablePMList.includes(wantedPM)) {
const pmStr = `${availablePMList.slice(0, -1).join(', ')} or ${availablePMList[availablePMList.length - 1]}`
console.log(`"${wantedPM}" is not a valid package manager. Available package managers are: ${pmStr}.`)
process.exit(1)
}
const usedPM = whichPMRuns()
const cwd = process.env.INIT_CWD || process.cwd()
const isInstalledAsDependency = cwd.includes('node_modules')
if (usedPM && usedPM.name !== wantedPM && !isInstalledAsDependency) {
switch (wantedPM) {
case 'npm':
console.log(box('Use "npm install" for installation in this project'))
break
case 'cnpm':
console.log(box('Use "cnpm install" for installation in this project'))
break
case 'pnpm':
console.log(box(`Use "pnpm install" for installation in this project.
If you don't have pnpm, install it via "npm i -g pnpm".
For more details, go to https://pnpm.io/`))
break
case 'yarn':
console.log(box(`Use "yarn" for installation in this project.
If you don't have Yarn, install it via "npm i -g yarn".
For more details, go to https://yarnpkg.com/`))
break
case 'bun':
console.log(box(`Use "bun install" for installation in this project.
If you don't have Bun, go to https://bun.sh/docs/installation and find installation method that suits your environment.`))
break
}
process.exit(1)
}