π₯ Make videos programmatically with React
`@remotion/cli`: `npx remotion add` command
JonnyBurgerca713d7e4a88
diff --git a/packages/cli/src/add.ts b/packages/cli/src/add.tsnew file mode 100644index 0000000000..e697517ffd--- /dev/null+++ b/packages/cli/src/add.ts@@ -0,0 +1,137 @@+import {RenderInternals, type LogLevel} from '@remotion/renderer';+import {StudioServerInternals} from '@remotion/studio-server';+import {spawn} from 'node:child_process';+import {chalk} from './chalk';+import {listOfRemotionPackages} from './list-of-remotion-packages';+import {Log} from './log';++export const addCommand = async ({+ remotionRoot,+ packageManager,+ packageName,+ logLevel,+ args,+}: {+ remotionRoot: string;+ packageManager: string | undefined;+ packageName: string;+ logLevel: LogLevel;+ args: string[];+}) => {+ // Validate that the package name is a Remotion package+ if (!listOfRemotionPackages.includes(packageName)) {+ throw new Error(+ `Package "${packageName}" is not a Remotion package. Run "npx remotion versions" to see installed Remotion packages.`,+ );+ }++ const {+ dependencies,+ devDependencies,+ optionalDependencies,+ peerDependencies,+ } = StudioServerInternals.getInstalledDependencies(remotionRoot);++ // Check if the package is already installed+ const allDeps = [+ ...dependencies,+ ...devDependencies,+ ...optionalDependencies,+ ...peerDependencies,+ ];++ if (allDeps.includes(packageName)) {+ Log.info(+ {indent: false, logLevel},+ chalk.yellow(`Package "${packageName}" is already installed.`),+ );+ return;+ }++ // Find the version of installed Remotion packages+ const installedRemotionPackages = listOfRemotionPackages.filter((pkg) =>+ allDeps.includes(pkg),+ );++ if (installedRemotionPackages.length === 0) {+ throw new Error(+ 'No Remotion packages found in your project. Install Remotion first.',+ );+ }++ // Get the version from the first installed Remotion package+ const packageJsonPath = `${remotionRoot}/node_modules/${installedRemotionPackages[0]}/package.json`;+ let targetVersion: string;++ try {+ // eslint-disable-next-line @typescript-eslint/no-var-requires+ const packageJson = require(packageJsonPath);+ targetVersion = packageJson.version;+ Log.info(+ {indent: false, logLevel},+ `Installing ${packageName}@${targetVersion} to match your other Remotion packages`,+ );+ } catch (err) {+ throw new Error(+ `Could not determine version of installed Remotion packages: ${(err as Error).message}`,+ );+ }++ const manager = StudioServerInternals.getPackageManager(+ remotionRoot,+ packageManager,+ 0,+ );++ if (manager === 'unknown') {+ throw new Error(+ `No lockfile was found in your project (one of ${StudioServerInternals.lockFilePaths+ .map((p) => p.path)+ .join(', ')}). Install dependencies using your favorite manager!`,+ );+ }++ const command = StudioServerInternals.getInstallCommand({+ manager: manager.manager,+ packages: [packageName],+ version: targetVersion,+ additionalArgs: args,+ });++ Log.info(+ {indent: false, logLevel},+ chalk.gray(`$ ${manager.manager} ${command.join(' ')}`),+ );++ const task = spawn(manager.manager, command, {+ env: {+ ...process.env,+ ADBLOCK: '1',+ DISABLE_OPENCOLLECTIVE: '1',+ },+ stdio: RenderInternals.isEqualOrBelowLogLevel(logLevel, 'info')+ ? 'inherit'+ : 'ignore',+ });++ await new Promise<void>((resolve) => {+ task.on('close', (code) => {+ if (code === 0) {+ resolve();+ } else if (RenderInternals.isEqualOrBelowLogLevel(logLevel, 'info')) {+ throw new Error(+ `Failed to install ${packageName}, see logs above`,+ );+ } else {+ throw new Error(+ `Failed to install ${packageName}, run with --log=info to see logs`,+ );+ }+ });+ });++ Log.info(+ {indent: false, logLevel},+ `β ${packageName} has been installed!`,+ );+};diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.tsindex 22c7e4b015..14fbd5c6f0 100644--- a/packages/cli/src/index.ts+++ b/packages/cli/src/index.ts@@ -41,6 +41,7 @@ import {shouldUseNonOverlayingLogger} from './should-use-non-overlaying-logger';import {still} from './still';import {studioCommand} from './studio';import {upgradeCommand} from './upgrade';+import {addCommand} from './add';import {VERSIONS_COMMAND,validateVersionsBeforeCommand,@@ -118,6 +119,21 @@ export const cli = async () => {logLevel,args,});+ } else if (command === 'add') {+ const packageName = args[0];+ if (!packageName) {+ throw new Error(+ 'Please specify a package name. Example: npx remotion add @remotion/transitions',+ );+ }++ await addCommand({+ remotionRoot,+ packageManager: parsedCli['package-manager'],+ packageName,+ logLevel,+ args: args.slice(1),+ });} else if (command === VERSIONS_COMMAND) {await versionsCommand(remotionRoot, logLevel);} else if (command === BROWSER_COMMAND) {diff --git a/packages/cli/src/print-help.ts b/packages/cli/src/print-help.tsindex cc85cd4cd6..71ad7fcf99 100644--- a/packages/cli/src/print-help.ts+++ b/packages/cli/src/print-help.ts@@ -133,6 +133,20 @@ export const printHelp = (logLevel: LogLevel) => {chalk.gray('https://www.remotion.dev/docs/cli/upgrade'),);+ Log.info({indent: false, logLevel});+ Log.info(+ {indent: false, logLevel},+ chalk.blue('remotion add') + chalk.gray(' <package-name>'),+ );+ Log.info(+ {indent: false, logLevel},+ 'Add a Remotion package with the correct version.',+ );+ Log.info(+ {indent: false, logLevel},+ chalk.gray('https://www.remotion.dev/docs/cli/add'),+ );+Log.info({indent: false, logLevel});Log.info({indent: false, logLevel},diff --git a/packages/docs/docs/cli/add.mdx b/packages/docs/docs/cli/add.mdxnew file mode 100644index 0000000000..41c77507f1--- /dev/null+++ b/packages/docs/docs/cli/add.mdx@@ -0,0 +1,53 @@+---+image: /generated/articles-docs-cli-add.png+title: npx remotion add+sidebar_label: add+crumb: CLI Reference+---++Adds a Remotion package to your project with the same version as your other Remotion packages.++```+npx remotion add <package-name>+```++## Example++```bash+npx remotion add @remotion/transitions+```++This command will:+1. Verify that the package name is a valid Remotion package+2. Check if the package is already installed+3. Detect the version of your currently installed Remotion packages+4. Install the specified package with the matching version++## Flags++### `--package-manager`<AvailableFrom v="4.0.367"/>++_optional_++Forces a specific package manager to be used. By default, Remotion will auto-detect the package manager based on your lockfile.++Acceptable values are `npm`, `yarn` and `pnpm`++## Package manager support++`npm`, `yarn` and `pnpm` are all supported.++## Additional arguments++Any additional arguments you pass to this command will be forwarded as flags to the package manager, before the package name.++## Use case++This command is useful when you want to add a new Remotion package to your project and ensure it's installed with the same version as your other Remotion packages. This helps avoid version mismatches that could cause compatibility issues.++For example, if you have `[email protected]` installed and you want to add `@remotion/transitions`, running `npx remotion add @remotion/transitions` will install `@remotion/[email protected]`.++## See also++- [npx remotion upgrade](/docs/cli/upgrade)+- [npx remotion versions](/docs/cli/versions)diff --git a/packages/docs/docs/cli/table-of-contents.tsx b/packages/docs/docs/cli/table-of-contents.tsxindex 9b3ca37251..91b4db3a10 100644--- a/packages/docs/docs/cli/table-of-contents.tsx+++ b/packages/docs/docs/cli/table-of-contents.tsx@@ -50,6 +50,10 @@ export const TableOfContents: React.FC = () => {<strong>upgrade</strong><div>Upgrade to a newer version</div></TOCItem>+ <TOCItem link="/docs/cli/add">+ <strong>add</strong>+ <div>Add a Remotion package with matching version</div>+ </TOCItem><TOCItem link="/docs/cli/gpu"><strong>gpu</strong><div>Print information about {"Chrome's"} usage of the GPU</div>diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex 1258ee81c4..0c3622ce09 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -116,6 +116,7 @@ const sidebars: SidebarsConfig = {},'cli/versions','cli/upgrade',+ 'cli/add','cli/ffmpeg','cli/ffprobe','cli/gpu',