Skip to content

🎥 Make videos programmatically with React

use enum paths

Jonny Burgerd5ccc1151c5c
diff --git a/packages/cli/src/codemods/stringify-with-path.ts b/packages/cli/src/codemods/stringify-with-path.ts
index 985a2338f2..17ce405fe8 100644
--- a/packages/cli/src/codemods/stringify-with-path.ts
+++ b/packages/cli/src/codemods/stringify-with-path.ts
@@ -1,6 +1,6 @@
-type Path = (string | number)[];
+import type {EnumPath} from '../editor/components/RenderModal/SchemaEditor/extract-enum-json-paths';
-const doesMatchPath = (path1: Path, enumPaths: Path[]) => {
+const doesMatchPath = (path1: EnumPath, enumPaths: EnumPath[]) => {
return enumPaths.some((p) =>
path1.every((item, index) => {
if (p[index] === '[]' && !Number.isNaN(Number(item))) {
@@ -18,10 +18,10 @@ const doesMatchPath = (path1: Path, enumPaths: Path[]) => {
export const stringifyDefaultProps = ({
props,
- enumPath,
+ enumPaths,
}: {
props: unknown;
- enumPath: Path[];
+ enumPaths: EnumPath[];
}) =>
JSON.stringify(
props,
@@ -31,7 +31,7 @@ export const stringifyDefaultProps = ({
return `__REMOVEQUOTE__new Date('${item.toISOString()}')__REMOVEQUOTE__`;
}
- if (typeof this[key] === 'string' && doesMatchPath(path, enumPath)) {
+ if (typeof this[key] === 'string' && doesMatchPath(path, enumPaths)) {
return `${this[key]}__ADD_AS_CONST__`;
}
diff --git a/packages/cli/src/codemods/update-default-props.ts b/packages/cli/src/codemods/update-default-props.ts
index c14cae30b7..538a17c856 100644
--- a/packages/cli/src/codemods/update-default-props.ts
+++ b/packages/cli/src/codemods/update-default-props.ts
@@ -1,3 +1,4 @@
+import type {EnumPath} from '../editor/components/RenderModal/SchemaEditor/extract-enum-json-paths';
import {stringifyDefaultProps} from './stringify-with-path';
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
@@ -99,10 +100,12 @@ export const updateDefaultProps = async ({
input,
compositionId,
newDefaultProps,
+ enumPaths,
}: {
input: string;
compositionId: string;
newDefaultProps: unknown;
+ enumPaths: EnumPath[];
}): Promise<string> => {
const starter = findStarter({input, compositionId});
@@ -136,7 +139,7 @@ export const updateDefaultProps = async ({
const newFile =
input.substring(0, startPos) +
- stringifyDefaultProps({props: newDefaultProps}) +
+ stringifyDefaultProps({props: newDefaultProps, enumPaths}) +
input.substring(endPos);
const configFilePath = await resolveConfigFile();
diff --git a/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx b/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
index 2c22b0dbbe..6a43564c8c 100644
--- a/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
+++ b/packages/cli/src/editor/components/RenderModal/RenderModalData.tsx
@@ -29,6 +29,7 @@ import {
deserializeJSONWithDate,
serializeJSONWithDate,
} from './SchemaEditor/date-serialization';
+import {extractEnumJsonPaths} from './SchemaEditor/extract-enum-json-paths';
import {SchemaEditor} from './SchemaEditor/SchemaEditor';
import {
NoDefaultProps,
@@ -250,9 +251,18 @@ export const RenderModalData: React.FC<{
}, []);
const onUpdate = useCallback(() => {
+ if (schema === 'no-zod' || z === null) {
+ sendErrorNotification('Cannot update default props: No Zod schema');
+ return;
+ }
+
setValBeforeSafe(inputProps);
- updateDefaultProps(composition.id, inputProps);
- }, [composition.id, inputProps]);
+ updateDefaultProps(
+ composition.id,
+ inputProps,
+ extractEnumJsonPaths(schema, z, [])
+ );
+ }, [composition.id, inputProps, schema, z]);
useEffect(() => {
setSaving(false);
@@ -260,16 +270,22 @@ export const RenderModalData: React.FC<{
const onSave = useCallback(
(updater: (oldState: unknown) => unknown) => {
+ if (schema === 'no-zod' || z === null) {
+ sendErrorNotification('Cannot update default props: No Zod schema');
+ return;
+ }
+
setSaving(true);
updateDefaultProps(
composition.id,
- updater(composition.defaultProps)
+ updater(composition.defaultProps),
+ extractEnumJsonPaths(schema, z, [])
).catch((err) => {
sendErrorNotification(`Cannot update default props: ${err.message}`);
setSaving(false);
});
},
- [composition.defaultProps, composition.id]
+ [composition.defaultProps, composition.id, schema, z]
);
const connectionStatus = useContext(PreviewServerConnectionCtx).type;
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/extract-enum-json-paths.ts b/packages/cli/src/editor/components/RenderModal/SchemaEditor/extract-enum-json-paths.ts
index 1a7a19ac66..135fbf5025 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/extract-enum-json-paths.ts
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/extract-enum-json-paths.ts
@@ -1,6 +1,8 @@
import type {z} from 'zod';
import type {ZodType} from '../../get-zod-if-possible';
+export type EnumPath = (string | number)[];
+
export const extractEnumJsonPaths = (
schema: Zod.ZodTypeAny,
zodRuntime: ZodType,
diff --git a/packages/cli/src/editor/components/RenderQueue/actions.ts b/packages/cli/src/editor/components/RenderQueue/actions.ts
index 48890b854a..5e189a0fc6 100644
--- a/packages/cli/src/editor/components/RenderQueue/actions.ts
+++ b/packages/cli/src/editor/components/RenderQueue/actions.ts
@@ -13,6 +13,7 @@ import type {
} from '../../../preview-server/render-queue/job';
import type {RequiredChromiumOptions} from '../../../required-chromium-options';
import {serializeJSONWithDate} from '../RenderModal/SchemaEditor/date-serialization';
+import type {EnumPath} from '../RenderModal/SchemaEditor/extract-enum-json-paths';
const callApi = <Endpoint extends keyof ApiRoutes>(
endpoint: Endpoint,
@@ -215,12 +216,14 @@ export const cancelRenderJob = (job: RenderJob) => {
export const updateDefaultProps = (
compositionId: string,
- defaultProps: unknown
+ defaultProps: unknown,
+ enumPaths: EnumPath[]
) => {
return callApi('/api/update-default-props', {
compositionId,
defaultProps: serializeJSONWithDate(defaultProps, undefined)
.serializedString,
+ enumPaths,
});
};
diff --git a/packages/cli/src/preview-server/render-queue/job.ts b/packages/cli/src/preview-server/render-queue/job.ts
index 3f12e4edda..c2399e15b9 100644
--- a/packages/cli/src/preview-server/render-queue/job.ts
+++ b/packages/cli/src/preview-server/render-queue/job.ts
@@ -7,6 +7,7 @@ import type {
StillImageFormat,
VideoImageFormat,
} from '@remotion/renderer';
+import type {EnumPath} from '../../editor/components/RenderModal/SchemaEditor/extract-enum-json-paths';
import type {AggregateRenderProgress} from '../../progress-types';
import type {RequiredChromiumOptions} from '../../required-chromium-options';
@@ -158,6 +159,7 @@ export type UnsubscribeFromFileExistenceRequest = {
export type UpdateDefaultPropsRequest = {
compositionId: string;
defaultProps: string;
+ enumPaths: EnumPath[];
};
export type CanUpdateDefaultPropsRequest = {
diff --git a/packages/cli/src/preview-server/routes/can-update-default-props.ts b/packages/cli/src/preview-server/routes/can-update-default-props.ts
index cad50270f1..1564d93fa1 100644
--- a/packages/cli/src/preview-server/routes/can-update-default-props.ts
+++ b/packages/cli/src/preview-server/routes/can-update-default-props.ts
@@ -34,6 +34,7 @@ export const canUpdateDefaultPropsHandler: ApiHandler<
compositionId,
input: readFileSync(projectInfo.videoFile, 'utf-8'),
newDefaultProps: {},
+ enumPaths: [],
});
return {
diff --git a/packages/cli/src/preview-server/routes/update-default-props.ts b/packages/cli/src/preview-server/routes/update-default-props.ts
index 074291abd6..c914b12461 100644
--- a/packages/cli/src/preview-server/routes/update-default-props.ts
+++ b/packages/cli/src/preview-server/routes/update-default-props.ts
@@ -9,7 +9,7 @@ import {checkIfTypeScriptFile} from './can-update-default-props';
export const updateDefaultPropsHandler: ApiHandler<
UpdateDefaultPropsRequest,
void
-> = async ({input: {compositionId, defaultProps}, remotionRoot}) => {
+> = async ({input: {compositionId, defaultProps, enumPaths}, remotionRoot}) => {
const projectInfo = await getProjectInfo(remotionRoot);
// TODO: What happens if this error is thrown? Handle in frontend
if (!projectInfo.videoFile) {
@@ -23,6 +23,7 @@ export const updateDefaultPropsHandler: ApiHandler<
compositionId,
input: readFileSync(projectInfo.videoFile, 'utf-8'),
newDefaultProps: deserializeJSONWithDate(defaultProps),
+ enumPaths,
});
writeFileSync(projectInfo.videoFile, updated);
diff --git a/packages/cli/src/test/stringify-default-props.test.ts b/packages/cli/src/test/stringify-default-props.test.ts
index 6b7c837760..09880b1ed7 100644
--- a/packages/cli/src/test/stringify-default-props.test.ts
+++ b/packages/cli/src/test/stringify-default-props.test.ts
@@ -11,7 +11,7 @@ test('Should stringify default props correctly', () => {
abc: 'def',
newDate: new Date('2022-01-02'),
},
- enumPath: [],
+ enumPaths: [],
});
expect(result).toBe(
`{"abc":"def","newDate":new Date('2022-01-02T00:00:00.000Z')}`
@@ -34,7 +34,7 @@ test('Should stringify default props correctly', () => {
const enumPaths = extractEnumJsonPaths(schema, z, []);
expect(enumPaths).toStrictEqual([['abc'], ['array', '[]']]);
- const result = stringifyDefaultProps({props: values, enumPath: enumPaths});
+ const result = stringifyDefaultProps({props: values, enumPaths});
expect(result).toBe(
`{"abc":"hi" as const,"array":["hi" as const],"notAnEnum":""}`
);
diff --git a/packages/cli/src/test/update-default-props.test.ts b/packages/cli/src/test/update-default-props.test.ts
index 6e56e2d7de..87187bde72 100644
--- a/packages/cli/src/test/update-default-props.test.ts
+++ b/packages/cli/src/test/update-default-props.test.ts
@@ -17,6 +17,7 @@ test('Should be able to update default props', async () => {
input: file,
compositionId: 'Comp3',
newDefaultProps: {abc: 'def', newDate: new Date('2022-01-02')},
+ enumPaths: [],
});
expect(update).toBe(expected);