forked from tailwindlabs/tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-types.js
104 lines (92 loc) · 2.76 KB
/
generate-types.js
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
import prettier from 'prettier'
import { corePlugins } from '../src/corePlugins'
import colors from '../src/public/colors'
import defaultTheme from '../src/public/default-theme'
import fs from 'fs'
import path from 'path'
import * as types from './type-utils'
fs.writeFileSync(
path.join(process.cwd(), 'types', 'generated', 'corePluginList.d.ts'),
`export type CorePluginList = ${Object.keys(corePlugins)
.map((p) => `'${p}'`)
.join(' | ')}`
)
let colorsWithoutDeprecatedColors = Object.fromEntries(
Object.entries(Object.getOwnPropertyDescriptors(colors))
.filter(([_, { value }]) => {
return typeof value !== 'undefined'
})
.map(([name, definition]) => [name, definition.value])
)
let deprecatedColors = Object.entries(Object.getOwnPropertyDescriptors(colors))
.filter(([_, { value }]) => {
return typeof value === 'undefined'
})
.map(([name, definition]) => {
let warn = console.warn
let messages = []
console.warn = (...args) => messages.push(args.pop())
definition.get()
console.warn = warn
let message = messages.join(' ').trim()
let newColor = message.match(/renamed to `(.*)`/)[1]
return `/** @deprecated ${message} */${name}: DefaultColors['${newColor}'],`
})
.join('\n')
fs.writeFileSync(
path.join(process.cwd(), 'types', 'generated', 'colors.d.ts'),
prettier.format(
`export interface DefaultColors { ${JSON.stringify(colorsWithoutDeprecatedColors).slice(
1,
-1
)}\n${deprecatedColors}\n}`,
{
semi: false,
singleQuote: true,
printWidth: 100,
parser: 'typescript',
}
)
)
const defaultThemeTypes = Object.entries(defaultTheme)
.map(([name, value]) => {
// Special cases for slightly more accurate types
if (name === 'keyframes') {
return [name, `Record<${types.forKeys(value)}, Record<string, CSSDeclarationList>>`]
}
if (name === 'fontSize') {
return [name, `Record<${types.forKeys(value)}, [string, { lineHeight: string }]>`]
}
// General cases
if (typeof value === 'string') {
return [name, `string`]
}
if (typeof value === 'function') {
return [name, null]
}
if (typeof value === 'object') {
if (Object.keys(value).length === 0) {
return [name, null]
}
return [name, types.forValue(value)]
}
return [name, `unknown`]
})
.filter(([, type]) => type !== null)
.map(([name, type]) => `${name}: ${type}`)
.join('\n')
fs.writeFileSync(
path.join(process.cwd(), 'types', 'generated', 'default-theme.d.ts'),
prettier.format(
`
type CSSDeclarationList = Record<string, string>
export type DefaultTheme = { ${defaultThemeTypes} }
`,
{
semi: false,
singleQuote: true,
printWidth: 100,
parser: 'typescript',
}
)
)