tldraw/scripts/type-gen.js
2021-06-25 11:28:52 +01:00

85 lines
2.1 KiB
JavaScript

// @ts-check
/*
This script will generate TypeScript content for the code editor. It inlines
the content of several files into one large string which can be passed to the
Monaco editor as an extraLib.
Important notes:
- Files must include the "Start Copy Here" comment indicated below.
- This comment must be placed BELOW any import statements.
Run the script with `yarn scripts`.
*/
const fs = require('fs/promises')
const root = process.cwd()
async function inlineFileContents(path) {
console.log(`📄 Inlining contents of ${path}`)
const text = await fs.readFile(`${root}${path}`, 'utf-8')
return text
.match(
/\/\* ----------------- Start Copy Here ---------------- \*\/(.|\n)*$/g
)[0]
.replaceAll('/* ----------------- Start Copy Here ---------------- */', '')
.replaceAll('export default', '')
.replaceAll('export ', '')
}
async function copyTypesToFile() {
console.log('⚙️ Generating types-import.ts')
const content =
`
// HEY! DO NOT MODIFY THIS FILE. THE CONTENTS OF THIS FILE
// ARE AUTO-GENERATED BY A SCRIPT AT: /scripts/type-gen.js
// ANY CHANGES WILL BE LOST WHEN THE SCRIPT RUNS AGAIN!
export default {` +
`
name: "types.ts",
content: \`
${await inlineFileContents('/types.ts')}
${await inlineFileContents('/utils/vec.ts')}
${await inlineFileContents('/state/code/utils.ts')}
${await inlineFileContents('/state/code/index.ts')}
${await inlineFileContents('/state/code/dot.ts')}
${await inlineFileContents('/state/code/ellipse.ts')}
${await inlineFileContents('/state/code/line.ts')}
${await inlineFileContents('/state/code/polyline.ts')}
${await inlineFileContents('/state/code/ray.ts')}
${await inlineFileContents('/state/code/arrow.ts')}
${await inlineFileContents('/state/code/draw.ts')}
${await inlineFileContents('/state/code/rectangle.ts')}
${await inlineFileContents('/state/code/control.ts')}
declare const controls: {[key:string]: any} = {}
\`}`
await fs.writeFile(
__dirname + '/../components/code-panel/types-import.ts',
content
)
console.log('✅ Process complete')
}
// Kickoff
copyTypesToFile()