-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
213 lines (188 loc) · 6.87 KB
/
index.js
File metadata and controls
213 lines (188 loc) · 6.87 KB
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
import { readFile } from 'node:fs/promises'
/**
* @typedef {import('node:util').ParseArgsConfig} ParseArgsConfig
* @typedef {Exclude<ParseArgsConfig['options'], undefined>} ParseArgsOptionsConfig
* @typedef {ParseArgsOptionsConfig[string]} ParseArgsOptionConfig
*
* @typedef {Object} ArgscloptsOptions
* @property {string} [help] - At least one argument with a help string is required.
* @property {string} [helpLabel] - Override the help label
* Extends ParseArgsOptionConfig with additional properties 'help' and 'helpLabel'.
*
* // Define an extended version of ParseArgsOptionsConfig
* @typedef {Record<string, ArgscloptsOptions & ParseArgsOptionConfig>} ArgscloptsParseArgsOptionsConfig
*/
const width = 23
const indent = ' '
/**
* @param {ArgscloptsParseArgsOptionsConfig} options - Options matching the config.options shape in util.parseArgs
* @returns {string} - Description of what the return value represents
*/
export function usage (options) {
let output = ''
Object.entries(options)
.forEach(([name, opt]) => {
output += indent
let helpLabel = opt.helpLabel
if (!helpLabel) {
helpLabel = '--' + name
if (opt.short) helpLabel += ', -' + opt.short
}
output += helpLabel
let offset = 0
if (helpLabel.length > width) {
output += '\n' + indent
} else {
offset = helpLabel.length
}
output += Array(width - offset).join(' ')
output += opt.help
if (opt.default) {
output += ' (default: ' + JSON.stringify(opt.default) + ')'
}
output += '\n'
})
return output
}
/**
* Read a package.json at a path
* @param {string} pkgPath The path to the package json file
* @return {Promise<{
* name: string
* version: string
* }>} The package.json contents parsed into an object
*/
export async function readPkg (pkgPath) {
return JSON.parse(await readFile(pkgPath, 'utf8'))
}
/**
* A function used for formatting with given inputs
*
* @callback FormatterFunction
* @param {Object} arg - The argument object for the header function.
* @param {string} [arg.name] - The name of the package or command line
* @param {string} [arg.version] - The version of the package
* @returns {string} The result string from the header function.
*/
/**
* Generates a header string based on the provided parameters.
*
* @param {Object} params - The parameters object.
* @param {string} [params.pkgPath] - The path to the package file. Used to determine the package name if 'name' is not provided.
* @param {string} [params.name] - The name to be used in the header. If not provided, it's extracted from the package file at 'pkgPath'.
* @param {FormatterFunction} [params.headerFn] - A function that returns the usage string.
* @param {FormatterFunction} [params.exampleFn] - A function that returns an example string.
* @returns {Promise<string>} - A promise that resolves to the constructed header string.
* @throws {Error} If a name cannot be determined.
*
* @example
* header({ pkgPath: './package.json', name: 'myApp' })
* .then(console.log); // Logs the header string for 'myApp'.
*/
export async function header ({
pkgPath,
name,
headerFn = ({ name }) => `Usage: ${name} [options]\n`,
exampleFn = ({ name }) => indent + `Example: ${name}\n`
}) {
let pkg
if (!name && pkgPath) {
pkg = await readPkg(pkgPath)
}
const pkgName = name ?? pkg?.name
if (!pkgName) throw new Error('A name cannot be determined')
const header = [headerFn({ name: pkgName }), exampleFn({ name: pkgName })].join('\n')
return header
}
/**
* Generates a footer string based on the provided parameters.
*
* @param {Object} params - The parameters object.
* @param {string} [params.pkgPath] - The path to the package file. Used to determine the package name if 'name' is not provided.
* @param {string} [params.name] - The name to be used in the header. If not provided, it's extracted from the package file at 'pkgPath'.
* @param {string} [params.version] - The version to be used in the footer. If not provided, it's extracted from the package file at 'pkgPath'.
* @param {FormatterFunction} [params.footerFn] - A function that returns the footer string.
* @returns {Promise<string>} - A promise that resolves to the constructed footer string.
* @throws {Error} If a name cannot be determined.
*
*
*/
export async function footer ({
pkgPath,
name,
version,
footerFn = ({ name, version }) => `${name} (v${version})`
}) {
let pkg
if ((!name || !version) && pkgPath) {
pkg = await readPkg(pkgPath)
}
const pkgName = name ?? pkg?.name
if (!pkgName) throw new Error('A name cannot be determined')
const pkgVersion = version ?? pkg?.version
if (!pkgVersion) throw new Error('A name cannot be determined')
return footerFn({ name: pkgName, version: pkgVersion })
}
/**
* Generate the full help text string
*
* @param {Object} params
* @param {ArgscloptsParseArgsOptionsConfig} params.options - Options matching the config.options shape in util.parseArgs
* @param {string} [params.pkgPath] - The path to a package.json.
* @param {string} [params.name] - The bin name
* @param {string} [params.version] - The bin version
* @param {FormatterFunction} [params.headerFn] - A function that returns the usage string.
* @param {FormatterFunction} [params.exampleFn] - A function that returns an example string.
* @param {FormatterFunction} [params.footerFn] - A function that returns the footer string.
* @returns {Promise<string>} - A promise that resolves to the constructed footer string.
* @throws {Error} If a name cannot be determined.
*
*
*/
export async function formatHelpText ({
options,
pkgPath,
name,
version,
footerFn,
headerFn,
exampleFn
}) {
const helpText = [
await header({ pkgPath, name, headerFn, exampleFn }),
usage(options),
await footer({ pkgPath, name, version, footerFn })
]
return helpText.join('\n')
}
/**
* Generate and print the full help text string
*
* @param {Object} params
* @param {ArgscloptsParseArgsOptionsConfig} params.options - Options matching the config.options shape in util.parseArgs
* @param {string} [params.pkgPath] - The path to a package.json.
* @param {string} [params.name] - The bin name
* @param {string} [params.version] - The bin version
* @param {FormatterFunction} [params.headerFn] - A function that returns the usage string.
* @param {FormatterFunction} [params.exampleFn] - A function that returns an example string.
* @param {FormatterFunction} [params.footerFn] - A function that returns the footer string.
* @throws {Error} If a name cannot be determined.
*
*
*/
export async function printHelpText ({
options,
pkgPath,
name,
version,
footerFn,
headerFn,
exampleFn
}) {
const helpText = [
await header({ pkgPath, name, headerFn, exampleFn }),
usage(options),
await footer({ pkgPath, name, version, footerFn })
]
console.log(helpText.join('\n'))
}