-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
78 lines (65 loc) · 2.49 KB
/
utils.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
import clc from 'cli-color';
import { writeFileSync } from 'fs';
import isEmpty from 'lodash/isEmpty';
import { templateOptions } from './templates';
import {
FilterOption,
SelectionPromptAnswer,
TemplateOption,
TemplateOptionAlias
} from './types';
export const logError = (msg: string) => console.log(clc.redBright(msg));
export const logAlert = (msg: string) => console.log(clc.yellowBright(msg));
export const logSuccess = (msg: string) => console.log(clc.greenBright(msg));
export const logCode = (code: string) => console.log(clc.greenBright(code));
export const logHeading = (txt: string) => console.log(clc.cyanBright(txt));
export const getDirPath = (): string => `${process.env.INIT_CWD || process.env.PWD}`;
export const parseAnswers = (answers: SelectionPromptAnswer): TemplateOptionAlias | undefined => {
return parseTemplateOption(answers.template);
};
export const parseTemplateOption = (tempOpt: string): TemplateOptionAlias | undefined => {
const betweenSquareBrackets = /\[(.+?)\]/;
const captured = tempOpt.match(betweenSquareBrackets);
return captured?.[1] as TemplateOptionAlias;
};
export const generateFile = (temp: TemplateOption, fileName?: string): void => {
if (!isEmpty(temp.alias)) {
const dirPath = getDirPath();
writeFileSync(`${dirPath}/${fileName || 'component'}.${temp.fileType}`, temp.template);
} else {
logError('No template match');
}
};
export const getFilteredTemplateOptions = (filters: FilterOption[]): TemplateOption[] => {
return templateOptions.filter((temp: TemplateOption) => {
const platform = temp.platform.toLowerCase() as FilterOption;
const shape = temp.shape.toLowerCase() as FilterOption;
const syntax = temp.syntax.toLowerCase() as FilterOption;
const file = temp.fileType.toLowerCase() as FilterOption;
if (
filters.includes(platform) ||
filters.includes(shape) ||
filters.includes(syntax) ||
filters.includes(file)
) {
return true;
}
return false;
});
};
export const diplayTemplateOptions = () => {
return templateOptions.map((opt: TemplateOption) => {
return `[${opt.alias}]: ${opt.description}`;
});
};
export const displayFilteredOptions = (temp: TemplateOption[]) => {
return temp.map((opt: TemplateOption) => {
return `[${opt.alias}]: ${opt.description}`;
});
};
export const getSearchedTemplateOption =
(temp: TemplateOptionAlias): TemplateOption | undefined => {
return templateOptions.find((t: TemplateOption) => {
return t.alias === temp;
});
};