tldraw/packages/tlschema/scripts/new-record.js
alex 71aef1764d
Rework the assets package for strategy-specific imports (#1341)
The assets package now only exports esm-formatted .js files. There's one
for each strategy - import-based, and meta.url-based. These are directly
generated as .js and .d.ts files rather than generated as .ts and
converted to js/dts through other means.

As this package depends on esm-specific stuff to function, we don't
publish a cjs version any more.

### Change Type

<!-- 💡 Indicate the type of change your pull request is. -->
<!-- 🤷‍♀️ If you're not sure, don't select anything -->
<!-- ✂️ Feel free to delete unselected options -->

<!-- To select one, put an x in the box: [x] -->

- [ ] `patch` — Bug Fix
- [ ] `minor` — New Feature
- [x] `major` — Breaking Change

- [ ] `dependencies` — Dependency Update (publishes a `patch` release,
for devDependencies use `internal`)

- [ ] `documentation` — Changes to the documentation only (will not
publish a new version)
- [ ] `tests` — Changes to any testing-related code only (will not
publish a new version)
- [ ] `internal` — Any other changes that don't affect the published
package (will not publish a new version)

### Release Notes

- [dev] If you're using the `@tldraw/assets` package, you need to update
your code to `import { getAssetUrlsByImport } from
'@tldraw/assets/imports'` instead of `import { getBundlerAssetUrls }
from '@tldraw/assets`
2023-05-09 16:08:38 +00:00

78 lines
1.9 KiB
JavaScript

// @ts-check
/* eslint-disable */
const kleur = require('kleur')
const fs = require('fs')
const path = require('path')
const typeName = process.argv[2]
if (!typeName.match(/^TL[A-Z][a-z]+[a-zA-Z0-9]+$/)) {
console.error(
kleur.red('ERROR: Type name must start with'),
`'${kleur.bold('TL')}'`,
kleur.red('and be in'),
kleur.bold('PascalCase')
)
process.exit(1)
}
const lowerCaseName = typeName[2].toLowerCase() + typeName.slice(3)
const recordsDir = path.join(__dirname, '..', 'src', 'records')
if (!fs.existsSync(recordsDir)) {
console.error(kleur.red("ERROR: Can't find records directory at path"), recordsDir)
process.exit(1)
}
const filePath = path.join(recordsDir, `${typeName}.ts`)
if (fs.existsSync(filePath)) {
console.error(kleur.red('ERROR: File already exists at path'), filePath)
process.exit(1)
}
const snakeCaseName =
typeName[2].toLowerCase() +
typeName
.slice(3)
.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`)
.trimStart()
fs.writeFileSync(
filePath,
`import { BaseRecord, defineMigrations, createRecordType, ID } from '@tldraw/tlstore'
// --- MIGRATIONS ---
// STEP 1: Add a new version number here, give it a meaningful name.
// It should be 1 higher than the current version
const Versions = {
Initial: 0,
} as const
export const ${lowerCaseName}TypeMigrations = defineMigrations({
// STEP 2: Update the current version to point to your latest version
currentVersion: Versions.Initial,
firstVersion: Versions.Initial,
migrators: {
// STEP 3: Add an up+down migration for the new version here
},
})
/**
* ${typeName}
*/
export interface ${typeName} extends BaseRecord<'${snakeCaseName}'> {}
export const ${typeName} = createRecordType<${typeName}>('${snakeCaseName}', {
migrations: ${lowerCaseName}TypeMigrations,
}).withDefaultProperties(() => ({
}))
export type ${typeName}Id = ID<${typeName}>
`
)
console.log(kleur.green('Created new record type at path'), filePath)