-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
340 lines (309 loc) · 8.83 KB
/
cli.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env node
import { intro, outro, spinner } from '@clack/prompts'
import { red, bold, bgYellow, black, yellow, green, bgRed } from 'picocolors'
import minimist from 'minimist'
import { join, relative, resolve } from 'node:path'
import { existsSync, mkdirSync } from 'node:fs'
import { emptyDir } from './lib/empty-dir'
import { renderApp } from './lib/renderers/app'
import { renderLib } from './lib/renderers/lib'
import { renderMonorepo } from './lib/renderers/monorepo'
import { renderWithChangesets } from './lib/renderers/wtih-changesets'
import { renderWithHusky } from './lib/renderers/wtih-husky'
import { renderWithGH } from './lib/renderers/wtih-gh'
import { gitInitRepo } from './lib/git-init-repo'
import { renderReadme } from './lib/renderers/readme'
import { getCommand } from './lib/get-command'
import { askInstallDeps, runPrompt } from './lib/run-prompt'
import { installDeps } from './lib/inistall-deps'
import { ensurePnpm } from './lib/ensure-pnpm'
import { renderWithCommitlint } from './lib/renderers/with-commitlint'
const { version } = require('./package.json')
// possible options:
// --lib
// --app
// --help
// --monorepo
// --force (for force overwriting)
// --inject (for injecting into existing project)
// --git (for git init)
// --actions (for github actions)
// --changesets (for changesets)
// --commitlint (for commitlint)
const {
_,
lib,
app,
monorepo,
force,
inject,
git,
actions,
changesets,
commitlint,
} = minimist(process.argv.slice(2), {
string: ['_'],
boolean: true,
})
const cwd = process.cwd()
const projectDir = _[0]
const defaultProjectDir = projectDir
if ([app, lib, monorepo].filter(Boolean).length > 1) {
console.log(
red('✖'),
bold('Choose only one between --app, --lib and --monorepo'),
)
process.exit(1)
}
async function main() {
await ensurePnpm()
console.log()
intro(bgYellow(black(` Crane CLI v${version} `)))
const {
projectDir,
existingProject,
packageName,
projectType,
initChangesets,
initGit,
initCommitLint,
initActions,
} = await runPrompt({
projectDir: defaultProjectDir,
app,
lib,
monorepo,
force,
inject,
changesets,
git,
commitlint,
actions,
})
const fullProjectDir = join(cwd, projectDir)
const projectAlreadyExists = existsSync(fullProjectDir)
if (projectAlreadyExists) {
if (existingProject === 'force') {
executeForce(fullProjectDir)
}
} else {
createFolder(fullProjectDir)
}
await scaffold({
fullProjectDir,
existingProject,
packageName,
projectType,
initChangesets,
initGit,
initCommitLint,
initActions,
})
const shouldInstallDeps = await askInstallDeps({ projectType })
if (shouldInstallDeps) {
await executeInstallDeps({
fullProjectDir,
})
}
outro(bgYellow(black("You're all set!")))
printGuideText({
fullProjectDir,
changesets,
projectType,
shouldInstallDeps,
initGit,
})
}
main().catch(console.error)
function executeForce(fullProjectDir: string) {
const s = spinner()
s.start(` Deleting the content of ${fullProjectDir}...`)
try {
emptyDir(fullProjectDir)
} catch (e) {
console.log(red('✖'), bgRed(' Failed to delete project folder'))
console.log()
console.log(e)
process.exit(1)
}
s.stop(`${fullProjectDir} content deleted`)
}
function createFolder(fullProjectDir: string) {
const s = spinner()
s.start(`Creating project folder: ${fullProjectDir}`)
try {
mkdirSync(fullProjectDir, { recursive: true })
} catch (e) {
console.log(red('✖'), bgRed(' Failed to create project folder'))
console.log()
console.log(e)
process.exit(1)
}
s.stop(`Project folder ${fullProjectDir} created`)
}
async function executeInstallDeps({
fullProjectDir,
}: {
fullProjectDir: string
}) {
const s = spinner()
s.start(`Installing dependencies using in ${fullProjectDir} with pnpm...`)
try {
await installDeps({ destFolder: fullProjectDir })
} catch (e) {
console.log(red('✖'), bgRed(' Failed to install dependencies'))
console.log()
console.log(e)
process.exit(1)
}
s.stop('Dependencies installed')
}
async function scaffold({
fullProjectDir,
existingProject,
projectType,
packageName,
initChangesets,
initGit,
initCommitLint,
initActions,
}: {
fullProjectDir: string
existingProject: 'force' | 'inject' | 'skip'
projectType: 'app' | 'lib' | 'monorepo' | 'monorepo-app' | 'monorepo-lib'
packageName: string | undefined
initChangesets: boolean
initGit: boolean
initCommitLint: boolean
initActions: boolean
}) {
try {
const s = spinner()
const actionMessage =
existingProject === 'inject' ? 'Injecting' : 'Scaffolding'
s.start(`${actionMessage} ${packageName} in ${fullProjectDir}...`)
const templateRoot = resolve(__dirname, 'template')
if (projectType === 'app' || projectType === 'monorepo-app') {
await renderApp(
templateRoot,
packageName,
fullProjectDir,
existingProject,
)
} else if (projectType === 'lib' || projectType === 'monorepo-lib') {
await renderLib(
templateRoot,
packageName,
fullProjectDir,
existingProject,
)
} else if (projectType === 'monorepo') {
await renderMonorepo(templateRoot, fullProjectDir, existingProject)
}
s.stop('Base scaffolding complete')
if (initChangesets) {
const spinnerChangesets = spinner()
spinnerChangesets.start('Adding changesets...')
renderWithChangesets(templateRoot, fullProjectDir)
spinnerChangesets.stop('changesets added')
}
// render husky only at top root level project dir
if (initGit) {
const spinnerHusky = spinner()
spinnerHusky.start('Adding husky...')
renderWithHusky(templateRoot, fullProjectDir)
spinnerHusky.stop('husky added')
if (initCommitLint) {
const spinnerCommitLint = spinner()
spinnerCommitLint.start('Adding commitlint...')
renderWithCommitlint(templateRoot, fullProjectDir)
spinnerCommitLint.stop('commitlint added')
}
if (initActions) {
const spinnerActions = spinner()
spinnerActions.start('Adding GitHub actions...')
renderWithGH(templateRoot, fullProjectDir, 'standalone')
if (projectType === 'monorepo') {
renderWithGH(templateRoot, fullProjectDir, 'monorepo')
}
spinnerActions.stop('GitHub actions added')
}
const spinnerGit = spinner()
spinnerGit.start('Initializing git...')
await gitInitRepo(fullProjectDir)
spinnerGit.stop('git initialized')
}
const spinnerReadme = spinner()
spinnerReadme.start('Rendering README...')
renderReadme(
packageName ?? defaultProjectDir,
projectType,
fullProjectDir,
existingProject,
)
spinnerReadme.stop('README rendered')
} catch (e) {
const actionMessage = existingProject === 'inject' ? 'inject' : 'scaffold'
console.log(red('✖'), bgRed(` Failed to ${actionMessage} the project`))
console.log()
console.log(e)
process.exit(1)
}
}
function printGuideText({
fullProjectDir,
changesets,
projectType,
shouldInstallDeps,
initGit,
}: {
fullProjectDir: string
changesets: boolean
projectType: string
shouldInstallDeps: boolean
initGit: boolean
}) {
if (projectType !== 'monorepo-app' && projectType !== 'monorepo-lib') {
console.log(bold('Now run:\n'))
if (fullProjectDir !== cwd) {
console.log(` ${bold(green(`cd ${relative(cwd, fullProjectDir)}`))}`)
}
if (!shouldInstallDeps) {
console.log(` ${bold(green(getCommand('pnpm', 'install')))}`)
}
if (initGit) {
console.log()
console.log(
`${bold(
yellow(
'Check the .husky folder and make sure the hooks are executable. If not, run: chmod ug+x .husky/*',
),
)}`,
)
}
console.log()
console.log(bold('Available commands:\n'))
console.log()
console.log(` ${bold(green(getCommand('pnpm', 'dev')))}`)
console.log(` ${bold(green(getCommand('pnpm', 'build')))}`)
console.log(` ${bold(green(getCommand('pnpm', 'test')))}`)
console.log(` ${bold(green(getCommand('pnpm', 'test:ci')))}`)
console.log(` ${bold(green(getCommand('pnpm', 'lint')))}`)
console.log(` ${bold(green(getCommand('pnpm', 'format')))}`)
if (changesets) {
console.log(` ${bold(green(getCommand('pnpm', 'changeset')))}`)
console.log(` ${bold(green(getCommand('pnpm', 'changeset version')))}`)
console.log(` ${bold(green(getCommand('pnpm', 'release')))}`)
}
console.log()
if (projectType === 'monorepo') {
console.log(
`${bold(
yellow(
`To enable Turborepo Remote Cache, don't forget to put your settings inside ${fullProjectDir}/.turbo/config.json file.`,
),
)}`,
)
}
}
}