This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collect.cli.ts
111 lines (108 loc) · 3.03 KB
/
collect.cli.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
import {
Command,
InvalidArgumentError,
Option,
} from 'commander';
import { collect } from '$commands/collect/collect';
import {
keyTransformCliToFunc,
defaultCollectOptions,
defaultKeyTransformCli,
} from '$commands/collect/collect.constants';
import type {
CollectOptions,
KeyTransformFn,
} from '$commands/collect/collect.types';
import { KeyTransformCli } from '$commands/collect/collect.types';
import {
commaSeparatedList,
number,
} from '$utils/parser';
export function collectCli() {
return new Command('collect')
.description('collect route data')
.option(
'-i, --inDir <path>',
'directory path to collect',
defaultCollectOptions.inDir
)
.option(
'-e, --extensions <list>',
'comma-separated list of file extensions to collect',
commaSeparatedList,
defaultCollectOptions.extensions,
)
.option(
'-x, --ignorePatterns <list>',
'comma-separated list of filename patterns (string literal or regex) to ignore',
commaSeparatedList,
defaultCollectOptions.ignorePatterns,
)
.option(
'--no-output',
'don\'t write to disk but print to console instead',
defaultCollectOptions.output,
)
.option(
'-o, --outDir <path>',
'outputs generated files to this folder',
defaultCollectOptions.outDir,
)
.option(
'-d, --depth <number>',
'depth of inDir to collect',
number,
defaultCollectOptions.depth,
)
.addOption(
new Option( '-t, --keyTransform <variant>', 'how to transform route key')
.default([])
.argParser<KeyTransformFn[]>((value, previous) => {
if (!(value in KeyTransformCli)) {
throw new InvalidArgumentError(`Must be one of: ${Object.keys(KeyTransformCli).join(', ')}`);
}
const valued = keyTransformCliToFunc[KeyTransformCli[value] as KeyTransformCli];
return [...previous, valued];
}),
)
.option(
'-k, --dirkey <name>',
'how to transform route key',
defaultCollectOptions.dirkey,
)
.option(
'--no-utils',
'don\'t generate utils for building path with arguments',
defaultCollectOptions.utils,
)
.option(
'--no-typescript',
'outputs files in javascript instead of typescript',
defaultCollectOptions.typescript,
)
.option(
'--verbose',
'prints more info during operation',
defaultCollectOptions.verbose,
)
.action((options: CollectOptions) => {
if (options.keyTransform.length === 0) {
options.keyTransform = defaultKeyTransformCli.map((k) => keyTransformCliToFunc[k]);
}
if (options.verbose) {
console.info('Running "collect" with options:');
console.table(Object.fromEntries(
Object.entries(options).map(
([key, value]) => ([
key,
{ value },
])
)),
);
}
const output = collect(options);
if (!options.output) {
console.log(output);
}
});
}