You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

type-gen.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // @ts-check
  2. /*
  3. Type gen script
  4. This script will generate TypeScript declarations for the code editor. It reads
  5. the global types, as well as all of the code classes, and writes them into a
  6. single file as a string. This string is fed into the Monaco editor as an extraLib.
  7. */
  8. const fs = require('fs/promises')
  9. async function copyTypesToFile() {
  10. const types = await fs.readFile(__dirname + '/../types.ts', 'utf8')
  11. const codeIndex = await fs.readFile(
  12. __dirname + '/../state/code/index.ts',
  13. 'utf8'
  14. )
  15. const codeDot = await fs.readFile(__dirname + '/../state/code/dot.ts', 'utf8')
  16. const codeEllipse = await fs.readFile(
  17. __dirname + '/../state/code/ellipse.ts',
  18. 'utf8'
  19. )
  20. const codeLine = await fs.readFile(
  21. __dirname + '/../state/code/line.ts',
  22. 'utf8'
  23. )
  24. const codePolyline = await fs.readFile(
  25. __dirname + '/../state/code/polyline.ts',
  26. 'utf8'
  27. )
  28. const codeRay = await fs.readFile(__dirname + '/../state/code/ray.ts', 'utf8')
  29. const codeArrow = await fs.readFile(
  30. __dirname + '/../state/code/arrow.ts',
  31. 'utf8'
  32. )
  33. const codeDraw = await fs.readFile(
  34. __dirname + '/../state/code/draw.ts',
  35. 'utf8'
  36. )
  37. const codeRectangle = await fs.readFile(
  38. __dirname + '/../state/code/rectangle.ts',
  39. 'utf8'
  40. )
  41. const codeVector = await fs.readFile(__dirname + '/../utils/vec.ts', 'utf8')
  42. const codeUtils = await fs.readFile(
  43. __dirname + '/../state/code/utils.ts',
  44. 'utf8'
  45. )
  46. const content =
  47. `
  48. // HEY! DO NOT MODIFY THIS FILE. THE CONTENTS OF THIS FILE
  49. // ARE AUTO-GENERATED BY A SCRIPT AT: /scripts/type-gen.js
  50. // ANY CHANGES WILL BE LOST WHEN THE SCRIPT RUNS AGAIN!
  51. export default {` +
  52. `
  53. name: "types.ts",
  54. content: \`
  55. ${types}
  56. ${codeIndex.match(/export default(.|\n)*$/g)[0]}
  57. ${codeDot.match(/\/\*\*(.|\n)*$/g)[0]}
  58. ${codeEllipse.match(/\/\*\*(.|\n)*$/g)[0]}
  59. ${codeLine.match(/\/\*\*(.|\n)*$/g)[0]}
  60. ${codePolyline.match(/\/\*\*(.|\n)*$/g)[0]}
  61. ${codeRay.match(/\/\*\*(.|\n)*$/g)[0]}
  62. ${codeRectangle.match(/\/\*\*(.|\n)*$/g)[0]}
  63. ${codeArrow.match(/\/\*\*(.|\n)*$/g)[0]}
  64. ${codeDraw.match(/\/\*\*(.|\n)*$/g)[0]}
  65. ${codeUtils.match(/\/\*\*(.|\n)*$/g)[0]}
  66. ${codeVector}
  67. \`
  68. }`
  69. .replaceAll('export default', '')
  70. .replaceAll('export ', '')
  71. await fs.writeFile(
  72. __dirname + '/../components/code-panel/types-import.ts',
  73. content
  74. )
  75. }
  76. // Kickoff
  77. copyTypesToFile()