-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.ts
More file actions
63 lines (58 loc) · 1.53 KB
/
cli.ts
File metadata and controls
63 lines (58 loc) · 1.53 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
import inquirer, { Question } from 'inquirer';
import glob from 'fast-glob';
import { getConfig } from './config';
inquirer.registerPrompt(
'autocomplete',
require('inquirer-autocomplete-prompt')
);
export async function showFilesChooser(
message: string,
choices: Array<string>
): Promise<Array<string>> {
const { files } = await showFilesChooserAnd(message, choices, {});
return files;
}
export async function showFilesChooserAnd<
T extends Record<string, Question>,
U extends { [key in keyof T]: T[key] },
Z extends { files: Array<string> } & U
>(message: string, choices: Array<string>, additionalQuestions: T): Promise<Z> {
const additionalQuestionsArray: Array<Question> = Object.entries(
additionalQuestions
).map(([name, question]) => ({
name,
...question,
}));
return inquirer.prompt<Z>([
{
type: 'checkbox',
name: 'files',
message,
choices,
validate: (answer: string) => {
if (answer.length < 1) {
return 'Are you tricking me 🤨? Please choose files';
}
return true;
},
},
...additionalQuestionsArray,
]);
}
export function chooseFileFromFileSystemAnd(
...additionalQuestions: Array<Question>
) {
const { exclude } = getConfig();
return inquirer.prompt([
{
type: 'autocomplete',
name: 'path',
message: 'Search by file name',
pageSize: 4,
source: (_answers, input = '*') => {
return glob(`**/*${input}*`, { ignore: exclude });
},
},
...additionalQuestions,
]);
}