-
Notifications
You must be signed in to change notification settings - Fork 165
/
generate-readme.js
138 lines (116 loc) · 3.64 KB
/
generate-readme.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
'use strict'
const { readFileSync, existsSync } = require('node:fs')
const path = require('node:path')
const generify = require('generify')
const argv = require('yargs-parser')
const { execSync } = require('node:child_process')
const log = require('./log')
function toMarkdownList (a) {
return a.map(d => `- ${d}`).join('\n')
}
function generate (dir, { pluginMeta, encapsulated, pluginFileName }) {
process.chdir(dir)
return new Promise((resolve, reject) => {
if (!existsSync(path.join(dir, 'package.json'))) {
execSync('npm init -y')
log('info', `generated package.json in ${dir}`)
}
log('info', `reading package.json in ${dir}`)
let pkg = readFileSync('package.json')
try {
pkg = JSON.parse(pkg)
} catch (err) {
return reject(err)
}
pluginMeta.decorators = pluginMeta.decorators ? pluginMeta.decorators : { fastify: [], reply: [] }
pluginMeta.dependencies = pluginMeta.dependencies ? pluginMeta.dependencies : []
const peerDepFastify = pkg.peerDependencies ? pkg.peerDependencies.fastify : ''
const depFastify = pkg.dependencies ? pkg.dependencies.fastify : ''
const minFastify = pluginMeta.fastify || peerDepFastify || depFastify
let accessibilityTemplate = ''
if (!encapsulated) {
accessibilityTemplate = '- [X] Accessible in the same context where you require them\n- [ ] Accessible only in a child context\n'
} else {
accessibilityTemplate = '- [ ] Accessible in the same context where you require them\n- [X] Accessible only in a child context\n'
}
const fastifyDecorators = toMarkdownList(pluginMeta.decorators.fastify)
const replyDecorators = toMarkdownList(pluginMeta.decorators.reply)
const pluginDeps = toMarkdownList(pluginMeta.dependencies)
generify(
path.join(__dirname, 'templates', 'readme'),
dir,
{
accessibilityTemplate,
fastifyDecorators,
replyDecorators,
pluginDeps,
packageName: pkg.name,
pluginFileName,
minFastify
},
function (file) {
log('debug', `generated ${file}`)
},
function (err) {
if (err) {
return reject(err)
}
log('info', `README for plugin ${pkg.name} generated successfully`)
resolve()
}
)
})
}
function stop (error) {
if (error) {
console.log(error)
process.exit(1)
}
process.exit()
}
function showHelp () {
console.log(
readFileSync(path.join(__dirname, 'help', 'readme.txt'), 'utf8')
)
return stop()
}
function cli (args) {
const opts = argv(args)
const dir = process.cwd()
if (opts._.length !== 1) {
log('error', 'Missing the required file parameter\n')
return showHelp()
}
if (existsSync(path.join(dir, 'README.md'))) {
log('error', 'a README.md file already exists in target directory')
process.exit(1)
}
const pluginPath = path.join(dir, path.basename(opts._[0], '.js'))
let plugin
try {
plugin = require(pluginPath)
} catch (err) {
log('error', 'plugin could not be loaded', err)
process.exit(1)
}
const pluginMeta = plugin[Symbol.for('plugin-meta')]
if (!pluginMeta) {
log('error', 'no plugin metadata could be found. Are you sure that you use https://github.com/fastify/fastify-plugin ?')
process.exit(1)
}
const encapsulated = !plugin[Symbol.for('skip-override')]
const pluginFileName = path.basename(opts._[0])
generate(dir, { pluginMeta, encapsulated, pluginFileName }).catch(function (err) {
if (err) {
log('error', err.message)
process.exit(1)
}
})
}
module.exports = {
generate,
cli
}
if (require.main === module) {
cli(process.argv.slice(2))
}