generate-tailwind-classes.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const fs = require('fs')
  2. const color = require('./../../build/css/tw-extend/color')
  3. /**
  4. *
  5. * Generates a JS file with all the custom tailwind color classes
  6. *
  7. * @param {string} file
  8. * Name of file to be created
  9. */
  10. function writeJsFile(file: string) {
  11. const backgrounds = Object.keys(color).filter((x) => x.includes('background-'))
  12. const borders = Object.keys(color).filter((x) => x.includes('border-'))
  13. const texts = Object.keys(color).filter((x) => x.includes('foreground-'))
  14. const colors = Object.keys(color).filter(
  15. (x) =>
  16. !x.includes('foreground-') &&
  17. !x.includes('background-') &&
  18. !x.includes('border-') &&
  19. !x.includes('colors-') &&
  20. !x.includes('variables-')
  21. )
  22. const palletes = Object.keys(color).filter((x) => x.includes('colors-'))
  23. // console.log('Example tailwind classes: ')
  24. function santizieDefaults(x: string) {
  25. // console.log(x)
  26. let value = x
  27. value = value.replace('-DEFAULT', '')
  28. value = value.replace('background', 'bg')
  29. value = value.replace('foreground', 'text')
  30. value = value.replace('colors-', 'bg-')
  31. value = value.toLowerCase()
  32. return value
  33. }
  34. const result = {
  35. background: [...backgrounds.map((x) => santizieDefaults(x))],
  36. border: [...borders.map((x) => santizieDefaults(x))],
  37. text: [...texts.map((x) => santizieDefaults(x))],
  38. colors: [...colors.map((x) => `bg-${santizieDefaults(x)}`)],
  39. palletes: [...palletes.map((x) => santizieDefaults(x))],
  40. }
  41. const fileContent = `export default ${JSON.stringify(result, null, 2)};\n`
  42. fs.writeFileSync(file, fileContent)
  43. console.log('saved example color classes to : ', file)
  44. }
  45. writeJsFile('./src/lib/tailwind-demo-classes.ts')
  46. // example output
  47. // ./src/lib/tailwind-demo-classes.js
  48. // module.exports = {
  49. // "background": [
  50. // "bg",
  51. // "bg-selection",
  52. // //..
  53. // ],
  54. // "border": [
  55. // "border",
  56. // "border-muted",
  57. // //..
  58. // ],
  59. // "text": [
  60. // "text",
  61. // "text-strong",
  62. // //..
  63. // ],
  64. // "colors": [
  65. // "bg-destructive-200",
  66. // "bg-destructive-300",
  67. // //..
  68. // ],
  69. // "palletes": [
  70. // "bg-black",
  71. // "bg-white",
  72. // //..
  73. // ]
  74. // }