-
Notifications
You must be signed in to change notification settings - Fork 69
/
flags.ts
155 lines (133 loc) · 5.39 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
151
152
153
154
155
// tslint:disable interface-over-type-literal
import {URL} from 'url'
import {Definition, OptionFlag, BooleanFlag, Default} from '../interfaces'
import * as fs from 'fs'
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, _: 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>
}
type IntegerFlagOptions = Partial<OptionFlag<number>> & {
min?: number;
max?: number;
}
export function integer(opts: IntegerFlagOptions & {multiple: true} & ({required: true} | { default: Default<number[]> })): OptionFlag<number[]>
export function integer(opts: IntegerFlagOptions & {multiple: true}): OptionFlag<number[] | undefined>
export function integer(opts: IntegerFlagOptions & ({required: true} | { default: Default<number> })): OptionFlag<number>
export function integer(opts?: IntegerFlagOptions): OptionFlag<number | undefined>
export function integer(opts: IntegerFlagOptions = {}): OptionFlag<number> | OptionFlag<number[]> | OptionFlag<number | undefined> | OptionFlag<number[] | undefined> {
return build({
...opts,
// parse thinks it needs an array to be returned, which is not the case
parse: async input => {
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 (opts.parse ? opts.parse(input, 1) : num)
},
})()
}
type DirectoryFlagOptions = Partial<OptionFlag<string>> & {
exists?: boolean;
}
export function directory(opts: DirectoryFlagOptions & {multiple: true} & ({required: true} | { default: Default<string[]> })): OptionFlag<string[]>
export function directory(opts: DirectoryFlagOptions & {multiple: true}): OptionFlag<string[] | undefined>
export function directory(opts: DirectoryFlagOptions & ({required: true} | { default: Default<string> })): OptionFlag<string>
export function directory(opts?: DirectoryFlagOptions): OptionFlag<string | undefined>
export function directory(opts: DirectoryFlagOptions = {}): OptionFlag<string> | OptionFlag<string[]> | OptionFlag<string | undefined> | OptionFlag<string[] | undefined> {
return build<string>({
...opts,
parse: async (input: string) => {
if (opts.exists) {
// 2nd "context" arg is required but unused
return opts.parse ? opts.parse(await dirExists(input), true) : dirExists(input)
}
return opts.parse ? opts.parse(input, true) : input
},
})()
}
type FileFlagOptions = Partial<OptionFlag<string>> & {
exists?: boolean;
}
export function file(opts: FileFlagOptions & {multiple: true} & ({required: true} | { default: Default<string[]> })): OptionFlag<string[]>
export function file(opts: FileFlagOptions & {multiple: true}): OptionFlag<string[] | undefined>
export function file(opts: FileFlagOptions & ({required: true} | { default: Default<string> })): OptionFlag<string>
export function file(opts?: FileFlagOptions): OptionFlag<string | undefined>
export function file(opts: FileFlagOptions = {}): OptionFlag<string> | OptionFlag<string[]> | OptionFlag<string | undefined> | OptionFlag<string[] | undefined> {
return build<string>({
...opts,
parse: async (input: string) => {
if (opts.exists) {
// 2nd "context" arg is required but unused
return opts.parse ? opts.parse(await fileExists(input), true) : fileExists(input)
}
return opts.parse ? opts.parse(input, true) : input
},
})()
}
/**
* Initializes a string as a URL. Throws an error
* if the string is not a valid URL.
*/
export const url = build({
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 build<T>(options)()
}
const stringFlag = build({})
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
}