forked from fastify/fastify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
executable file
·171 lines (152 loc) · 5.41 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
'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-colada pretty logging (not suitable for production)`)
log('debug', `run '${chalk.bold('npm test')}' to execute the unit tests`)
}
}
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-colada 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.scripts = Object.assign(pkg.scripts || {}, template.scripts)
pkg.dependencies = Object.assign(pkg.dependencies || {}, template.dependencies)
pkg.devDependencies = Object.assign(pkg.devDependencies || {}, template.devDependencies)
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)
}
const template = opts.lang === 'ts' || opts.lang === 'typescript' ? typescriptTemplate : javascriptTemplate
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))
}