-
-
Notifications
You must be signed in to change notification settings - Fork 782
/
app-generator.ts
187 lines (164 loc) · 5.92 KB
/
app-generator.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
186
187
import {Generator, GeneratorOptions} from '../generator'
import spawn from 'cross-spawn'
import chalk from 'chalk'
import username from 'username'
import {readJSONSync, writeJson} from 'fs-extra'
import {resolve, join} from 'path'
import {fetchLatestVersionsFor} from '../utils/fetch-latest-version-for'
import {log} from '@blitzjs/display'
import {getBlitzDependencyVersion} from '../utils/get-blitz-dependency-version'
export interface AppGeneratorOptions extends GeneratorOptions {
appName: string
useTs: boolean
yarn: boolean
version: string
skipInstall: boolean
}
export class AppGenerator extends Generator<AppGeneratorOptions> {
sourceRoot: string = resolve(__dirname, './templates/app')
// Disable file-level prettier because we manually run prettier at the end
prettierDisabled = true
filesToIgnore() {
if (!this.options.useTs) {
return ['tsconfig.json']
}
return ['jsconfig.json']
}
async getTemplateValues() {
return {
name: this.options.appName,
username: await username(),
}
}
getTargetDirectory() {
return ''
}
async preCommit() {
this.fs.move(this.destinationPath('gitignore'), this.destinationPath('.gitignore'))
}
async postWrite() {
const gitInitResult = spawn.sync('git', ['init'], {
stdio: 'ignore',
})
const pkgJsonLocation = join(this.destinationPath(), 'package.json')
const pkg = readJSONSync(pkgJsonLocation)
console.log('') // New line needed
const spinner = log.spinner(log.withBrand('Retrieving the freshest of dependencies')).start()
const [
{value: newDependencies, isFallback: dependenciesUsedFallback},
{value: newDevDependencies, isFallback: devDependenciesUsedFallback},
{value: blitzDependencyVersion, isFallback: blitzUsedFallback},
] = await Promise.all([
fetchLatestVersionsFor(pkg.dependencies),
fetchLatestVersionsFor(pkg.devDependencies),
getBlitzDependencyVersion(this.options.version),
])
pkg.dependencies = newDependencies
pkg.devDependencies = newDevDependencies
pkg.dependencies.blitz = blitzDependencyVersion
const fallbackUsed = dependenciesUsedFallback || devDependenciesUsedFallback || blitzUsedFallback
await writeJson(pkgJsonLocation, pkg, {spaces: 2})
if (!fallbackUsed && !this.options.skipInstall) {
spinner.succeed()
await new Promise((resolve) => {
const logFlag = this.options.yarn ? '--json' : '--loglevel=error'
const cp = spawn(this.options.yarn ? 'yarn' : 'npm', ['install', logFlag], {
stdio: ['inherit', 'pipe', 'pipe'],
})
const getJSON = (data: string) => {
try {
return JSON.parse(data)
} catch {
return null
}
}
const spinners: any[] = []
if (!this.options.yarn) {
const spinner = log
.spinner(log.withBrand('Installing those dependencies (this will take a few minutes)'))
.start()
spinners.push(spinner)
}
cp.stdout?.setEncoding('utf8')
cp.stderr?.setEncoding('utf8')
cp.stdout?.on('data', (data) => {
if (this.options.yarn) {
let json = getJSON(data)
if (json && json.type === 'step') {
spinners[spinners.length - 1]?.succeed()
const spinner = log.spinner(log.withBrand(json.data.message)).start()
spinners.push(spinner)
}
if (json && json.type === 'success') {
spinners[spinners.length - 1]?.succeed()
}
}
})
cp.stderr?.on('data', (data) => {
if (this.options.yarn) {
let json = getJSON(data)
if (json && json.type === 'error') {
spinners[spinners.length - 1]?.fail()
console.error(json.data)
}
} else {
// Hide the annoying Prisma warning about not finding the schema file
// because we generate the client ourselves
if (!data.includes('schema.prisma')) {
console.error(`\n${data}`)
}
}
})
cp.on('exit', (code) => {
if (!this.options.yarn && spinners[spinners.length - 1].isSpinning) {
if (code !== 0) spinners[spinners.length - 1].fail()
else {
spinners[spinners.length - 1].succeed()
}
}
resolve()
})
})
const runLocalNodeCLI = (command: string) => {
if (this.options.yarn) {
return spawn.sync('yarn', ['run', ...command.split(' ')])
} else {
return spawn.sync('npx', command.split(' '))
}
}
// Ensure the generated files are formatted with the installed prettier version
const prettierResult = runLocalNodeCLI('prettier --loglevel silent --write .')
if (prettierResult.status !== 0) {
throw new Error('Failed running prettier')
}
} else {
console.log('') // New line needed
spinner.fail(
chalk.red.bold(
`We had some trouble connecting to the network, so we'll skip installing your dependencies right now. Make sure to run ${
this.options.yarn ? "'yarn'" : "'npm install'"
} once you're connected again.`,
),
)
}
if (gitInitResult.status === 0) {
this.commitChanges()
} else {
log.warning('Failed to run git init.')
log.warning('Find out more about how to install git here: https://git-scm.com/downloads.')
}
}
commitChanges() {
const commands: Array<[string, string[], object]> = [
['git', ['add', '.'], {stdio: 'ignore'}],
['git', ['commit', '-m', 'New baby Blitz app!'], {stdio: 'ignore'}],
]
for (let command of commands) {
const result = spawn.sync(...command)
if (result.status !== 0) {
log.error(`Failed to run command ${command[0]} with ${command[1].join(' ')} options.`)
break
}
}
}
}