-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.mjs
More file actions
314 lines (283 loc) · 9.31 KB
/
utils.mjs
File metadata and controls
314 lines (283 loc) · 9.31 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import utils, { promisify } from 'node:util'
import { stat } from 'node:fs/promises'
import { execFile } from 'node:child_process'
import { platform } from 'node:os'
import http from 'node:http'
import https from 'node:https'
import { parse, format } from 'node:url'
import { getAllRegistries } from './config.mjs'
/**
* @param {import('http').RequestOptions | import('https').RequestOptions | string | URL} options
* @param {(res: import('http').IncomingMessage) => void} callback
* @returns {import('http').ClientRequest}
*/
export function request(options, callback) {
const url = parse(format(options), false, true)
const module = url.protocol === 'https:' ? https : http
return module.request(options, callback)
}
export const parseArgs = utils.parseArgs || _parseArgs
/**
* Polyfill for Node.js util.parseArgs
* @param {import('util').ParseArgsConfig} config
* @returns {ReturnType<import('util').parseArgs>}
*/
function _parseArgs(config = {}) {
const {
options = {},
strict = false,
allowPositionals = false,
args = process.argv.slice(2),
} = config
/** @type {{ [longOption: string]: undefined | string | boolean | Array<string | boolean> }} */
const values = {}
/** @type {string[]} */
const positionals = []
// Initialize default values
for (const [name, opt] of Object.entries(options)) {
if ('default' in opt) {
values[name] = opt.default
} else if (opt.multiple) {
values[name] = []
}
}
for (let i = 0; i < args.length; i++) {
const arg = args[i]
// Handle positional arguments
if (!arg.startsWith('-')) {
if (!allowPositionals && strict) {
throw new Error(`Unexpected positional argument: ${arg}`)
}
positionals.push(arg)
continue
}
// Handle long options (--option)
if (arg.startsWith('--')) {
const optName = arg.slice(2)
const option = options[optName]
if (!option) {
if (strict) {
throw new Error(`Unknown option: ${arg}`)
}
continue
}
if (option.type === 'boolean') {
if (option.multiple) {
if (!Array.isArray(values[optName])) values[optName] = []
values[optName].push(true)
} else {
values[optName] = true
}
} else if (option.type === 'string') {
const value = args[++i]
if (value === undefined) {
throw new Error(`Option ${arg} requires a value`)
}
if (option.multiple) {
if (!Array.isArray(values[optName])) values[optName] = []
values[optName].push(value)
} else {
values[optName] = value
}
}
continue
}
// Handle short options (-o)
if (arg.startsWith('-')) {
const shortOpts = arg.slice(1).split('')
for (let j = 0; j < shortOpts.length; j++) {
const shortOpt = shortOpts[j]
let optName = null
// Find option by short name
for (const [name, opt] of Object.entries(options)) {
if (opt.short === shortOpt) {
optName = name
break
}
}
if (!optName) {
if (strict) {
throw new Error(`Unknown option: -${shortOpt}`)
}
continue
}
const option = options[optName]
if (option.type === 'boolean') {
if (option.multiple) {
if (!Array.isArray(values[optName]))
values[optName] = []
//@ts-ignore
values[optName].push(true)
} else {
values[optName] = true
}
} else if (option.type === 'string') {
let value
// If not last char in group, remaining chars are the value
if (j < shortOpts.length - 1) {
value = shortOpts.slice(j + 1).join('')
j = shortOpts.length // End loop
} else {
value = args[++i]
if (value === undefined) {
throw new Error(
`Option -${shortOpt} requires a value`,
)
}
}
if (option.multiple) {
if (!Array.isArray(values[optName]))
values[optName] = []
//@ts-ignore
values[optName].push(value)
} else {
values[optName] = value
}
}
}
}
}
return { values, positionals }
}
/**
* ANSI escape codes mapping
* @typedef {keyof typeof styles} Format
*/
const styles = {
// Reset
reset: '\x1b[0m',
// Text styles
bold: '\x1b[1m',
dim: '\x1b[2m',
italic: '\x1b[3m',
underline: '\x1b[4m',
strikethrough: '\x1b[9m',
// Text colors
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
gray: '\x1b[90m',
grey: '\x1b[90m',
// Bright text colors
brightRed: '\x1b[91m',
brightGreen: '\x1b[92m',
brightYellow: '\x1b[93m',
brightBlue: '\x1b[94m',
brightMagenta: '\x1b[95m',
brightCyan: '\x1b[96m',
brightWhite: '\x1b[97m',
// Background colors
bgBlack: '\x1b[40m',
bgRed: '\x1b[41m',
bgGreen: '\x1b[42m',
bgYellow: '\x1b[43m',
bgBlue: '\x1b[44m',
bgMagenta: '\x1b[45m',
bgCyan: '\x1b[46m',
bgWhite: '\x1b[47m',
}
export const styleText = utils.styleText || _styleText
/**
* Basic implementation of util.styleText for formatting text with ANSI colors
* @param {Format | Format[]} format - A text format or an Array of text formats
* @param {string} text - The text to be formatted
* @returns {string} The formatted text with ANSI escape codes
*/
function _styleText(format, text) {
const formats = Array.isArray(format) ? format : [format]
// Build the opening escape sequences
let openCodes = ''
for (const fmt of formats)
if (fmt in styles) openCodes += styles[/** @type {Format} */ (fmt)]
// Return formatted text with reset at the end
return openCodes + text + styles.reset
}
/**
* @param {string} filePath
* @noexcept
*/
export const isFile = async (filePath) =>
await stat(filePath)
.then((stat) => stat.isFile())
.catch((_) => false)
export const _execFileAsync = promisify(execFile)
/**
* @param {string} file
* @param {string[]} args
*/
export async function execFileAsync(file, args) {
if (platform() === 'win32') {
// start /b cmd /c <file> <args>
return _execFileAsync('cmd', ['/c', file, ...args], {
windowsHide: true,
})
} else {
return _execFileAsync(file, args)
}
}
/**
* @param {string} currentRegistryUrl
* @param {Array<{name:string,url:string,timeSpent?:number|null}>=} registriesInfo
* @param {number=} timeoutLimit - milliseconds
*/
export async function printRegistries(
currentRegistryUrl,
registriesInfo,
timeoutLimit,
) {
const registries = await getAllRegistries()
if (!registriesInfo)
registriesInfo = Array.from(registries.entries()).map(
([name, url]) => ({
name,
url,
}),
)
const maxNameLength = Math.max(
...registriesInfo.map(({ name }) => name.length),
)
/**
* @type {number=}
*/
let maxUrlLength = undefined
for (let { name, url, timeSpent } of registriesInfo) {
if (timeoutLimit) {
// lazy compute
if (!maxUrlLength)
maxUrlLength = Math.max(
...registriesInfo.map(({ url }) => url.length),
)
}
const isCurrent = url === currentRegistryUrl
let row = `${isCurrent ? '*' : ' '} ${name.padEnd(maxNameLength)} → ${
maxUrlLength ? url.padEnd(maxUrlLength) : url
}`
if (isCurrent) row = styleText('blue', row)
if (timeoutLimit) {
if (!timeSpent) {
row += styleText('red', ` (Error)`)
} else if (timeSpent >= timeoutLimit) {
row += styleText(
'red',
` (>${(timeoutLimit / 1000).toFixed(1)}s)`,
)
} else if (timeSpent >= timeoutLimit / 2) {
row += styleText(
'yellow',
` (${(timeSpent / 1000).toFixed(2)}s)`,
)
} else {
row += styleText(
'green',
` (${(timeSpent / 1000).toFixed(2)}s)`,
)
}
}
console.log(row)
}
}