-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Generate.ts
156 lines (125 loc) · 4.24 KB
/
Generate.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
import { Command, arg, format, HelpError, getSchemaPath, isError } from '@prisma/sdk'
import chalk from 'chalk'
import logUpdate from 'log-update'
import { missingGeneratorMessage, getGenerators, highlightTS, link } from '@prisma/sdk'
import { formatms } from './utils/formatms'
import { simpleDebounce } from './utils/simpleDebounce'
import fs from 'fs'
import path from 'path'
const pkg = eval(`require('../package.json')`)
/**
* $ prisma2 generate
*/
export class Generate implements Command {
public static new(): Generate {
return new Generate()
}
// static help template
private static help = format(`
Generate artifacts (e.g. Prisma Client)
${chalk.bold('Usage')}
With an existing schema.prisma:
${chalk.dim('$')} prisma2 generate
Or specify a schema:
${chalk.dim('$')} prisma2 generate --schema=./schema.prisma'
${chalk.bold('Flags')}
--watch Watches the Prisma project file
`)
private logText = ''
private runGenerate = simpleDebounce(async ({ generators, watchMode }) => {
const message: string[] = []
for (const generator of generators) {
const toStr = generator.options!.generator.output!
? chalk.dim(` to .${path.sep}${path.relative(process.cwd(), generator.options!.generator.output!)}`)
: ''
const name = generator.manifest ? generator.manifest.prettyName : generator.options!.generator.provider
const before = Date.now()
await generator.generate()
generator.stop()
const after = Date.now()
message.push(`✔ Generated ${chalk.bold(name!)}${toStr} in ${formatms(after - before)}\n`)
}
this.logText += message.join('\n')
})
// parse arguments
public async parse(argv: string[]): Promise<string | Error> {
// parse the arguments according to the spec
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--watch': Boolean,
'--schema': String,
})
if (isError(args)) {
return this.help(args.message)
}
if (args['--help']) {
return this.help()
}
const watchMode = args['--watch']
const schemaPath = await getSchemaPath(args['--schema'])
if (!schemaPath) {
throw new Error(
`Either provide ${chalk.greenBright(
'--schema',
)} or make sure that you are in a folder with a ${chalk.greenBright('schema.prisma')} file.`,
)
}
const generators = await getGenerators({
schemaPath,
printDownloadProgress: !watchMode,
version: pkg.prisma.version,
cliVersion: pkg.version,
})
if (generators.length === 0) {
console.error(missingGeneratorMessage)
}
const watchingText = `\n${chalk.green('Watching...')} ${chalk.dim(schemaPath)}\n`
if (watchMode) {
logUpdate(watchingText)
fs.watch(schemaPath, async eventType => {
if (eventType === 'change') {
const generators = await getGenerators({
schemaPath,
printDownloadProgress: !watchMode,
version: pkg.prisma.version,
cliVersion: pkg.version,
})
if (generators.length === 0) {
console.error(missingGeneratorMessage)
}
logUpdate(`\n${chalk.green('Building...')}\n\n${this.logText}`)
await this.runGenerate({ generators, watchMode })
logUpdate(watchingText + '\n' + this.logText)
}
})
}
await this.runGenerate({ generators, watchMode })
const isJSClient = generators.find((g) => g.options && g.options.generator.provider === 'prisma-client-js')
if (watchMode) {
logUpdate(watchingText + '\n' + this.logText)
await new Promise(r => null)
} else {
const hint = `
You can now start using Prisma Client in your code:
\`\`\`
${highlightTS(`\
import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()`)}
\`\`\`
Explore the full API: ${link('http://pris.ly/d/client')}`
logUpdate(
this.logText + (isJSClient ? hint : ''),
)
}
return ''
}
// help message
public help(error?: string): string | HelpError {
if (error) {
return new HelpError(`\n${chalk.bold.red(`!`)} ${error}\n${Generate.help}`)
}
return Generate.help
}
}