71aef1764d
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`
87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
import { readFile, writeFile as writeFileUnchecked } from 'fs/promises'
|
|
import json5 from 'json5'
|
|
import { basename, dirname, join, relative } from 'path'
|
|
import prettier from 'prettier'
|
|
import { fileURLToPath } from 'url'
|
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
const __dirname = dirname(__filename)
|
|
const isBublic = basename(join(__dirname, '../..')) === 'bublic'
|
|
export const REPO_ROOT = join(__dirname, isBublic ? '../../..' : '../..')
|
|
export const BUBLIC_ROOT = join(__dirname, '../..')
|
|
|
|
export async function readJsonIfExists(file: string) {
|
|
const fileContents = await readFileIfExists(file)
|
|
if (fileContents === null) {
|
|
return null
|
|
}
|
|
return json5.parse(fileContents)
|
|
}
|
|
|
|
export async function readFileIfExists(file: string) {
|
|
try {
|
|
return await readFile(file, 'utf8')
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
const prettierConfigPromise = prettier.resolveConfig(__dirname)
|
|
export async function writeCodeFile(
|
|
generator: string,
|
|
language: 'typescript' | 'javascript',
|
|
filePath: string,
|
|
code: string
|
|
) {
|
|
const formattedCode = prettier.format(
|
|
`
|
|
// This file is automatically generated by ${generator}.
|
|
// Do not edit manually.
|
|
|
|
${code}
|
|
`,
|
|
{
|
|
...(await prettierConfigPromise),
|
|
parser: language === 'typescript' ? 'typescript' : 'babel',
|
|
}
|
|
)
|
|
await writeStringFile(filePath, formattedCode)
|
|
}
|
|
|
|
export async function writeStringFile(filePath: string, contents: string) {
|
|
await writeFile(filePath, Buffer.from(contents, 'utf-8'))
|
|
}
|
|
|
|
export async function writeFile(filePath: string, contents: Buffer) {
|
|
if (process.env.CI) {
|
|
let existingContents: Buffer | null = null
|
|
try {
|
|
existingContents = await readFile(filePath)
|
|
} catch {
|
|
// Ignore
|
|
}
|
|
if (existingContents && !existingContents.equals(contents)) {
|
|
console.log(
|
|
`Asset file ${relative(
|
|
REPO_ROOT,
|
|
filePath
|
|
)} has changed. Please run this script again and commit the changes.`
|
|
)
|
|
console.log('Contents before:')
|
|
console.log(existingContents.toString('utf-8'))
|
|
console.log('\nContents after:')
|
|
console.log(contents.toString('utf-8'))
|
|
|
|
process.exit(1)
|
|
}
|
|
}
|
|
await writeFileUnchecked(filePath, contents, 'utf-8')
|
|
}
|
|
|
|
export async function writeJsonFile(filePath: string, contents: unknown) {
|
|
const formattedJson = prettier.format(JSON.stringify(contents, null, '\t'), {
|
|
...(await prettierConfigPromise),
|
|
parser: 'json',
|
|
})
|
|
await writeStringFile(filePath, formattedJson)
|
|
}
|