Skip to content

🎥 Make videos programmatically with React

logic for updating default props

Jonny Burger2d45745758d4
diff --git a/packages/cli/package.json b/packages/cli/package.json
index ff78649aa0..9133311b9e 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -57,6 +57,7 @@
"@types/node": "^16.7.5",
"@types/prompts": "^2.4.1",
"@types/react": "18.0.26",
+ "@types/prettier": "^2.7.2",
"@types/react-dom": "18.0.10",
"@types/semver": "^7.3.4",
"@typescript-eslint/eslint-plugin": "5.18.0",
diff --git a/packages/cli/src/codemods/update-default-props.ts b/packages/cli/src/codemods/update-default-props.ts
new file mode 100644
index 0000000000..b111d77cb5
--- /dev/null
+++ b/packages/cli/src/codemods/update-default-props.ts
@@ -0,0 +1,124 @@
+import {format, resolveConfig, resolveConfigFile} from 'prettier';
+
+const findStarter = ({
+ input,
+ compositionId,
+}: {
+ input: string;
+ compositionId: string;
+}) => {
+ const format1 = input.indexOf(`id="${compositionId}"`);
+ if (format1 > -1) {
+ return format1;
+ }
+
+ const format2 = input.indexOf(`id='${compositionId}'`);
+ if (format2 > -1) {
+ return format2;
+ }
+
+ const format3 = input.indexOf(`id={'${compositionId}'}`);
+ if (format3 > -1) {
+ return format3;
+ }
+
+ const format4 = input.indexOf(`id={"${compositionId}"}`);
+ if (format4 > -1) {
+ return format4;
+ }
+
+ const format5 = input.indexOf(`id={\`${compositionId}\``);
+ if (format5 > -1) {
+ return format5;
+ }
+
+ throw new Error(`Could not find composition ID ${compositionId} in file`);
+};
+
+const findEnder = (input: string, position: number, maxPosition: number) => {
+ let currentPosition = position;
+ while (currentPosition < maxPosition) {
+ const next = input.indexOf('}}', currentPosition + 1);
+ if (next === -1) {
+ throw new Error('Could not find end of defaultProps');
+ }
+
+ currentPosition = next;
+
+ const nextChar = input[next + 1];
+ if (nextChar === ',') {
+ continue;
+ }
+
+ return [position, currentPosition + 1];
+ }
+
+ throw new Error('did not find string');
+};
+
+const findTerminators = (input: string, position: number) => {
+ const nextComposition = input.indexOf('<Composition', position);
+ if (nextComposition > -1) {
+ return nextComposition;
+ }
+
+ const nextStill = input.indexOf('<Still', position);
+ if (nextStill > -1) {
+ return nextStill;
+ }
+
+ return Infinity;
+};
+
+// TODO: Add more sanity checks
+// TODO: better error messages
+// TODO: throw if prettier was not found
+export const updateDefaultProps = async ({
+ input,
+ compositionId,
+ newDefaultProps,
+}: {
+ input: string;
+ compositionId: string;
+ newDefaultProps: unknown;
+}): Promise<string> => {
+ const starter = findStarter({input, compositionId});
+
+ const START_TOKEN = 'defaultProps={';
+
+ const start = input.indexOf(START_TOKEN, starter);
+ if (start === -1) {
+ throw new Error('Could not find defaultProps in <Composition> tag');
+ }
+
+ const maxEnd = findTerminators(input, starter);
+ const [startPos, endPos] = findEnder(
+ input,
+ start + START_TOKEN.length,
+ maxEnd
+ );
+
+ const newFile =
+ input.substring(0, startPos) +
+ JSON.stringify(newDefaultProps) +
+ input.substring(endPos);
+
+ const configFilePath = await resolveConfigFile();
+ if (!configFilePath) {
+ throw new Error('prettier config file not found');
+ }
+
+ const prettierConfig = await resolveConfig(configFilePath);
+ if (!prettierConfig) {
+ throw new Error('Prettier config not found');
+ }
+
+ const prettified = format(newFile, {
+ ...prettierConfig,
+ rangeStart: startPos,
+ rangeEnd: endPos,
+ filepath: 'test.tsx',
+ plugins: [],
+ });
+ return prettified;
+};
diff --git a/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx b/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx
index 511c8c4ca7..364bf4e15d 100644
--- a/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx
+++ b/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx
@@ -42,6 +42,7 @@ export const CurrentCompositionKeybindings: React.FC = () => {
renderButton.click();
}, [type, video]);
+ // TODO: Does not work if the left sidebar is collapsed
useEffect(() => {
const binding = keybindings.registerKeybinding({
event: 'keydown',
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx
index eae4ed65fd..b5dd60467b 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx
@@ -1,4 +1,5 @@
import {useCallback, useMemo} from 'react';
+import {Spacing} from '../../layout';
import {InlineRemoveButton} from '../InlineRemoveButton';
import type {JSONPath} from './zod-types';
import {ZodSwitch} from './ZodSwitch';
@@ -52,6 +53,7 @@ export const ZodArrayItemEditor: React.FC<{
return (
<div style={row}>
<InlineRemoveButton onClick={onRemove} />
+ <Spacing x={1} />
<div style={flex}>
<ZodSwitch
jsonPath={newJsonPath}
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx
index bb4f7019be..f5317dff9f 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx
@@ -50,13 +50,13 @@ const getMaxValue = (schema: z.ZodTypeAny) => {
return maxCheck.value;
};
-const getStep = (schema: z.ZodTypeAny) => {
+const getStep = (schema: z.ZodTypeAny): number | undefined => {
const multipleStep = (schema as z.ZodNumber)._def.checks.find(
(c) => c.kind === 'multipleOf'
);
if (!multipleStep) {
- return 1;
+ return undefined;
}
if (multipleStep.kind !== 'multipleOf') {
diff --git a/packages/cli/src/test/snapshots/root-after.tsx b/packages/cli/src/test/snapshots/root-after.tsx
new file mode 100644
index 0000000000..fe9c6aa99d
--- /dev/null
+++ b/packages/cli/src/test/snapshots/root-after.tsx
@@ -0,0 +1,38 @@
+import React from 'react';
+import {Composition} from 'remotion';
+
+const HelloWorld = () => {
+ return <div>Hello World</div>;
+};
+
+export const Root: React.FC = () => {
+ return (
+ <>
+ <Composition
+ component={HelloWorld}
+ durationInFrames={120}
+ fps={30}
+ height={1080}
+ width={1080}
+ id="HelloWorld"
+ />
+ <Composition
+ component={HelloWorld}
+ durationInFrames={120}
+ fps={30}
+ height={1080}
+ width={1080}
+ id="Comp2"
+ />
+ <Composition
+ component={HelloWorld}
+ durationInFrames={120}
+ fps={30}
+ height={1080}
+ width={1080}
+ id="Comp3"
+ defaultProps={{abc: 'def'}}
+ />
+ </>
+ );
+};
diff --git a/packages/cli/src/test/snapshots/root-before.tsx b/packages/cli/src/test/snapshots/root-before.tsx
new file mode 100644
index 0000000000..0157bde6ca
--- /dev/null
+++ b/packages/cli/src/test/snapshots/root-before.tsx
@@ -0,0 +1,40 @@
+import React from 'react';
+import {Composition} from 'remotion';
+
+const HelloWorld = () => {
+ return <div>Hello World</div>;
+};
+
+export const Root: React.FC = () => {
+ return (
+ <>
+ <Composition
+ component={HelloWorld}
+ durationInFrames={120}
+ fps={30}
+ height={1080}
+ width={1080}
+ id="HelloWorld"
+ />
+ <Composition
+ component={HelloWorld}
+ durationInFrames={120}
+ fps={30}
+ height={1080}
+ width={1080}
+ id="Comp2"
+ />
+ <Composition
+ component={HelloWorld}
+ durationInFrames={120}
+ fps={30}
+ height={1080}
+ width={1080}
+ id="Comp3"
+ defaultProps={{
+ foo: 'bar',
+ }}
+ />
+ </>
+ );
+};
diff --git a/packages/cli/src/test/update-default-props.test.ts b/packages/cli/src/test/update-default-props.test.ts
new file mode 100644
index 0000000000..12a31f0a04
--- /dev/null
+++ b/packages/cli/src/test/update-default-props.test.ts
@@ -0,0 +1,23 @@
+import {readFileSync} from 'fs';
+import path from 'path';
+import {expect, test} from 'vitest';
+import {updateDefaultProps} from '../codemods/update-default-props';
+
+test('Should be able to update default props', async () => {
+ const file = readFileSync(
+ path.join(__dirname, 'snapshots', 'root-before.tsx'),
+ 'utf-8'
+ );
+ const expected = readFileSync(
+ path.join(__dirname, 'snapshots', 'root-after.tsx'),
+ 'utf-8'
+ );
+
+ const update = await updateDefaultProps({
+ input: file,
+ compositionId: 'Comp3',
+ newDefaultProps: {abc: 'def'},
+ });
+
+ expect(update).toBe(expected);
+});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 144e26feac..b6538c281f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -142,6 +142,7 @@ importers:
'@remotion/renderer': workspace:*
'@types/minimist': ^1.2.2
'@types/node': ^16.7.5
+ '@types/prettier': ^2.7.2
'@types/prompts': ^2.4.1
'@types/react': 18.0.26
'@types/react-dom': 18.0.10
@@ -183,6 +184,7 @@ importers:
'@jonny/eslint-config': 3.0.266_t54va6jeo5lf3vtqih5uiyrm5e
'@types/minimist': 1.2.2
'@types/node': 16.10.2
+ '@types/prettier': 2.7.2
'@types/prompts': 2.4.1
'@types/react': 18.0.26
'@types/react-dom': 18.0.10
@@ -9365,6 +9367,10 @@ packages:
resolution: {integrity: sha512-ri0UmynRRvZiiUJdiz38MmIblKK+oH30MztdBVR95dv/Ubw6neWSb8u1XpRb72L4qsZOhz+L+z9JD40SJmfWow==}
dev: true
+ /@types/prettier/2.7.2:
+ resolution: {integrity: sha512-KufADq8uQqo1pYKVIYzfKbJfBAc0sOeXqGbFaSpv8MRmC/zXgowNZmFcbngndGk922QDmOASEXUZCaY48gs4cg==}
+ dev: true
+
/@types/progress/2.0.5:
resolution: {integrity: sha512-ZYYVc/kSMkhH9W/4dNK/sLNra3cnkfT2nJyOAIDY+C2u6w72wa0s1aXAezVtbTsnN8HID1uhXCrLwDE2ZXpplg==}
dependencies: