import { copyFileSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, } from 'node:fs'; import {tmpdir} from 'node:os'; import path from 'node:path'; import {$} from 'bun'; import limit from 'p-limit'; import {FEATURED_TEMPLATES} from './packages/create-video/src/templates'; import {shouldReleasePackage} from './packages/studio-shared/src/release-package-policy'; const p = limit(4); const args = process.argv.slice(2); const listOnly = args.includes('--list'); const checkOnly = args.includes('--check'); const tagArg = args.find((arg) => arg.startsWith('--tag=')); const releaseTag = tagArg?.slice('--tag='.length) ?? null; const onlyArg = args.find((arg) => arg.startsWith('--only=')); const onlyPackage = onlyArg?.slice('--only='.length) ?? null; if ( args.some( (arg) => arg !== '--list' && arg !== '--check' && arg !== tagArg && arg !== onlyArg, ) || (listOnly && (checkOnly || releaseTag !== null || onlyPackage !== null)) || (checkOnly && releaseTag !== null) || (onlyPackage !== null && releaseTag === null && !checkOnly) || releaseTag === '' || onlyPackage === '' ) { throw new Error( 'Usage: bun publish.ts [--list | --check [--only=] | --tag= [--only=]]', ); } const releaseVersion = JSON.parse( readFileSync( path.join(process.cwd(), 'packages', 'core', 'package.json'), 'utf-8', ), ).version as string; const dirs = readdirSync('packages') .filter((dir) => lstatSync(path.join(process.cwd(), 'packages', dir)).isDirectory(), ) .filter((dir) => existsSync(path.join(process.cwd(), 'packages', dir, 'package.json')), ); const packagesToPublish: { dir: string; name: string; hasLicense: boolean; hasNativeTypescriptBuild: boolean; hasPrepublishOnly: boolean; packagePath: string; }[] = []; let foundOnlyPackage = false; for (const dir of dirs) { const localTemplates = FEATURED_TEMPLATES.map( (t) => t.templateInMonorepo, ).filter(Boolean) as string[]; if (localTemplates.includes(dir)) { continue; } const packagePath = path.join(process.cwd(), 'packages', dir); const packageJsonPath = path.join(packagePath, 'package.json'); const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); if (packageJson.private) { continue; } if ( !shouldReleasePackage({ packageName: packageJson.name, releaseVersion, }) ) { continue; } if (onlyPackage !== null && packageJson.name !== onlyPackage) { continue; } foundOnlyPackage = true; if (listOnly) { console.log(packageJson.name); continue; } packagesToPublish.push({ dir, name: packageJson.name, hasLicense: packageJson.license === 'SEE LICENSE IN LICENSE.md', hasNativeTypescriptBuild: /\b(?:tsc|tsrs)\b/.test( packageJson.scripts?.make ?? '', ), hasPrepublishOnly: typeof packageJson.scripts?.prepublishOnly === 'string', packagePath, }); } if (onlyPackage !== null && !foundOnlyPackage) { throw new Error(`No releasable package named ${onlyPackage}`); } if (!listOnly) { const packedDir = mkdtempSync(path.join(tmpdir(), 'remotion-publish-')); const validatedTarballs = new Map(); try { const preflightResults = await Promise.allSettled( packagesToPublish.map( ({ dir, name, hasLicense, hasNativeTypescriptBuild, hasPrepublishOnly, packagePath, }) => p(async () => { const licensePath = path.join(packagePath, 'LICENSE.md'); const copiedLicense = hasLicense && !existsSync(licensePath); const stagingDir = path.join(packedDir, dir); mkdirSync(stagingDir); try { if (copiedLicense) { copyFileSync( path.join(process.cwd(), 'LICENSE.md'), licensePath, ); } // Publishing a tarball skips prepublishOnly, so run it before packing. if (hasPrepublishOnly) { await $`bun run prepublishOnly`.cwd(packagePath); } await $`bun pm pack --destination ${stagingDir} --quiet`.cwd( packagePath, ); const tarballName = readdirSync(stagingDir).find((file) => file.endsWith('.tgz'), ); if (!tarballName) { throw new Error(`No tarball created for ${name}`); } const tarball = path.join(stagingDir, tarballName); await $`tar -xzf ${tarball} -C ${stagingDir}`.quiet(); const packedPackagePath = path.join(stagingDir, 'package'); const packedPackageJson = JSON.parse( readFileSync( path.join(packedPackagePath, 'package.json'), 'utf-8', ), ); if ( hasLicense || packedPackageJson.license === 'SEE LICENSE IN LICENSE.md' ) { const packedLicensePath = path.join( packedPackagePath, 'LICENSE.md', ); if ( !existsSync(packedLicensePath) || !lstatSync(packedLicensePath).isFile() || readFileSync(packedLicensePath, 'utf-8').trim().length === 0 ) { throw new Error( `Missing or empty LICENSE.md in ${name} tarball`, ); } } if (hasNativeTypescriptBuild) { const distDir = path.join(packedPackagePath, 'dist'); const jsFiles = existsSync(distDir) ? readdirSync(distDir, {recursive: true}).filter((file) => /\.(c|m)?js$/.test(file), ) : []; if (jsFiles.length === 0) { throw new Error(`No compiled JavaScript in ${name}`); } const emptyFiles = jsFiles.filter( (file) => statSync(path.join(distDir, file)).size === 0, ); if (emptyFiles.length > 0) { throw new Error( `Empty compiled JavaScript in ${name}: ${emptyFiles.join(', ')}`, ); } } validatedTarballs.set(name, tarball); } finally { if (copiedLicense) { rmSync(licensePath, {force: true}); } } }), ), ); const preflightFailure = preflightResults.find( (result) => result.status === 'rejected', ); if (preflightFailure?.status === 'rejected') { throw preflightFailure.reason; } if (checkOnly) { console.log(`Validated ${validatedTarballs.size} packages`); } else { const publishResults = await Promise.allSettled( packagesToPublish.map(({name, packagePath}) => p(async () => { const tarball = validatedTarballs.get(name); if (!tarball) { throw new Error(`No validated tarball for ${name}`); } if (releaseTag === null) { await $`bun publish ${tarball} --tolerate-republish`.cwd( packagePath, ); } else { await $`bun publish ${tarball} --tag=${releaseTag}`.cwd( packagePath, ); } }), ), ); const publishFailure = publishResults.find( (result) => result.status === 'rejected', ); if (publishFailure?.status === 'rejected') { throw publishFailure.reason; } } } finally { rmSync(packedDir, {recursive: true, force: true}); } }