forked from prisma/prisma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Version.ts
154 lines (128 loc) · 4.21 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
import { enginesVersion, getCliQueryEngineBinaryType } from '@prisma/engines'
import { getBinaryTargetForCurrentPlatform } from '@prisma/get-platform'
import type { Command } from '@prisma/internals'
import {
arg,
BinaryType,
format,
formatTable,
getConfig,
getEnginesMetaInfo,
getSchema,
getSchemaPath,
HelpError,
isError,
loadEnvFile,
wasm,
} from '@prisma/internals'
import { bold, dim, red } from 'kleur/colors'
import os from 'os'
import { match, P } from 'ts-pattern'
import { getInstalledPrismaClientVersion } from './utils/getClientVersion'
const packageJson = require('../package.json') // eslint-disable-line @typescript-eslint/no-var-requires
/**
* $ prisma version
*/
export class Version implements Command {
static new(): Version {
return new Version()
}
private static help = format(`
Print current version of Prisma components
${bold('Usage')}
${dim('$')} prisma -v [options]
${dim('$')} prisma version [options]
${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()
}
loadEnvFile({ printMessage: true })
const binaryTarget = await getBinaryTargetForCurrentPlatform()
const cliQueryEngineBinaryType = getCliQueryEngineBinaryType()
const [enginesMetaInfo, enginesMetaInfoErrors] = await getEnginesMetaInfo()
const enginesRows = enginesMetaInfo.map((engineMetaInfo) => {
return (
match(engineMetaInfo)
.with({ 'query-engine': P.select() }, (currEngineInfo) => {
return [
`Query Engine${cliQueryEngineBinaryType === BinaryType.QueryEngineLibrary ? ' (Node-API)' : ' (Binary)'}`,
currEngineInfo,
]
})
// @ts-ignore TODO @jkomyno, as affects the type of rows
.with({ 'schema-engine': P.select() }, (currEngineInfo) => {
return ['Schema Engine', currEngineInfo]
})
.exhaustive()
)
})
const prismaClientVersion = await getInstalledPrismaClientVersion()
const rows = [
[packageJson.name, packageJson.version],
['@prisma/client', prismaClientVersion ?? 'Not found'],
['Computed binaryTarget', binaryTarget],
['Operating System', os.platform()],
['Architecture', os.arch()],
['Node.js', process.version],
...enginesRows,
['Schema Wasm', `@prisma/prisma-schema-wasm ${wasm.prismaSchemaWasmVersion}`],
['Default Engines Hash', enginesVersion],
['Studio', packageJson.devDependencies['@prisma/studio-server']],
]
/**
* If reading Rust engines metainfo (like their git hash) failed, display the errors to stderr,
* and let Node.js exit naturally, but with error code 1.
*/
if (enginesMetaInfoErrors.length > 0) {
process.exitCode = 1
enginesMetaInfoErrors.forEach((e) => console.error(e))
}
const schemaPath = await getSchemaPath()
const featureFlags = await this.getFeatureFlags(schemaPath)
if (featureFlags && featureFlags.length > 0) {
rows.push(['Preview Features', featureFlags.join(', ')])
}
// @ts-ignore TODO @jkomyno, as affects the type of rows
return formatTable(rows, { json: args['--json'] })
}
private async getFeatureFlags(schemaPath: string | null): Promise<string[]> {
if (!schemaPath) {
return []
}
try {
const datamodel = await getSchema()
const config = await getConfig({
datamodel,
ignoreEnvVarErrors: true,
})
const generator = config.generators.find((g) => g.previewFeatures.length > 0)
if (generator) {
return generator.previewFeatures
}
} catch (e) {
// console.error(e)
}
return []
}
public help(error?: string): string | HelpError {
if (error) {
return new HelpError(`\n${bold(red(`!`))} ${error}\n${Version.help}`)
}
return Version.help
}
}