-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcode-snippet-generators.ts
106 lines (91 loc) · 3.58 KB
/
code-snippet-generators.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
import camelCase from 'lodash.camelcase';
import kebabCase from 'lodash.kebabcase';
import snakeCase from 'lodash.snakecase';
import { SvgDefinition } from '../converters/shared.converter';
import { FileConversionOptions, ConstantsConversionOptions } from '../options/conversion-options';
export enum Delimiter {
CAMEL = 'CAMEL',
KEBAB = 'KEBAB',
SNAKE = 'SNAKE',
UPPER = 'UPPER'
}
export const generateInterfaceDefinition = (conversionOptions: FileConversionOptions | ConstantsConversionOptions) => {
const iconNameType =
conversionOptions.generateType || conversionOptions.generateTypeObject ? conversionOptions.typeName : 'string';
return `export interface ${conversionOptions.interfaceName}{
name: ${conversionOptions.generateType ? iconNameType : 'string'};
data: string;}`;
};
export const generateTypeDefinition = (
conversionOptions: FileConversionOptions | ConstantsConversionOptions,
svgDefinitions: SvgDefinition[]
): string => {
let typesDefinition = '';
if (conversionOptions.generateType) {
typesDefinition += `
export type ${conversionOptions.typeName} = ${svgDefinitions
.map(({ typeName }, index) => `'${typeName}'${index === svgDefinitions.length - 1 ? '' : ' | '}`)
.join('')};`;
}
if (conversionOptions.generateTypeObject) {
typesDefinition += `
export const ${conversionOptions.typeName} = {
${svgDefinitions
.map(
({ typeName }, index) =>
`'${typeName}': '${typeName}'${conversionOptions.generateType ? ` as ${conversionOptions.typeName}` : ''}${
index === svgDefinitions.length - 1 ? '' : ','
}`
)
.join('')}
};`;
}
return typesDefinition;
};
export const generateSvgConstant = (variableName: string, filenameWithoutEnding: string, data: string): string => {
return `export const ${variableName}: {
name: '${filenameWithoutEnding}',
data: string
} = {
name: '${filenameWithoutEnding}',
data: \`${data}\`
};`;
};
export const generateExportStatement = (fileName: string, generatedIconsFolderName?: string): string => {
if (generatedIconsFolderName) {
return `export * from './${generatedIconsFolderName}/${fileName}';`;
}
return `export * from './${fileName}';`;
};
export const generateNamedImportStatement = (name: string, module: string): string =>
`import {${name}} from '${module}';\n`;
export const generateTypeName = (filenameWithoutEnding, delimiter: Delimiter): string => {
if (delimiter === Delimiter.CAMEL) {
return `${camelCase(filenameWithoutEnding)}`;
}
if (delimiter === Delimiter.KEBAB) {
return `${kebabCase(filenameWithoutEnding)}`;
}
if (delimiter === Delimiter.UPPER) {
return `${snakeCase(filenameWithoutEnding).toUpperCase()}`;
}
return `${snakeCase(filenameWithoutEnding)}`;
};
export const generateVariableName = (prefix: string, filenameWithoutEnding): string => {
const camelCased = camelCase(filenameWithoutEnding);
return prefix ? `${prefix}${capitalize(camelCased)}` : camelCased;
};
export const generateTypeHelper = (interfaceName: string): string => `
export type IconNameSubset<T extends Readonly<${interfaceName}[]>> = T[number]['name'];
`;
export const generateTypeHelperWithImport = (
interfaceName: string,
iconsFolderName: string,
modelFileName: string
): string => `
import {${interfaceName}} from './${iconsFolderName}/${modelFileName}';
${generateTypeHelper(interfaceName)}
`;
const capitalize = (value: string): string => {
return value.charAt(0).toUpperCase() + value.slice(1);
};