forked from fastify/fastify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
executable file
·114 lines (97 loc) · 2.65 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
'use strict'
const {
readFile,
writeFile
} = require('fs')
const chalk = require('chalk')
const path = require('path')
const generify = require('generify')
const minimist = require('minimist')
const templatedir = path.join(__dirname, 'app_template')
const cliPkg = require('./package')
function generate (dir, log, cb) {
if (!cb) {
cb = log
log = () => {}
}
const pkgFile = path.join(dir, 'package.json')
log('info', `reading package.json in ${dir}`)
readFile(pkgFile, (err, data) => {
if (err) {
return cb(err)
}
var pkg
try {
pkg = JSON.parse(data)
} catch (err) {
return cb(err)
}
pkg.scripts.test = 'standard && tap test/*.test.js test/*/*.test.js test/*/*/*.test.js'
pkg.scripts.start = 'fastify start app.js'
pkg.scripts.dev = 'fastify start -l info -P app.js'
pkg.scripts.lint = 'standard --fix'
pkg.dependencies = Object.assign(pkg.dependencies || {}, {
'fastify': cliPkg.dependencies.fastify,
'fastify-plugin': cliPkg.devDependencies['fastify-plugin'],
'fastify-autoload': cliPkg.devDependencies['fastify-autoload'],
'fastify-cli': '^' + cliPkg.version
})
pkg.devDependencies = Object.assign(pkg.devDependencies || {}, {
'standard': cliPkg.devDependencies['standard'],
'tap': cliPkg.devDependencies['tap']
})
log('debug', `edited package.json, saving`)
writeFile(pkgFile, JSON.stringify(pkg), (err) => {
if (err) {
return cb(err)
}
log('debug', `saved package.json`)
log('info', `copying sample project`)
generify(templatedir, dir, {}, function (file) {
log('debug', `generated ${file}`)
}, function (err) {
if (err) {
return cb(err)
}
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 test')}' to execute the unit tests`)
cb()
})
})
})
}
const levels = {
'debug': 0,
'info': 1,
'error': 2
}
const colors = [
(l) => l,
chalk.green,
chalk.red
]
function cli (args) {
const opts = minimist(args)
generate(opts._[0] || process.cwd(), log, function (err) {
if (err) {
log('error', err.message)
process.exit(1)
}
})
function log (severity, line) {
const level = levels[severity] || 0
if (level === 1) {
line = '--> ' + line
}
console.log(colors[level](line))
}
}
module.exports = {
generate,
cli
}
if (require.main === module) {
cli(process.argv.slice(2))
}