UNPKG

2.89 kBJavaScriptView Raw
1import prettier from 'prettier'
2import { corePlugins } from '../src/corePlugins'
3import colors from '../src/public/colors'
4import defaultTheme from '../src/public/default-theme'
5import fs from 'fs'
6import path from 'path'
7import * as types from './type-utils'
8
9fs.writeFileSync(
10 path.join(process.cwd(), 'types', 'generated', 'corePluginList.d.ts'),
11 `export type CorePluginList = ${Object.keys(corePlugins)
12 .map((p) => `'${p}'`)
13 .join(' | ')}`
14)
15
16let colorsWithoutDeprecatedColors = Object.fromEntries(
17 Object.entries(Object.getOwnPropertyDescriptors(colors))
18 .filter(([_, { value }]) => {
19 return typeof value !== 'undefined'
20 })
21 .map(([name, definition]) => [name, definition.value])
22)
23
24let deprecatedColors = Object.entries(Object.getOwnPropertyDescriptors(colors))
25 .filter(([_, { value }]) => {
26 return typeof value === 'undefined'
27 })
28 .map(([name, definition]) => {
29 let warn = console.warn
30 let messages = []
31 console.warn = (...args) => messages.push(args.pop())
32 definition.get()
33 console.warn = warn
34 let message = messages.join(' ').trim()
35 let newColor = message.match(/renamed to `(.*)`/)[1]
36 return `/** @deprecated ${message} */${name}: DefaultColors['${newColor}'],`
37 })
38 .join('\n')
39
40fs.writeFileSync(
41 path.join(process.cwd(), 'types', 'generated', 'colors.d.ts'),
42 prettier.format(
43 `export interface DefaultColors { ${JSON.stringify(colorsWithoutDeprecatedColors).slice(
44 1,
45 -1
46 )}\n${deprecatedColors}\n}`,
47 {
48 semi: false,
49 singleQuote: true,
50 printWidth: 100,
51 parser: 'typescript',
52 }
53 )
54)
55
56const defaultThemeTypes = Object.entries(defaultTheme)
57 .map(([name, value]) => {
58 // Special cases for slightly more accurate types
59 if (name === 'keyframes') {
60 return [name, `Record<${types.forKeys(value)}, Record<string, CSSDeclarationList>>`]
61 }
62
63 if (name === 'fontSize') {
64 return [name, `Record<${types.forKeys(value)}, [string, { lineHeight: string }]>`]
65 }
66
67 // General cases
68 if (typeof value === 'string') {
69 return [name, `string`]
70 }
71
72 if (typeof value === 'function') {
73 return [name, null]
74 }
75
76 if (typeof value === 'object') {
77 if (Object.keys(value).length === 0) {
78 return [name, null]
79 }
80
81 return [name, types.forValue(value)]
82 }
83
84 return [name, `unknown`]
85 })
86 .filter(([, type]) => type !== null)
87 .map(([name, type]) => `${name}: ${type}`)
88 .join('\n')
89
90fs.writeFileSync(
91 path.join(process.cwd(), 'types', 'generated', 'default-theme.d.ts'),
92 prettier.format(
93 `
94 import { Config } from '../../types'
95 type CSSDeclarationList = Record<string, string>
96 export type DefaultTheme = Config['theme'] & { ${defaultThemeTypes} }
97 `,
98 {
99 semi: false,
100 singleQuote: true,
101 printWidth: 100,
102 parser: 'typescript',
103 }
104 )
105)