/
preinstall.js
95 lines (79 loc) · 2.58 KB
/
preinstall.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const path = require('path')
const globalDirs = require('global-dirs')
const { drawBox } = require('@prisma/sdk/dist/utils/drawBox')
const isInstalledGlobally = require('is-installed-globally')
const BOLD = '\u001b[1m'
const WHITE_BRIGHT = '\u001b[37;1m'
const RESET = '\u001b[0m'
// returns { pkgManager: 'yarn' | 'npm', pkgPath: string } | null
function isPackageInstalledGlobally(name) {
try {
const pkgPath = require.resolve(path.join(globalDirs.yarn.packages, `${name}/package.json`))
return { pkgManager: 'yarn', pkgPath }
} catch (_) {
try {
const pkgPath = require.resolve(path.join(globalDirs.npm.packages, `${name}/package.json`))
return { pkgManager: 'npm', pkgPath }
} catch (_) {
//
}
}
return null
}
function prismaIsInstalledGlobally() {
const prisma2InstalledGlobally = isPackageInstalledGlobally('prisma2')
if (prisma2InstalledGlobally) {
return {
...prisma2InstalledGlobally,
name: 'prisma2',
}
}
return null
}
const b = (str) => BOLD + str + RESET
const white = (str) => WHITE_BRIGHT + str + RESET
export function main() {
const nodeVersions = process.version.split('.')
const nodeMajorVersion = parseInt(nodeVersions[0].slice(1))
if (nodeMajorVersion < 14) {
console.error(
drawBox({
str: `Prisma only supports Node.js >= 14`,
verticalPadding: 1,
horizontalPadding: 3,
}),
)
process.exit(1)
}
// When running in npx, npm puts this package into a /_npx/ folder. Tested on Win, Mac, Linux
if (__dirname.includes('_npx')) {
process.exit(0)
}
if (!isInstalledGlobally) {
process.exit(0)
}
const installedGlobally = prismaIsInstalledGlobally()
if (!installedGlobally) {
process.exit(0)
}
const pkg = require(installedGlobally.pkgPath)
const parts = pkg.version.split('-')
const isDev = parts.length > 1 ? parts[1].split('.') === 'dev' : false
let message
if (installedGlobally.name === 'prisma2') {
message = `
The package ${white('prisma2')} has been renamed to ${white('prisma')}.
Please uninstall ${white('prisma2')} globally first.
Then install ${white('prisma')} to continue using ${b('Prisma 2+')}:
# Uninstall old CLI
${white(installedGlobally.pkgManager === 'yarn' ? 'yarn global remove prisma2' : 'npm uninstall -g prisma2')}
# Install new CLI
${white(`npm install prisma${isDev ? '@dev' : ''} --save-dev`)}
# Invoke via npx
${white('npx prisma --help')}
Learn more here: https://pris.ly/preview025
`
}
console.error(drawBox({ str: message, verticalPadding: 1, horizontalPadding: 3 }))
process.exit(1)
}