-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrap.ts
More file actions
140 lines (124 loc) · 3.88 KB
/
Copy pathBootstrap.ts
File metadata and controls
140 lines (124 loc) · 3.88 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
131
132
133
134
135
136
137
138
139
140
import inquirer from 'inquirer'
import * as path from 'path'
import * as fs from 'fs'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { Builder } from './Builder.js'
import { Logger } from './Logger.js'
import { Npm } from './Npm.js'
import { PackageJson } from './PackageJson.js'
import { LoadedStarter, StarterLoader } from './StarterLoader.js'
import { Path } from './types.js'
interface ParsedArgs {
dir: string
debug: boolean
force: boolean
projectName: string
[key: string]: unknown
}
export class Bootstrap {
protected starterLoader = new StarterLoader()
private async askMissingOptions(parsedArgs: ParsedArgs) {
const starters: LoadedStarter[] = []
for (const module of this.starterLoader.getOptions()) {
const moduleOption = parsedArgs[module.name.toLowerCase()] as string
if (moduleOption) {
if (moduleOption !== 'none') {
starters.push(this.starterLoader.getStarter(moduleOption))
}
continue
}
const answer = await inquirer.prompt<{ starter: string }>({
type: 'list',
name: 'starter',
message: `Which ${module.name} would you like to use?`,
choices: [...module.starters, 'none'],
})
if (answer.starter !== 'none') {
starters.push(this.starterLoader.getStarter(answer.starter))
}
}
return starters
}
public async runCLI(args: string[]) {
try {
let cli = yargs(hideBin(args))
.usage('create-node-app [options]')
.option('dir', {
type: 'string',
alias: 'd',
default: './node-app',
description: 'Destination directory',
})
.option('debug', {
type: 'boolean',
alias: 'D',
default: false,
description: 'Enables debug logs',
})
.option('project-name', {
type: 'string',
alias: 'n',
default: 'node-app',
description: 'Google Cloud project name',
})
.option('force', {
type: 'boolean',
alias: 'f',
default: false,
description:
"Overwrite existing destination directory if it's not empty",
})
const starterOptions = this.starterLoader.getOptions()
for (const module of starterOptions) {
cli = cli.option(module.name.toLowerCase(), {
type: 'string',
choices: ['none', ...module.starters],
description: `Selects ${module.name}`,
})
}
cli = cli
.version()
.help()
.check(argv => {
for (const [key, val] of Object.entries(argv)) {
if (Array.isArray(val) && key !== '_') {
throw new Error(`Option --${key} specified multiple times`)
}
}
return true
})
const parsedArgs = cli.parseSync()
const destination = path.normalize(parsedArgs.dir) as Path
const logger = new Logger(parsedArgs.debug)
const npm = new Npm({ dir: destination, logger })
const packageJson = new PackageJson(npm, logger)
if (fs.existsSync(destination) && !parsedArgs.force) {
const answer = await inquirer.prompt<{ force: boolean }>({
type: 'confirm',
name: 'force',
message: `Destination directory "${destination}" already exists. Do you want to overwrite everything in it?`,
})
if (!answer.force) {
process.exit(0)
}
}
const starters = await this.askMissingOptions(parsedArgs)
const builder = new Builder({
npm,
logger,
packageJson,
starters,
destination: destination,
projectName: parsedArgs.projectName,
})
await builder.build()
} catch (error) {
if (error instanceof Error && error.name === 'ExitPromptError') {
process.exit(0)
} else {
throw error
}
}
}
}