-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Version.ts
185 lines (160 loc) · 5.05 KB
/
Version.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { getCliQueryEngineBinaryType } from '@prisma/engines'
import { getPlatform } from '@prisma/get-platform'
import {
arg,
BinaryType,
Command,
engineEnvVarMap,
format,
getConfig,
getSchema,
getSchemaPath,
getVersion,
HelpError,
isError,
resolveBinary,
} from '@prisma/sdk'
import chalk from 'chalk'
import fs from 'fs'
import path from 'path'
import { getInstalledPrismaClientVersion } from './utils/getClientVersion'
const packageJson = require('../package.json') // eslint-disable-line @typescript-eslint/no-var-requires
interface BinaryInfo {
path: string
version: string
fromEnvVar?: string
}
/**
* $ prisma version
*/
export class Version implements Command {
static new(): Version {
return new Version()
}
private static help = format(`
Print current version of Prisma components
${chalk.bold('Usage')}
${chalk.dim('$')} prisma -v [options]
${chalk.dim('$')} prisma version [options]
${chalk.bold('Options')}
-h, --help Display this help message
--json Output JSON
`)
async parse(argv: string[]): Promise<string | Error> {
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--version': Boolean,
'-v': '--version',
'--json': Boolean,
'--telemetry-information': String,
})
if (isError(args)) {
return this.help(args.message)
}
if (args['--help']) {
return this.help()
}
const platform = await getPlatform()
const cliQueryEngineBinaryType = getCliQueryEngineBinaryType()
const introspectionEngine = await this.resolveEngine(
BinaryType.introspectionEngine,
)
const migrationEngine = await this.resolveEngine(BinaryType.migrationEngine)
// TODO This conditional does not really belong here, CLI should be able to tell you which engine it is _actually_ using
const queryEngine = await this.resolveEngine(cliQueryEngineBinaryType)
const fmtBinary = await this.resolveEngine(BinaryType.prismaFmt)
const prismaClientVersion = await getInstalledPrismaClientVersion()
const rows = [
[packageJson.name, packageJson.version],
['@prisma/client', prismaClientVersion ?? 'Not found'],
['Current platform', platform],
[
`Query Engine${
cliQueryEngineBinaryType === BinaryType.libqueryEngine
? ' (Node-API)'
: ' (Binary)'
}`,
this.printBinaryInfo(queryEngine),
],
['Migration Engine', this.printBinaryInfo(migrationEngine)],
['Introspection Engine', this.printBinaryInfo(introspectionEngine)],
['Format Binary', this.printBinaryInfo(fmtBinary)],
[
'Default Engines Hash',
packageJson.dependencies['@prisma/engines'].split('.').pop(),
],
['Studio', packageJson.devDependencies['@prisma/studio-server']],
]
const schemaPath = await getSchemaPath()
const featureFlags = await this.getFeatureFlags(schemaPath)
if (featureFlags && featureFlags.length > 0) {
rows.push(['Preview Features', featureFlags.join(', ')])
}
return this.printTable(rows, args['--json'])
}
private async getFeatureFlags(schemaPath: string | null): Promise<string[]> {
if (!schemaPath) {
return []
}
try {
const datamodel = await getSchema()
const config = await getConfig({
datamodel,
})
const generator = config.generators.find(
(g) => g.previewFeatures.length > 0,
)
if (generator) {
return generator.previewFeatures
}
} catch (e) {
// console.error(e)
}
return []
}
private printBinaryInfo({
path: absolutePath,
version,
fromEnvVar,
}: BinaryInfo): string {
const resolved = fromEnvVar ? `, resolved by ${fromEnvVar}` : ''
return `${version} (at ${path.relative(
process.cwd(),
absolutePath,
)}${resolved})`
}
private async resolveEngine(binaryName: BinaryType): Promise<BinaryInfo> {
const envVar = engineEnvVarMap[binaryName]
const pathFromEnv = process.env[envVar]
if (pathFromEnv && fs.existsSync(pathFromEnv)) {
const version = await getVersion(pathFromEnv, binaryName)
return { version, path: pathFromEnv, fromEnvVar: envVar }
}
const binaryPath = await resolveBinary(binaryName)
const version = await getVersion(binaryPath, binaryName)
return { path: binaryPath, version }
}
private printTable(rows: string[][], json = false): string {
if (json) {
const result = rows.reduce((acc, [name, value]) => {
acc[slugify(name)] = value
return acc
}, {})
return JSON.stringify(result, null, 2)
}
const maxPad = rows.reduce((acc, curr) => Math.max(acc, curr[0].length), 0)
return rows
.map(([left, right]) => `${left.padEnd(maxPad)} : ${right}`)
.join('\n')
}
public help(error?: string): string | HelpError {
if (error) {
return new HelpError(`\n${chalk.bold.red(`!`)} ${error}\n${Version.help}`)
}
return Version.help
}
}
function slugify(str: string): string {
return str.toString().toLowerCase().replace(/\s+/g, '-')
}