-
Notifications
You must be signed in to change notification settings - Fork 69
/
flags.ts
150 lines (131 loc) · 4.04 KB
/
flags.ts
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
import {URL} from 'url'
import {Definition, OptionFlag, BooleanFlag} from '../interfaces'
import * as fs from 'fs'
import {FlagParser, CustomOptionFlag} from '../interfaces/parser'
/**
* Create a custom flag.
*
* @example
* type Id = string
* type IdOpts = { startsWith: string; length: number };
*
* export const myFlag = custom<Id, IdOpts>({
* parse: async (input, opts) => {
* if (input.startsWith(opts.startsWith) && input.length === opts.length) {
* return input
* }
*
* throw new Error('Invalid id')
* },
* })
*/
export function custom<T, P>(
defaults: {parse: FlagParser<T, string, P>} & Partial<CustomOptionFlag<T, P>>,
): Definition<T, P>
export function custom<T = string, P = Record<string, unknown>>(defaults: Partial<CustomOptionFlag<T, P>>): Definition<T, P>
export function custom<T, P = Record<string, unknown>>(defaults: Partial<CustomOptionFlag<T, P>>): Definition<T, P> {
return (options: any = {}) => {
return {
parse: async (i: string, _context: any, _opts: P) => i,
...defaults,
...options,
input: [] as string[],
multiple: Boolean(options.multiple === undefined ? defaults.multiple : options.multiple),
type: 'option',
}
}
}
/**
* @deprecated Use Flags.custom instead.
*/
export function build<T>(
defaults: {parse: OptionFlag<T>['parse']} & Partial<OptionFlag<T>>,
): Definition<T>
export function build(defaults: Partial<OptionFlag<string>>): Definition<string>
export function build<T>(defaults: Partial<OptionFlag<T>>): Definition<T> {
return (options: any = {}) => {
return {
parse: async (i: string, _context: any) => i,
...defaults,
...options,
input: [] as string[],
multiple: Boolean(options.multiple === undefined ? defaults.multiple : options.multiple),
type: 'option',
}
}
}
export function boolean<T = boolean>(
options: Partial<BooleanFlag<T>> = {},
): BooleanFlag<T> {
return {
parse: async (b, _) => b,
...options,
allowNo: Boolean(options.allowNo),
type: 'boolean',
} as BooleanFlag<T>
}
export const integer = custom<number, {min?: number; max?: number;}>({
parse: async (input, ctx, opts) => {
if (!/^-?\d+$/.test(input))
throw new Error(`Expected an integer but received: ${input}`)
const num = Number.parseInt(input, 10)
if (opts.min !== undefined && num < opts.min)
throw new Error(`Expected an integer greater than or equal to ${opts.min} but received: ${input}`)
if (opts.max !== undefined && num > opts.max)
throw new Error(`Expected an integer less than or equal to ${opts.max} but received: ${input}`)
return num
},
})
export const directory = custom<string, {exists?: boolean}>({
parse: async (input, _, opts) => {
if (opts.exists) return dirExists(input)
return input
},
})
export const file = custom<string, {exists?: boolean}>({
parse: async (input, _, opts) => {
if (opts.exists) return fileExists(input)
return input
},
})
/**
* Initializes a string as a URL. Throws an error
* if the string is not a valid URL.
*/
export const url = custom<URL>({
parse: async input => {
try {
return new URL(input)
} catch {
throw new Error(`Expected a valid url but received: ${input}`)
}
},
})
export function option<T>(
options: {parse: OptionFlag<T>['parse']} & Partial<OptionFlag<T>>,
) {
return custom<T>(options)()
}
const stringFlag = custom({})
export {stringFlag as string}
export const defaultFlags = {
color: boolean({allowNo: true}),
}
const dirExists = async (input: string): Promise<string> => {
if (!fs.existsSync(input)) {
throw new Error(`No directory found at ${input}`)
}
if (!(await fs.promises.stat(input)).isDirectory()) {
throw new Error(`${input} exists but is not a directory`)
}
return input
}
const fileExists = async (input: string): Promise<string> => {
if (!fs.existsSync(input)) {
throw new Error(`No file found at ${input}`)
}
if (!(await fs.promises.stat(input)).isFile()) {
throw new Error(`${input} exists but is not a file`)
}
return input
}