Skip to content

🎥 Make videos programmatically with React

start testing codemods

Jonny Burger08fac192f197
diff --git a/packages/it-tests/package.json b/packages/it-tests/package.json
index 658e9ea221..4b2fc2f286 100644
--- a/packages/it-tests/package.json
+++ b/packages/it-tests/package.json
@@ -25,6 +25,7 @@
"@remotion/layout-utils": "workspace:*",
"@remotion/install-whisper-cpp": "workspace:*",
"@remotion/google-fonts": "workspace:*",
+ "@remotion/studio-server": "workspace:*",
"@remotion/transitions": "workspace:*",
"@types/sharp": "^0.28.5",
"@types/bun": "^1.0.3",
diff --git a/packages/it-tests/src/codemods/codemods.test.ts b/packages/it-tests/src/codemods/codemods.test.ts
new file mode 100644
index 0000000000..92046c9400
--- /dev/null
+++ b/packages/it-tests/src/codemods/codemods.test.ts
@@ -0,0 +1,104 @@
+import { test } from "bun:test";
+import { readFileSync } from "fs";
+import { resolve } from "path";
+import { StudioServerInternals } from "@remotion/studio-server";
+import { expect } from "vitest";
+
+const rootFile = resolve(
+ __dirname,
+ "..",
+ "..",
+ "..",
+ "example",
+ "src",
+ "CodemodTestbed.tsx"
+);
+
+const getCompositionCount = (comp: string) => {
+ const lines = comp.split("\n");
+ const compositions = lines.filter(
+ (l) => l.includes("<Composition") || l.includes("<Still")
+ );
+ return compositions.length;
+};
+
+const contents = readFileSync(rootFile, "utf-8");
+const compCount = getCompositionCount(contents);
+
+test("Should be able to delete composition", async () => {
+ expect(contents).toContain('"one"');
+
+ const { changesMade, newContents } =
+ await StudioServerInternals.parseAndApplyCodemod({
+ input: contents,
+ codeMod: {
+ type: "delete-composition",
+ idToDelete: "one",
+ },
+ });
+ expect(changesMade.length).toBe(1);
+ expect(getCompositionCount(newContents)).toBe(compCount - 1);
+ expect(newContents).not.toContain('"one"');
+});
+
+test("Should be able to rename composition", async () => {
+ const { changesMade, newContents } =
+ await StudioServerInternals.parseAndApplyCodemod({
+ input: contents,
+ codeMod: {
+ type: "rename-composition",
+ idToRename: "one",
+ newId: "abc",
+ },
+ });
+ expect(changesMade.length).toBe(1);
+ expect(getCompositionCount(newContents)).toBe(compCount);
+ expect(newContents).not.toContain('"one"');
+ expect(newContents).toContain('"abc"');
+});
+
+test("Should be able to duplicate composition", async () => {
+ const { newContents } = await StudioServerInternals.parseAndApplyCodemod({
+ input: contents,
+ codeMod: {
+ type: "duplicate-composition",
+ idToDuplicate: "one",
+ newDurationInFrames: 200,
+ newFps: 24,
+ newHeight: 999,
+ newWidth: 998,
+ newId: "def",
+ tag: "Composition",
+ },
+ });
+
+ expect(getCompositionCount(newContents)).toBe(compCount + 1);
+ expect(newContents).toContain('"def"');
+ expect(newContents).toContain("width={998}");
+ expect(newContents).toContain("height={999}");
+ expect(newContents).toContain("fps={24}");
+ expect(newContents).toContain("durationInFrames={200}");
+});
+
+test("Should be able to duplicate composition into a still", async () => {
+ const { newContents } = await StudioServerInternals.parseAndApplyCodemod({
+ input: contents,
+ codeMod: {
+ type: "duplicate-composition",
+ idToDuplicate: "one",
+ newDurationInFrames: 200,
+ newFps: 24,
+ newHeight: 999,
+ newWidth: 998,
+ newId: "def",
+ tag: "Still",
+ },
+ });
+
+ expect(getCompositionCount(newContents)).toBe(compCount + 1);
+ expect(newContents).toContain('"def"');
+ expect(newContents).toContain("width={998}");
+ expect(newContents).toContain("height={999}");
+ expect(newContents).not.toContain("fps={24}");
+ expect(newContents).not.toContain("durationInFrames={200}");
+});
diff --git a/packages/studio-server/src/codemods/duplicate-composition.ts b/packages/studio-server/src/codemods/duplicate-composition.ts
index 1919d1658a..7c939f73cf 100644
--- a/packages/studio-server/src/codemods/duplicate-composition.ts
+++ b/packages/studio-server/src/codemods/duplicate-composition.ts
@@ -13,6 +13,31 @@ const getPrettier = async () => {
}
};
+export const formatOutput = async (tsContent: string) => {
+ const prettier = await getPrettier();
+
+ const {format, resolveConfig, resolveConfigFile} = prettier;
+
+ const configFilePath = await resolveConfigFile();
+ if (!configFilePath) {
+ throw new Error('The Prettier config file was not found');
+ }
+
+ const prettierConfig = await resolveConfig(configFilePath);
+ if (!prettierConfig) {
+ throw new Error(
+ `The Prettier config at ${configFilePath} could not be read`,
+ );
+ }
+
+ const newContents = await format(tsContent, {
+ ...prettierConfig,
+ filepath: 'test.tsx',
+ });
+
+ return newContents;
+};
+
export const parseAndApplyCodemod = async ({
input,
codeMod,
@@ -37,26 +62,5 @@ export const parseAndApplyCodemod = async ({
parser: tsParser,
}).code;
- const prettier = await getPrettier();
-
- const {format, resolveConfig, resolveConfigFile} = prettier;
-
- const configFilePath = await resolveConfigFile();
- if (!configFilePath) {
- throw new Error('The Prettier config file was not found');
- }
-
- const prettierConfig = await resolveConfig(configFilePath);
- if (!prettierConfig) {
- throw new Error(
- `The Prettier config at ${configFilePath} could not be read`,
- );
- }
-
- const newContents = await format(output, {
- ...prettierConfig,
- filepath: 'test.tsx',
- });
-
- return {changesMade, newContents};
+ return {changesMade, newContents: output};
};
diff --git a/packages/studio-server/src/index.ts b/packages/studio-server/src/index.ts
index 489a7d42b5..baa302dd06 100644
--- a/packages/studio-server/src/index.ts
+++ b/packages/studio-server/src/index.ts
@@ -25,6 +25,7 @@ export type {
} from '@remotion/studio-shared';
import {AnsiDiff} from './ansi-diff';
+import {parseAndApplyCodemod} from './codemods/duplicate-composition';
import {installFileWatcher} from './file-watcher';
import {getLatestRemotionVersion} from './get-latest-remotion-version';
import {
@@ -51,4 +52,5 @@ export const StudioServerInternals = {
installFileWatcher,
AnsiDiff,
formatBytes,
+ parseAndApplyCodemod,
};
diff --git a/packages/studio-server/src/preview-server/routes/apply-codemod.ts b/packages/studio-server/src/preview-server/routes/apply-codemod.ts
index c00d0444a5..333433c2a3 100644
--- a/packages/studio-server/src/preview-server/routes/apply-codemod.ts
+++ b/packages/studio-server/src/preview-server/routes/apply-codemod.ts
@@ -3,7 +3,10 @@ import type {
ApplyCodemodResponse,
} from '@remotion/studio-shared';
import {readFileSync, writeFileSync} from 'node:fs';
-import {parseAndApplyCodemod} from '../../codemods/duplicate-composition';
+import {
+ formatOutput,
+ parseAndApplyCodemod,
+} from '../../codemods/duplicate-composition';
import {simpleDiff} from '../../codemods/simple-diff';
import type {ApiHandler} from '../api-types';
import {getProjectInfo} from '../project-info';
@@ -33,7 +36,8 @@ export const applyCodemodHandler: ApiHandler<
});
if (!dryRun) {
- writeFileSync(projectInfo.rootFile, newContents);
+ const formatted = await formatOutput(newContents);
+ writeFileSync(projectInfo.rootFile, formatted);
}
return {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ce8ab68c13..468c4423e8 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1204,6 +1204,9 @@ importers:
'@remotion/shapes':
specifier: workspace:*
version: link:../shapes
+ '@remotion/studio-server':
+ specifier: workspace:*
+ version: link:../studio-server
'@remotion/transitions':
specifier: workspace:*
version: link:../transitions