Files
upstream-mirror a774ac673a
Some checks failed
Deploy analytics worker / Deploy analytics worker to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
i18n upload strings / Upload latest strings (push) Failing after 1s
Deploy bemo / Deploy bemo to ${{ (github.ref == 'refs/heads/bemo-production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
Deploy grafana resources / ${{ github.event_name == 'push' && 'Deploy grafana resources' || 'Validate grafana resources (dry run)' }} (push) Failing after 0s
End to end tests (dotcom) / Check for relevant changes (push) Failing after 0s
End to end tests (examples) / Check for relevant changes (push) Failing after 0s
End to end tests (mermaid readability) / Check for relevant changes (push) Failing after 1s
End to end tests (perf) / Check for relevant changes (push) Failing after 0s
Publish editor extensions / Publish editor extensions (push) Failing after 1s
Publish SDK packages / Remove the publish-packages label (push) Has been skipped
Checks / Tests & checks (push) Has been cancelled
Checks / Build all projects (push) Has been cancelled
Deploy templates / Deploy templates (push) Has been cancelled
End to end tests (dotcom) / End to end tests (dotcom) (push) Has been cancelled
End to end tests (mermaid readability) / End to end tests (mermaid readability) (push) Has been cancelled
End to end tests (perf) / End to end tests (perf) (push) Has been cancelled
Deploy .com / Deploy dotcom to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Has been cancelled
End to end tests (examples) / End to end tests (examples) (push) Has been cancelled
Publish SDK packages / Publish SDK packages (push) Has been cancelled
Publish SDK packages / Tell user released version (push) Has been cancelled
Publish SDK packages / Publishes code templates to separate repositories (push) Has been cancelled
Publish SDK packages / Deploys the template demos with the new SDK version (push) Has been cancelled
snapshot of https://github.com/tldraw/tldraw.git (main, 2026-09-23; blobs > 131072B omitted)
2026-09-23 16:12:52 +05:00

98 lines
3.4 KiB
TypeScript

import { execSync } from 'child_process'
import { copyFileSync, existsSync, readFileSync, writeFileSync } from 'fs'
import path from 'path'
import { pathToFileURL } from 'url'
import { glob } from 'glob'
import { generateTldrawPackageDocs } from './generate-tldraw-package-docs'
import { nicelog } from './lib/nicelog'
function markGeneratedFile(sourcePackageDir: string, fileName: string) {
const filePath = path.join(sourcePackageDir, fileName)
if (existsSync(filePath)) {
copyFileSync(filePath, `${filePath}.bak`)
} else {
writeFileSync(`${filePath}.generated`, '')
}
}
/** Prepares the package for publishing. the tarball in case it will be written to disk. */
export async function preparePackage({ sourcePackageDir }: { sourcePackageDir: string }) {
if (!existsSync(path.join(sourcePackageDir, 'src/index.ts'))) {
throw new Error(`No src/index.ts file found in '${sourcePackageDir}'!`)
}
const manifest = JSON.parse(readFileSync(path.join(sourcePackageDir, 'package.json'), 'utf8'))
const packageName = manifest.name ?? path.basename(sourcePackageDir)
const startTime = Date.now()
nicelog(`[prepack] ${packageName} starting...`)
execSync('yarn run -T lazy build', { cwd: sourcePackageDir, stdio: 'inherit' })
// save package.json and reinstate it in postpack
copyFileSync(
path.join(sourcePackageDir, 'package.json'),
path.join(sourcePackageDir, 'package.json.bak')
)
const cssFiles = glob.sync(path.join(sourcePackageDir, '*.css'))
// Include DOCS.md in the published tarball when present. npm auto-includes
// README.md and LICENSE but not DOCS.md, so we have to add it explicitly.
const extraFiles = new Set<string>()
const docsPath = path.join(sourcePackageDir, 'DOCS.md')
if (existsSync(docsPath)) {
extraFiles.add('DOCS.md')
}
if (packageName === 'tldraw') {
markGeneratedFile(sourcePackageDir, 'DOCS.md')
markGeneratedFile(sourcePackageDir, 'RELEASE_NOTES.md')
generateTldrawPackageDocs({ sourcePackageDir })
extraFiles.add('DOCS.md')
extraFiles.add('RELEASE_NOTES.md')
}
// construct the final package.json
// eslint-disable-next-line no-restricted-globals
const newManifest = structuredClone({
// filter out comments
...Object.fromEntries(
Object.entries(manifest).filter(([key]) => !key.startsWith('/*') && key !== 'types')
),
main: 'dist-cjs/index.js',
module: 'dist-esm/index.mjs',
source: 'src/index.ts',
exports: {
'.': {
import: './dist-esm/index.mjs',
require: './dist-cjs/index.js',
},
...Object.fromEntries(
cssFiles.map((file) => [`./${path.basename(file)}`, `./${path.basename(file)}`])
),
},
files: [...(manifest.files ?? []), 'dist-esm', 'dist-cjs', 'src', ...extraFiles],
type: undefined,
})
writeFileSync(
path.join(sourcePackageDir, 'package.json'),
JSON.stringify(newManifest, null, `\t`)
)
// GOTCHA: Yarn's pack command seems to have a race condition where it doesn't reliably pick up
// files, adding a tiny delay seems to fix it, but we make the delay extra long here just to be
// safe.
await new Promise((resolve) => setTimeout(resolve, 1000))
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1)
nicelog(`[prepack] ${packageName} done (${elapsed}s)`)
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
// module was called directly
;(async () => {
await preparePackage({
sourcePackageDir: path.resolve(path.normalize(process.argv[2] ?? process.cwd())),
})
})()
}