-
Notifications
You must be signed in to change notification settings - Fork 167
/
generate-plugin.js
executable file
·116 lines (102 loc) · 3.14 KB
/
generate-plugin.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
'use strict'
const {
readFile,
writeFile
} = require('node:fs').promises
const { existsSync } = require('node:fs')
const path = require('node:path')
const chalk = require('chalk')
const generify = require('generify')
const argv = require('yargs-parser')
const cliPkg = require('./package')
const { execSync } = require('node:child_process')
const { promisify } = require('node:util')
const log = require('./log')
const pluginTemplate = {
dir: 'plugin',
main: 'index.js',
types: 'index.d.ts',
scripts: {
lint: 'standard && npm run lint:typescript',
'lint:typescript': 'ts-standard',
test: 'npm run lint && npm run unit && npm run test:typescript',
'test:typescript': 'tsd',
unit: 'node --test'
},
dependencies: {
'fastify-plugin': cliPkg.dependencies['fastify-plugin']
},
devDependencies: {
'@types/node': cliPkg.devDependencies['@types/node'],
fastify: cliPkg.devDependencies.fastify,
'fastify-tsconfig': cliPkg.devDependencies['fastify-tsconfig'],
standard: cliPkg.devDependencies.standard,
'ts-standard': cliPkg.devDependencies['ts-standard'],
tsd: cliPkg.devDependencies.tsd,
typescript: cliPkg.devDependencies.typescript
},
tsd: {
directory: 'test'
},
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 test')}' to execute the tests`)
}
}
async function generate (dir, template) {
const generifyPromise = promisify(generify)
await generifyPromise(
path.join(__dirname, 'templates', template.dir),
dir,
{}
)
process.chdir(dir)
execSync('npm init -y')
log('info', `reading package.json in ${dir}`)
const pkg = await readFile('package.json').then(JSON.parse)
pkg.main = template.main
pkg.types = template.types
pkg.description = ''
pkg.license = 'MIT'
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.tsd = Object.assign(pkg.tsd || {}, template.tsd)
log('debug', 'edited package.json, saving')
await writeFile('package.json', JSON.stringify(pkg, null, 2))
template.logInstructions(pkg)
}
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)
}
generate(dir, pluginTemplate).catch(function (err) {
if (err) {
log('error', err.message)
process.exit(1)
}
})
}
module.exports = {
generate,
cli,
pluginTemplate
}
if (require.main === module) {
cli(process.argv.slice(2))
}