forked from prisma/prisma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersion.ts
More file actions
130 lines (107 loc) · 3.55 KB
/
Version.ts
File metadata and controls
130 lines (107 loc) · 3.55 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
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
import type { PrismaConfigInternal } from '@prisma/config'
import { enginesVersion } from '@prisma/engines'
import {
arg,
BinaryType,
Command,
createSchemaPathInput,
format,
formatTable,
getEnginesInfo,
getTypescriptVersion,
HelpError,
isError,
loadSchemaContext,
resolveEngine,
wasm,
} from '@prisma/internals'
import { bold, dim, red } from 'kleur/colors'
import os from 'os'
import { getInstalledPrismaClientVersion } from './utils/getClientVersion'
const packageJson = require('../package.json')
/**
* $ 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
`)
public help(error?: string): string | HelpError {
if (error) {
return new HelpError(`\n${bold(red(`!`))} ${error}\n${Version.help}`)
}
return Version.help
}
async parse(argv: string[], config: PrismaConfigInternal, baseDir: string = process.cwd()): Promise<string | Error> {
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--version': Boolean,
'-v': '--version',
'--config': String,
'--json': Boolean,
'--telemetry-information': String,
})
if (isError(args)) {
return this.help(args.message)
}
if (args['--help']) {
return this.help()
}
const engineResult = await resolveEngine(BinaryType.SchemaEngineBinary)
const [enginesInfo, schemaEngineRetrievalErrors] = getEnginesInfo(engineResult)
const schemaEngineRows = [['Schema Engine', enginesInfo] as const]
const prismaClientVersion = await getInstalledPrismaClientVersion()
const typescriptVersion = await getTypescriptVersion()
const rows = [
[packageJson.name, packageJson.version],
['@prisma/client', prismaClientVersion ?? 'Not found'],
['Operating System', os.platform()],
['Architecture', os.arch()],
['Node.js', process.version],
['TypeScript', typescriptVersion],
['Query Compiler', 'enabled'],
['PSL', `@prisma/prisma-schema-wasm ${wasm.prismaSchemaWasmVersion}`],
...schemaEngineRows,
['Default Engines Hash', enginesVersion],
['Studio', packageJson.dependencies['@prisma/studio-core']],
]
/**
* 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 (schemaEngineRetrievalErrors.length > 0) {
process.exitCode = 1
schemaEngineRetrievalErrors.forEach((e) => console.error(e))
}
const featureFlags = await this.getFeatureFlags(config.schema, baseDir)
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 | undefined, baseDir: string): Promise<string[]> {
try {
const { generators } = await loadSchemaContext({
schemaPath: createSchemaPathInput({ schemaPathFromConfig: schemaPath, baseDir }),
})
const generator = generators.find((g) => g.previewFeatures.length > 0)
if (generator) {
return generator.previewFeatures
}
} catch (e) {
// console.error(e)
}
return []
}
}