|
| 1 | +import path from 'path'; |
| 2 | +import { SchemaType } from '@eva-design/dss'; |
| 3 | +import { writeFileSync } from 'fs'; |
| 4 | + |
| 5 | +interface RawSchemaDefinitions { |
| 6 | + [controlName: string]: RawControlDefinitions; |
| 7 | +} |
| 8 | + |
| 9 | +interface RawControlDefinitions { |
| 10 | + [parameterName: string]: 'string' | 'number'; |
| 11 | +} |
| 12 | + |
| 13 | +const scriptArguments: string[] = process.argv.splice(2); |
| 14 | + |
| 15 | +if (scriptArguments.length === 0) { |
| 16 | + console.error('No specified mapping package.\n'); |
| 17 | + process.exit(1); |
| 18 | +} |
| 19 | + |
| 20 | +const { [0]: packageNameArgument } = scriptArguments; |
| 21 | + |
| 22 | +main(); |
| 23 | + |
| 24 | +function main() { |
| 25 | + const schemaPackagePath: string = path.resolve('packages', packageNameArgument); |
| 26 | + const schemaDefinitionsFilePath: string = `${schemaPackagePath}/${packageNameArgument}.d.ts`; |
| 27 | + |
| 28 | + const schema: SchemaType = require(`${schemaPackagePath}/mapping.json`); |
| 29 | + |
| 30 | + const rawSchemaDefinitions: RawSchemaDefinitions = createRawDefinitionsForSchema(schema); |
| 31 | + const writableSchemaDefinitions: string = createWritableSchemaDefinitionsFromRawSource(rawSchemaDefinitions); |
| 32 | + |
| 33 | + writeFileSync(schemaDefinitionsFilePath, writableSchemaDefinitions); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * @returns object with control definitions |
| 38 | + * key - control name, |
| 39 | + * value `raw` definition of control parameters |
| 40 | + * |
| 41 | + * e.g { Button: { backgroundColor: 'string', ... } } |
| 42 | + */ |
| 43 | +function createRawDefinitionsForSchema(schema: SchemaType): RawSchemaDefinitions { |
| 44 | + return Object.keys(schema.components).reduce((acc: RawSchemaDefinitions, controlName: string) => { |
| 45 | + const controlDefinitions = createRawDefinitionsForControl(schema, controlName); |
| 46 | + return { ...acc, [controlName]: controlDefinitions }; |
| 47 | + }, {}); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * @returns object with control definitions |
| 52 | + * key - parameter name, |
| 53 | + * value `raw` definition parameter |
| 54 | + * |
| 55 | + * e.g { backgroundColor: 'string', ... } |
| 56 | + */ |
| 57 | +function createRawDefinitionsForControl(schema: SchemaType, controlName: string): RawControlDefinitions { |
| 58 | + const { [controlName]: componentMapping } = schema.components; |
| 59 | + |
| 60 | + return Object.keys(componentMapping.meta.parameters).reduce((acc: RawControlDefinitions, parameterKey: string) => { |
| 61 | + const { [parameterKey]: parameterMeta } = componentMapping.meta.parameters; |
| 62 | + return { ...acc, [parameterKey]: parameterMeta.type }; |
| 63 | + }, {}); |
| 64 | +} |
| 65 | + |
| 66 | +function createWritableSchemaDefinitionsFromRawSource(rawSource: RawSchemaDefinitions): string { |
| 67 | + return Object.keys(rawSource).reduce((acc: string, controlName: string): string => { |
| 68 | + const controlDefinitionsOutput = createWritableControlDefinitionsFromRawSource(rawSource, controlName); |
| 69 | + return acc.concat(controlDefinitionsOutput); |
| 70 | + }, ''); |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * @returns raw control definitions mapped to TypeScript interfaces |
| 75 | + * |
| 76 | + * e.g { Button: { backgroundColor: 'string', ... } } |
| 77 | + * -> `export interface ButtonParameters { backgroundColor: string; ... }` |
| 78 | + */ |
| 79 | +function createWritableControlDefinitionsFromRawSource(rawSource: RawSchemaDefinitions, controlName: string): string { |
| 80 | + const { [controlName]: rawControlDefinitions } = rawSource; |
| 81 | + |
| 82 | + const jsonDefinitions: string = JSON.stringify(rawControlDefinitions, null, 2); |
| 83 | + const noQuotesDefinitions: string = jsonDefinitions.replace(/["']/g, ''); |
| 84 | + const writableDefinitions: string = noQuotesDefinitions.replace(/,/g, ';'); |
| 85 | + |
| 86 | + return `export interface ${controlName}Parameters ${writableDefinitions}`; |
| 87 | +} |
| 88 | + |
0 commit comments