tldraw/scripts/type-gen.js
2021-06-27 12:58:08 +01:00

110 lines
2.7 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('```', '\\`\\`\\`')
.replaceAll('export default', '')
.replaceAll('export ', '')
.replaceAll('vec.', 'Vec.')
}
async function copyTypesToFile() {
console.log('⚙️ Generating types-import.ts')
const content =
`
/* eslint-disable */
// 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: \`
type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
${await inlineFileContents('/types.ts')}
${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/text.ts')}
${await inlineFileContents('/state/code/rectangle.ts')}
${await inlineFileContents('/state/code/control.ts')}
const codeShapes = new Set<CodeShape<any>>()
const controls: Record<string, any> = {}
const defaultStyle: ShapeStyles = {
color: ColorStyle.Black,
size: SizeStyle.Medium,
isFilled: false,
dash: DashStyle.Solid,
}
const uniqueId = () => ''
const codeControls = new Set([])
declare function createShape(type: ShapeType, shape: Shape): any
declare function getShapeUtils<T>(shape: T): any
declare function getOrderedShapes(): CodeShape<any>[]
\`}`
await fs.writeFile(
__dirname + '/../components/code-panel/types-import.ts',
content
)
console.log('✅ Process complete')
}
// Kickoff
copyTypesToFile()