|
1 | 1 | import type FontConfig from '../classes/FontConfig'; |
| 2 | +import type { FontOptionVariance } from '../types'; |
2 | 3 |
|
3 | | -export function getTypesContent(fontConfig: FontConfig) { |
4 | | - const family = unique(fontConfig.fonts.map(font => font.family)); |
5 | | - const weight = unique( |
6 | | - fontConfig.fonts |
7 | | - .map(font => font.variances.map(variance => variance.weight)) |
8 | | - .flat() |
9 | | - ); |
10 | | - const style = unique( |
11 | | - fontConfig.fonts |
12 | | - .map(font => font.variances.map(variance => variance.style)) |
13 | | - .flat() |
14 | | - ); |
15 | | - |
16 | | - return generateAliases({ family, weight, style }); |
17 | | -} |
18 | | - |
19 | | -function unique<T>(array: T[]) { |
20 | | - return Array.from(new Set(array)); |
21 | | -} |
22 | | - |
23 | | -function generateAliases({ |
24 | | - family, |
25 | | - weight, |
26 | | - style |
27 | | -}: { |
| 4 | +interface TextProperties { |
| 5 | + [key: string]: (string | number)[] |
28 | 6 | family: string[]; |
29 | 7 | weight: (string | number)[]; |
30 | 8 | style: string[]; |
31 | | -}) { |
32 | | - return ` |
33 | | - export type FontFamily = ${family.map(family => `'${family}'`).join(' | ') || 'string'}; |
34 | | - export type FontWeight = ${weight.map(v => (typeof v === 'number' ? Number(v) : `"${v}"`)).join(' | ') || 'string'}; |
35 | | - export type FontStyle = ${style.map(style => `'${style}'`).join(' | ') || 'string'}; |
36 | | - `; |
37 | 9 | } |
38 | 10 |
|
39 | | -// import ts from 'typescript'; |
40 | | -// function generateAliases({ |
41 | | -// family, |
42 | | -// weight, |
43 | | -// style |
44 | | -// }: { |
45 | | -// family: string[]; |
46 | | -// weight: (string | number)[]; |
47 | | -// style: string[]; |
48 | | -// }) { |
49 | | -// const sourceFile = ts.createSourceFile( |
50 | | -// 'index.d.ts', |
51 | | -// '', |
52 | | -// ts.ScriptTarget.ESNext, |
53 | | -// false, |
54 | | -// ts.ScriptKind.TS |
55 | | -// ); |
| 11 | +function getFromVariances<T>(fontConfig: FontConfig, key: keyof FontOptionVariance): T[] { |
| 12 | + const uniqueStyles = new Set<T>() |
| 13 | + const fontCount = fontConfig.fonts.length |
56 | 14 |
|
57 | | -// const updatedSourceFile = ts.factory.updateSourceFile(sourceFile, [ |
58 | | -// createAliasDeclaration('FontFamily', family), |
59 | | -// createAliasDeclaration('FontWeigth', weight), |
60 | | -// createAliasDeclaration('FontStyle', style) |
61 | | -// ]); |
| 15 | + for (let i = 0; i < fontCount; i++) { |
| 16 | + const values = fontConfig.fonts[i].variances |
| 17 | + for (let j = 0, valueCount = values.length; j < valueCount; j++) { |
| 18 | + uniqueStyles.add(values[j][key] as T) |
| 19 | + } |
| 20 | + } |
62 | 21 |
|
63 | | -// const printer = ts.createPrinter(); |
64 | | -// return printer.printFile(updatedSourceFile); |
65 | | -// } |
| 22 | + return Array.from(uniqueStyles).filter(Boolean) |
| 23 | +} |
66 | 24 |
|
67 | | -// function createAliasDeclaration(name: string, values: (string | number)[]) { |
68 | | -// if (values.length === 0) { |
69 | | -// values = ['any']; |
70 | | -// } |
71 | | -// return ts.factory.createTypeAliasDeclaration( |
72 | | -// [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], |
73 | | -// name, |
74 | | -// undefined, |
75 | | -// ts.factory.createUnionTypeNode( |
76 | | -// values.map(value => { |
77 | | -// let literal; |
78 | | -// if (typeof value === 'number') { |
79 | | -// literal = ts.factory.createNumericLiteral(String(value)); |
80 | | -// } else { |
81 | | -// literal = ts.factory.createStringLiteral(String(value)); |
82 | | -// } |
83 | | -// return ts.factory.createLiteralTypeNode(literal); |
84 | | -// }) |
85 | | -// ) |
86 | | -// ); |
87 | | -// } |
| 25 | +function unique<T>(array: T[]): T[] { |
| 26 | + return Array.from(new Set(array)).filter(Boolean); |
| 27 | +} |
| 28 | + |
| 29 | +function capitalize(text: string): string { |
| 30 | + return text ? `${text.charAt(0).toUpperCase()}${text.slice(1)}` : text; |
| 31 | +} |
| 32 | + |
| 33 | +function generateAliases(textProperties: TextProperties): string { |
| 34 | + const props = Object.keys(textProperties); |
| 35 | + const len = props.length; |
| 36 | + const output: string[] = new Array(len); |
| 37 | + // Loop through each text property |
| 38 | + for (let i = 0; i < len; i++) { |
| 39 | + const prop = props[i]; |
| 40 | + const propValues: (string | number)[] = textProperties[props[i]]; |
| 41 | + const valueCount = propValues.length; |
| 42 | + let line = `export type Font${capitalize(prop)} = `; |
| 43 | + // Output generic type "string", if there are no values to parse |
| 44 | + if (!valueCount) output[i] = `${line}'string';`; |
| 45 | + else { |
| 46 | + // Merge all values into a union (|) type value. |
| 47 | + const values: string[] = new Array(valueCount); |
| 48 | + for (let j = 0; j < valueCount; j++) { |
| 49 | + values[j] = `'${propValues[j]}'`; |
| 50 | + } |
| 51 | + output[i] = `${line}${values.join(' | ')};`; |
| 52 | + } |
| 53 | + } |
| 54 | + return output.join('\n'); |
| 55 | +} |
| 56 | + |
| 57 | +export function getTypesContent(fontConfig: FontConfig): string { |
| 58 | + return generateAliases({ |
| 59 | + family: unique(fontConfig.fonts.map(font => font.family)), |
| 60 | + weight: getFromVariances(fontConfig, 'weight'), |
| 61 | + style: getFromVariances(fontConfig, 'style') |
| 62 | + }); |
| 63 | +} |
0 commit comments