generate-types.js 2.8 KB

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