#!/usr/bin/env bun import {randomUUID} from 'node:crypto'; import {tmpdir} from 'node:os'; import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync, } from 'node:fs'; import {basename, dirname, join, relative} from 'node:path'; import {pathToFileURL} from 'node:url'; import { addTailwindRootCss, addTailwindToConfig, } from './packages/create-video/src/add-tailwind'; import {createPublicFolder} from './packages/create-video/src/create-public-folder'; import {patchPackageJson} from './packages/create-video/src/patch-package-json'; import {patchReadmeMd} from './packages/create-video/src/patch-readme'; import {FEATURED_TEMPLATES} from './packages/create-video/src/templates'; import {localPackageRootKey} from './packages/studio-server/src/helpers/get-local-package-override'; const repositoryRoot = import.meta.dir; const agentPluginRoot = join(repositoryRoot, 'packages', 'agent-plugin'); const useOldVersion = process.argv.includes('--old'); const useCreateVideo = process.argv.includes('--create'); const run = (command: string[], cwd = repositoryRoot) => { const result = Bun.spawnSync(command, { cwd, stdin: 'inherit', stdout: 'inherit', stderr: 'inherit', }); if (result.exitCode !== 0) { throw new Error(`Command failed: ${command.join(' ')}`); } }; if (useCreateVideo) { console.log('Building create-video...'); run(['bunx', 'turbo', 'run', 'make', '--filter=create-video', '--no-update-notifier']); console.log('\nRunning create-video...'); run( [ 'node', join(repositoryRoot, 'packages', 'create-video', 'bin.js'), '--yes', '--blank', '--tmp', ], tmpdir(), ); process.exit(0); } if (!useOldVersion) { console.log('Building the current repository state...'); run(['bunx', 'turbo', 'run', 'make', '--no-update-notifier']); } console.log('\nBuilding agent skills...'); run(['bun', 'run', 'build'], agentPluginRoot); const projectRoot = join('/tmp', `remotion-video-${randomUUID().slice(0, 8)}`); mkdirSync(projectRoot); const listedFiles = Bun.spawnSync( [ 'git', 'ls-files', '--cached', '--others', '--exclude-standard', '--', 'packages/template-blank', ], {cwd: repositoryRoot}, ); if (listedFiles.exitCode !== 0) { throw new Error(listedFiles.stderr.toString()); } for (const file of listedFiles.stdout.toString().trim().split('\n')) { if (file === '') { continue; } const relativePath = relative('packages/template-blank', file); const destination = join(projectRoot, relativePath); mkdirSync(dirname(destination), {recursive: true}); cpSync(join(repositoryRoot, file), destination, {dereference: true}); } const blankTemplate = FEATURED_TEMPLATES.find( (template) => template.cliId === 'blank', ); if (!blankTemplate) { throw new Error('Could not find the blank create-video template.'); } addTailwindToConfig(projectRoot); addTailwindRootCss(projectRoot); createPublicFolder(projectRoot); patchReadmeMd(projectRoot, 'npm', blankTemplate); const currentVersion = JSON.parse( readFileSync( join(repositoryRoot, 'packages', 'core', 'package.json'), 'utf8', ), ).version as string; let remotionVersion = currentVersion; if (useOldVersion) { const publishedVersions = Bun.spawnSync( ['npm', 'view', 'remotion', 'versions', '--json'], {cwd: repositoryRoot}, ); if (publishedVersions.exitCode !== 0) { throw new Error(publishedVersions.stderr.toString()); } const stableVersions = ( JSON.parse(publishedVersions.stdout.toString()) as string[] ).filter((version) => /^\d+\.\d+\.\d+$/.test(version)); if (stableVersions.length < 2) { throw new Error('Could not find two published stable Remotion versions.'); } remotionVersion = stableVersions[stableVersions.length - 2]; console.log(`Using the previous stable Remotion release: ${remotionVersion}`); } patchPackageJson({ projectRoot, projectName: basename(projectRoot), latestRemotionVersion: remotionVersion, packageManager: 'npm', addTailwind: true, }); if (!useOldVersion) { const localPackageOverrides: Record = {}; const packagesRoot = join(repositoryRoot, 'packages'); for (const directory of readdirSync(packagesRoot)) { const packageDirectory = join(packagesRoot, directory); const packageJsonPath = join(packageDirectory, 'package.json'); if (!existsSync(packageJsonPath)) { continue; } const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')); if ( packageJson.private || (packageJson.name !== 'remotion' && !packageJson.name?.startsWith('@remotion/')) ) { continue; } localPackageOverrides[packageJson.name] = pathToFileURL(packageDirectory).href; } const projectPackageJsonPath = join(projectRoot, 'package.json'); const projectPackageJson = JSON.parse( readFileSync(projectPackageJsonPath, 'utf8'), ); for (const dependencyType of ['dependencies', 'devDependencies'] as const) { for (const dependency of Object.keys( projectPackageJson[dependencyType] ?? {}, )) { const localPackageOverride = localPackageOverrides[dependency]; if (localPackageOverride) { projectPackageJson[dependencyType][dependency] = localPackageOverride; } } } projectPackageJson[localPackageRootKey] = repositoryRoot; writeFileSync( projectPackageJsonPath, JSON.stringify(projectPackageJson, null, 2), ); } const gitInit = Bun.spawnSync(['git', 'init', '--quiet'], { cwd: projectRoot, stderr: 'inherit', }); if (gitInit.exitCode === 0) { Bun.spawnSync(['git', 'add', '--all'], {cwd: projectRoot}); Bun.spawnSync( ['git', 'commit', '--quiet', '-m', 'Create new Remotion video'], { cwd: projectRoot, }, ); Bun.spawnSync(['git', 'branch', '-M', 'main'], {cwd: projectRoot}); } console.log('\nInstalling the freshly built agent skills...'); run( [ 'bunx', '-y', '--loglevel=error', 'skills@1.5.26', 'add', agentPluginRoot, '--yes', ], projectRoot, ); console.log('\nInstalling npm dependencies...'); run(['npm', 'i'], projectRoot); if (gitInit.exitCode === 0) { Bun.spawnSync(['git', 'add', '--all'], {cwd: projectRoot}); Bun.spawnSync( ['git', 'commit', '--quiet', '-m', 'Install dependencies and agent skills'], { cwd: projectRoot, }, ); } console.log(`\nNew Remotion project ready at ${projectRoot}`); process.chdir(projectRoot); if (process.stdin.isTTY && process.stdout.isTTY) { console.log('Opening a shell in the new project. Run `exit` to return.\n'); const shell = process.env.SHELL ?? '/bin/sh'; const result = Bun.spawnSync([shell, '-l'], { cwd: projectRoot, stdin: 'inherit', stdout: 'inherit', stderr: 'inherit', }); process.exit(result.exitCode); } console.log(`Run: cd ${projectRoot}`);