forked from fastify/fastify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
executable file
·225 lines (197 loc) · 6.74 KB
/
generate.js
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
'use strict'
const {
readFile,
writeFile,
existsSync
} = require('fs')
const path = require('path')
const chalk = require('chalk')
const generify = require('generify')
const argv = require('yargs-parser')
const cliPkg = require('./package')
const { execSync } = require('child_process')
const log = require('./log')
const javascriptTemplate = {
dir: 'app',
main: 'app.js',
scripts: {
test: 'tap "test/**/*.test.js"',
start: 'fastify start -l info app.js',
dev: 'fastify start -w -l info -P app.js'
},
dependencies: {
fastify: cliPkg.dependencies.fastify,
'fastify-plugin': cliPkg.devDependencies['fastify-plugin'] || cliPkg.dependencies['fastify-plugin'],
'@fastify/autoload': cliPkg.devDependencies['@fastify/autoload'],
'@fastify/sensible': cliPkg.devDependencies['@fastify/sensible'],
'fastify-cli': '^' + cliPkg.version
},
devDependencies: {
tap: cliPkg.devDependencies.tap
},
logInstructions: function (pkg) {
log('debug', 'saved package.json')
log('info', `project ${pkg.name} generated successfully`)
log('debug', `run '${chalk.bold('npm install')}' to install the dependencies`)
log('debug', `run '${chalk.bold('npm start')}' to start the application`)
log('debug', `run '${chalk.bold('npm run dev')}' to start the application with pino-pretty pretty logging (not suitable for production)`)
log('debug', `run '${chalk.bold('npm test')}' to execute the unit tests`)
if (pkg.scripts.lint) {
log('debug', `run '${chalk.bold('npm lint')}' to run linter and fix code style issues`)
}
}
}
const typescriptTemplate = {
dir: 'app-ts',
main: 'app.ts',
scripts: {
test: 'npm run build:ts && tsc -p test/tsconfig.json && tap --ts "test/**/*.test.ts"',
start: 'npm run build:ts && fastify start -l info dist/app.js',
'build:ts': 'tsc',
'watch:ts': 'tsc -w',
dev: 'npm run build:ts && concurrently -k -p "[{name}]" -n "TypeScript,App" -c "yellow.bold,cyan.bold" "npm:watch:ts" "npm:dev:start"',
'dev:start': 'fastify start --ignore-watch=.ts$ -w -l info -P dist/app.js'
},
dependencies: {
fastify: cliPkg.dependencies.fastify,
'fastify-plugin': cliPkg.devDependencies['fastify-plugin'] || cliPkg.dependencies['fastify-plugin'],
'@fastify/autoload': cliPkg.devDependencies['@fastify/autoload'],
'@fastify/sensible': cliPkg.devDependencies['@fastify/sensible'],
'fastify-cli': '^' + cliPkg.version
},
devDependencies: {
'@types/node': cliPkg.devDependencies['@types/node'],
'@types/tap': cliPkg.devDependencies['@types/tap'],
'ts-node': cliPkg.devDependencies['ts-node'],
concurrently: cliPkg.devDependencies.concurrently,
'fastify-tsconfig': cliPkg.devDependencies['fastify-tsconfig'],
tap: cliPkg.devDependencies.tap,
typescript: cliPkg.devDependencies.typescript
},
nodemonConfig: {
watch: ['src/'],
ignore: ['dist/*']
},
logInstructions: function (pkg) {
log('debug', 'saved package.json')
log('info', `project ${pkg.name} generated successfully`)
log('debug', `run '${chalk.bold('npm install')}' to install the dependencies`)
log('debug', `run '${chalk.bold('npm start')}' to start the application`)
log('debug', `run '${chalk.bold('npm build:ts')}' to compile the typescript application`)
log('debug', `run '${chalk.bold('npm run dev')}' to start the application with pino-pretty pretty logging (not suitable for production)`)
log('debug', `run '${chalk.bold('npm test')}' to execute the unit tests`)
}
}
function generate (dir, template) {
return new Promise((resolve, reject) => {
generify(path.join(__dirname, 'templates', template.dir), dir, {}, function (file) {
log('debug', `generated ${file}`)
}, function (err) {
if (err) {
return reject(err)
}
process.chdir(dir)
execSync('npm init -y')
log('info', `reading package.json in ${dir}`)
readFile('package.json', (err, data) => {
if (err) {
return reject(err)
}
let pkg
try {
pkg = JSON.parse(data)
} catch (err) {
return reject(err)
}
pkg.main = template.main
pkg.type = template.type
pkg.scripts = Object.assign(pkg.scripts || {}, template.scripts)
pkg.dependencies = Object.assign(pkg.dependencies || {}, template.dependencies)
pkg.devDependencies = Object.assign(pkg.devDependencies || {}, template.devDependencies)
pkg.tap = template.tap
log('debug', 'edited package.json, saving')
writeFile('package.json', JSON.stringify(pkg, null, 2), (err) => {
if (err) {
return reject(err)
}
template.logInstructions(pkg)
resolve()
})
})
})
})
}
function cli (args) {
const opts = argv(args)
const dir = opts._[0]
if (dir && existsSync(dir)) {
if (dir !== '.' && dir !== './') {
log('error', 'directory ' + opts._[0] + ' already exists')
process.exit(1)
}
}
if (dir === undefined) {
log('error', 'must specify a directory to \'fastify generate\'')
process.exit(1)
}
if (!opts.integrate && existsSync(path.join(dir, 'package.json'))) {
log('error', 'a package.json file already exists in target directory')
process.exit(1)
}
let template
if (opts.lang === 'ts' || opts.lang === 'typescript') {
template = { ...typescriptTemplate }
if (opts.esm) {
template.dir = 'app-ts-esm'
template.type = 'module'
template.tap = {
'node-arg': [
'--no-warnings',
'--experimental-loader',
'ts-node/esm'
],
coverage: false
}
// For coverage, NYC with Typescript ESM doesn't work https://github.com/tapjs/node-tap/issues/735
template.devDependencies.c8 = cliPkg.devDependencies.c8
template.scripts.test = 'npm run build:ts && tsc -p test/tsconfig.json && c8 tap --ts "test/**/*.test.ts"'
}
} else {
template = { ...javascriptTemplate }
if (opts.esm) {
template.dir = 'app-esm'
template.type = 'module'
template.tap = {
coverage: false
}
template.devDependencies.c8 = cliPkg.devDependencies.c8
template.scripts.test = 'c8 tap "test/**/*.test.js"'
}
if (opts.standardlint) {
template.scripts = {
...template.scripts,
pretest: 'standard',
lint: 'standard --fix'
}
template.devDependencies = {
...template.devDependencies,
standard: cliPkg.devDependencies.standard
}
}
}
generate(dir, template).catch(function (err) {
if (err) {
log('error', err.message)
process.exit(1)
}
})
}
module.exports = {
generate,
cli,
javascriptTemplate,
typescriptTemplate
}
if (require.main === module) {
cli(process.argv.slice(2))
}