Skip to content

πŸŽ₯ Make videos programmatically with React

`@remotion/studio-server`: Log effect prop edits as effect({key: value}) (#7397)

Jonny Burger529d0a60873d
diff --git a/packages/studio-server/src/codemods/update-effect-props/update-effect-props.ts b/packages/studio-server/src/codemods/update-effect-props/update-effect-props.ts
index 73d3ee0f15..55d39b272f 100644
--- a/packages/studio-server/src/codemods/update-effect-props/update-effect-props.ts
+++ b/packages/studio-server/src/codemods/update-effect-props/update-effect-props.ts
@@ -28,6 +28,7 @@ export type UpdateEffectPropsResult = {
formatted: boolean;
oldValueString: string;
logLine: number;
+ effectCallee: string;
};
const parseValueExpression = (value: unknown): ExpressionKind => {
@@ -125,7 +126,7 @@ export const findEffectCallExpression = ({
attr: JSXAttribute;
effectIndex: number;
}):
- | {kind: 'ok'; call: CallExpression}
+ | {kind: 'ok'; call: CallExpression; callee: string}
| {
kind: 'error';
reason: 'not-found' | 'not-call-expression';
@@ -150,7 +151,7 @@ export const findEffectCallExpression = ({
return {kind: 'error', reason: 'not-call-expression'};
}
- return {kind: 'ok', call: target.node};
+ return {kind: 'ok', call: target.node, callee: target.callee};
};
const findObjectProperty = (
@@ -188,6 +189,7 @@ export const updateEffectPropsAst = ({
serialized: string;
oldValueString: string;
logLine: number;
+ effectCallee: string;
} => {
const ast = parseAst(input);
const jsx = findJsxElementAtNodePath(ast, sequenceNodePath);
@@ -213,7 +215,7 @@ export const updateEffectPropsAst = ({
throw new Error(`Cannot update effect prop: ${found.reason}`);
}
- const {call} = found;
+ const {call, callee: effectCallee} = found;
const isDefault =
update.defaultValue !== null &&
@@ -223,11 +225,11 @@ export const updateEffectPropsAst = ({
if (call.arguments.length === 0) {
if (isDefault) {
- const logLine = call.loc?.start.line ?? jsx.loc?.start.line ?? 1;
return {
serialized: serializeAst(ast),
oldValueString: '',
- logLine,
+ logLine: call.loc?.start.line ?? jsx.loc?.start.line ?? 1,
+ effectCallee,
};
}
@@ -275,6 +277,7 @@ export const updateEffectPropsAst = ({
serialized: serializeAst(ast),
oldValueString,
logLine,
+ effectCallee,
};
};
@@ -291,12 +294,13 @@ export const updateEffectProps = async ({
update: EffectPropUpdate;
prettierConfigOverride?: Record<string, unknown> | null;
}): Promise<UpdateEffectPropsResult> => {
- const {serialized, oldValueString, logLine} = updateEffectPropsAst({
- input,
- sequenceNodePath,
- effectIndex,
- update,
- });
+ const {serialized, oldValueString, logLine, effectCallee} =
+ updateEffectPropsAst({
+ input,
+ sequenceNodePath,
+ effectIndex,
+ update,
+ });
const {output, formatted} = await formatFileContent({
input: serialized,
@@ -308,5 +312,6 @@ export const updateEffectProps = async ({
oldValueString,
formatted,
logLine,
+ effectCallee,
};
};
diff --git a/packages/studio-server/src/preview-server/routes/log-updates/format-effect-prop-change.ts b/packages/studio-server/src/preview-server/routes/log-updates/format-effect-prop-change.ts
new file mode 100644
index 0000000000..f36e89ff8e
--- /dev/null
+++ b/packages/studio-server/src/preview-server/routes/log-updates/format-effect-prop-change.ts
@@ -0,0 +1,111 @@
+import {formatSideProps} from './format-side-props';
+import {
+ addedPrefixIfNoColor,
+ attrName,
+ colorValue,
+ punctuation,
+ strikeThroughOrRemovedPrefix,
+ type PropDelta,
+} from './formatting';
+
+const formatEffectPropBody = (key: string, valueString: string) => {
+ return `${punctuation(key)}${punctuation(': ')}${colorValue(valueString)}`;
+};
+
+const formatEffectCall = (
+ effectName: string,
+ key: string,
+ valueString: string,
+) => {
+ return `${attrName(effectName)}${punctuation('(')}${punctuation('{')}${formatEffectPropBody(key, valueString)}${punctuation('}')}${punctuation(')')}`;
+};
+
+const formatEffectDeletion = ({
+ effectName,
+ key,
+ valueString,
+}: {
+ effectName: string;
+ key: string;
+ valueString: string;
+}) => {
+ const inner = formatEffectPropBody(key, valueString);
+ return `${attrName(effectName)}${punctuation('(')}${punctuation('{')}${strikeThroughOrRemovedPrefix(inner)}${punctuation('}')}${punctuation(')')}`;
+};
+
+const formatEffectAddition = ({
+ effectName,
+ key,
+ valueString,
+}: {
+ effectName: string;
+ key: string;
+ valueString: string;
+}) => {
+ return addedPrefixIfNoColor(formatEffectCall(effectName, key, valueString));
+};
+
+const formatEffectInnerPropChange = ({
+ effectName,
+ key,
+ oldValueString,
+ newValueString,
+ defaultValueString,
+}: {
+ effectName: string;
+ key: string;
+ oldValueString: string;
+ newValueString: string;
+ defaultValueString: string | null;
+}) => {
+ if (defaultValueString !== null && newValueString === defaultValueString) {
+ return formatEffectDeletion({
+ effectName,
+ key,
+ valueString: oldValueString,
+ });
+ }
+
+ if (defaultValueString !== null && oldValueString === defaultValueString) {
+ return formatEffectAddition({
+ effectName,
+ key,
+ valueString: newValueString,
+ });
+ }
+
+ return `${formatEffectCall(effectName, key, oldValueString)} \u2192 ${formatEffectCall(effectName, key, newValueString)}`;
+};
+
+export const formatEffectPropChange = ({
+ effectName,
+ key,
+ oldValueString,
+ newValueString,
+ defaultValueString,
+ removedProps,
+ addedProps,
+}: {
+ effectName: string;
+ key: string;
+ oldValueString: string;
+ newValueString: string;
+ defaultValueString: string | null;
+ removedProps: PropDelta[];
+ addedProps: PropDelta[];
+}) => {
+ const suffix = formatSideProps({removedProps, addedProps});
+
+ return [
+ formatEffectInnerPropChange({
+ effectName,
+ key,
+ oldValueString,
+ newValueString,
+ defaultValueString,
+ }),
+ suffix,
+ ]
+ .filter(Boolean)
+ .join(', ');
+};
diff --git a/packages/studio-server/src/preview-server/routes/log-updates/log-effect-update.ts b/packages/studio-server/src/preview-server/routes/log-updates/log-effect-update.ts
new file mode 100644
index 0000000000..cd6cc74ab9
--- /dev/null
+++ b/packages/studio-server/src/preview-server/routes/log-updates/log-effect-update.ts
@@ -0,0 +1,50 @@
+import {RenderInternals} from '@remotion/renderer';
+import type {LogLevel} from '@remotion/renderer';
+import {formatEffectPropChange} from './format-effect-prop-change';
+import type {PropDelta} from './formatting';
+import {normalizeQuotes, warnAboutPrettierOnce} from './log-update';
+
+export const logEffectUpdate = ({
+ fileRelativeToRoot,
+ line,
+ effectName,
+ propKey,
+ oldValueString,
+ newValueString,
+ defaultValueString,
+ formatted,
+ logLevel,
+ removedProps,
+ addedProps,
+}: {
+ fileRelativeToRoot: string;
+ line: number;
+ effectName: string;
+ propKey: string;
+ oldValueString: string;
+ newValueString: string;
+ defaultValueString: string | null;
+ formatted: boolean;
+ logLevel: LogLevel;
+ removedProps: PropDelta[];
+ addedProps: PropDelta[];
+}) => {
+ const locationLabel = `${fileRelativeToRoot}:${line}`;
+ const propChange = formatEffectPropChange({
+ effectName,
+ key: propKey,
+ oldValueString: normalizeQuotes(oldValueString),
+ newValueString: normalizeQuotes(newValueString),
+ defaultValueString:
+ defaultValueString !== null ? normalizeQuotes(defaultValueString) : null,
+ removedProps,
+ addedProps,
+ });
+ RenderInternals.Log.info(
+ {indent: false, logLevel},
+ `${RenderInternals.chalk.blueBright(`${locationLabel}`)} ${propChange}`,
+ );
+ if (!formatted) {
+ warnAboutPrettierOnce(logLevel);
+ }
+};
diff --git a/packages/studio-server/src/preview-server/routes/save-effect-props.ts b/packages/studio-server/src/preview-server/routes/save-effect-props.ts
index 2907efd0a6..088488bf2a 100644
--- a/packages/studio-server/src/preview-server/routes/save-effect-props.ts
+++ b/packages/studio-server/src/preview-server/routes/save-effect-props.ts
@@ -18,8 +18,9 @@ import {
import {suppressBundlerUpdateForFile} from '../watch-ignore-next-change';
import {computeEffectPropStatus} from './can-update-effect-props';
import {findJsxElementAtNodePath} from './can-update-sequence-props';
-import {formatPropChange} from './log-updates/format-prop-change';
-import {logUpdate, normalizeQuotes} from './log-updates/log-update';
+import {formatEffectPropChange} from './log-updates/format-effect-prop-change';
+import {logEffectUpdate} from './log-updates/log-effect-update';
+import {normalizeQuotes} from './log-updates/log-update';
export const saveEffectPropsHandler: ApiHandler<
SaveEffectPropsRequest,
@@ -51,16 +52,17 @@ export const saveEffectPropsHandler: ApiHandler<
const parsedDefault = defaultValue !== null ? JSON.parse(defaultValue) : null;
- const {output, oldValueString, formatted, logLine} = await updateEffectProps({
- input: fileContents,
- sequenceNodePath: sequenceNodePath.nodePath,
- effectIndex,
- update: {
- key,
- value: JSON.parse(value),
- defaultValue: parsedDefault,
- },
- });
+ const {output, oldValueString, formatted, logLine, effectCallee} =
+ await updateEffectProps({
+ input: fileContents,
+ sequenceNodePath: sequenceNodePath.nodePath,
+ effectIndex,
+ update: {
+ key,
+ value: JSON.parse(value),
+ defaultValue: parsedDefault,
+ },
+ });
const defaultValueString =
parsedDefault !== null ? JSON.stringify(parsedDefault) : null;
@@ -70,7 +72,8 @@ export const saveEffectPropsHandler: ApiHandler<
const normalizedDefault =
defaultValueString !== null ? normalizeQuotes(defaultValueString) : null;
- const undoPropChange = formatPropChange({
+ const undoPropChange = formatEffectPropChange({
+ effectName: effectCallee,
key,
oldValueString: normalizedNew,
newValueString: normalizedOld,
@@ -78,7 +81,8 @@ export const saveEffectPropsHandler: ApiHandler<
removedProps: [],
addedProps: [],
});
- const redoPropChange = formatPropChange({
+ const redoPropChange = formatEffectPropChange({
+ effectName: effectCallee,
key,
oldValueString: normalizedOld,
newValueString: normalizedNew,
@@ -104,10 +108,11 @@ export const saveEffectPropsHandler: ApiHandler<
suppressBundlerUpdateForFile(absolutePath);
writeFileAndNotifyFileWatchers(absolutePath, output);
- logUpdate({
+ logEffectUpdate({
fileRelativeToRoot,
line: logLine,
- key: `[${effectIndex}].${key}`,
+ effectName: effectCallee,
+ propKey: key,
oldValueString,
newValueString: value,
defaultValueString,
diff --git a/packages/studio-server/src/test/log-effect-update.test.ts b/packages/studio-server/src/test/log-effect-update.test.ts
new file mode 100644
index 0000000000..bbcd4ac97f
--- /dev/null
+++ b/packages/studio-server/src/test/log-effect-update.test.ts
@@ -0,0 +1,144 @@
+import {afterAll, beforeAll, expect, spyOn, test} from 'bun:test';
+
+const originalForceColor = process.env.FORCE_COLOR;
+process.env.FORCE_COLOR = '1';
+
+import {RenderInternals} from '@remotion/renderer';
+import {formatEffectPropChange} from '../preview-server/routes/log-updates/format-effect-prop-change';
+import {
+ attrName,
+ numberValue,
+ punctuation,
+ strikeThroughOrRemovedPrefix,
+} from '../preview-server/routes/log-updates/formatting';
+import {logEffectUpdate} from '../preview-server/routes/log-updates/log-effect-update';
+
+const {chalk} = RenderInternals;
+
+beforeAll(() => {
+ process.env.FORCE_COLOR = '1';
+});
+
+afterAll(() => {
+ if (originalForceColor === undefined) {
+ delete process.env.FORCE_COLOR;
+ } else {
+ process.env.FORCE_COLOR = originalForceColor;
+ }
+});
+
+const halftoneCall = (propKey: string, value: string) => {
+ return `${attrName('halftone')}${punctuation('(')}${punctuation('{')}${punctuation(propKey)}${punctuation(': ')}${numberValue(value)}${punctuation('}')}${punctuation(')')}`;
+};
+
+test('logEffectUpdate prints halftone({dotSize}) β†’ halftone({dotSize}) when dotSize changes', () => {
+ const consoleSpy = spyOn(console, 'log').mockImplementation(() => undefined);
+
+ try {
+ logEffectUpdate({
+ fileRelativeToRoot: 'src/HtmlInCanvas/react-svg.tsx',
+ line: 21,
+ effectName: 'halftone',
+ propKey: 'dotSize',
+ oldValueString: '30',
+ newValueString: '52',
+ defaultValueString: null,
+ formatted: true,
+ logLevel: 'info',
+ removedProps: [],
+ addedProps: [],
+ });
+
+ expect(consoleSpy).toHaveBeenCalledTimes(1);
+ const logged = consoleSpy.mock.calls[0].join(' ');
+
+ const expectedPropChange = `${halftoneCall('dotSize', '30')} \u2192 ${halftoneCall('dotSize', '52')}`;
+ const expectedLine = `${chalk.blueBright('src/HtmlInCanvas/react-svg.tsx:21')} ${expectedPropChange}`;
+
+ expect(logged).toBe(expectedLine);
+ } finally {
+ consoleSpy.mockRestore();
+ }
+});
+
+test('logEffectUpdate prints halftone({dotSpacing}) when dotSpacing is added explicitly', () => {
+ const consoleSpy = spyOn(console, 'log').mockImplementation(() => undefined);
+
+ try {
+ logEffectUpdate({
+ fileRelativeToRoot: 'src/HtmlInCanvas/react-svg.tsx',
+ line: 21,
+ effectName: 'halftone',
+ propKey: 'dotSpacing',
+ oldValueString: '20',
+ newValueString: '2',
+ defaultValueString: '20',
+ formatted: true,
+ logLevel: 'info',
+ removedProps: [],
+ addedProps: [],
+ });
+
+ expect(consoleSpy).toHaveBeenCalledTimes(1);
+ const logged = consoleSpy.mock.calls[0].join(' ');
+
+ const addedCall = halftoneCall('dotSpacing', '2');
+ const expectedLine = `${chalk.blueBright('src/HtmlInCanvas/react-svg.tsx:21')} ${addedCall}`;
+
+ expect(logged).toBe(expectedLine);
+ } finally {
+ consoleSpy.mockRestore();
+ }
+});
+
+test('logEffectUpdate strikes only the prop body when removing offsetX to default', () => {
+ const consoleSpy = spyOn(console, 'log').mockImplementation(() => undefined);
+
+ try {
+ logEffectUpdate({
+ fileRelativeToRoot: 'src/HtmlInCanvas/react-svg.tsx',
+ line: 21,
+ effectName: 'halftone',
+ propKey: 'offsetX',
+ oldValueString: '58',
+ newValueString: '0',
+ defaultValueString: '0',
+ formatted: true,
+ logLevel: 'info',
+ removedProps: [],
+ addedProps: [],
+ });
+
+ expect(consoleSpy).toHaveBeenCalledTimes(1);
+ const logged = consoleSpy.mock.calls[0].join(' ');
+
+ const struck = strikeThroughOrRemovedPrefix(
+ `${punctuation('offsetX')}${punctuation(': ')}${numberValue('58')}`,
+ );
+ const expectedPropChange = `${attrName('halftone')}${punctuation('(')}${punctuation('{')}${struck}${punctuation('}')}${punctuation(')')}`;
+ const expectedLine = `${chalk.blueBright('src/HtmlInCanvas/react-svg.tsx:21')} ${expectedPropChange}`;
+
+ expect(logged).toBe(expectedLine);
+ } finally {
+ consoleSpy.mockRestore();
+ }
+});
+
+test('undo of an added effect prop strikes only offsetX inside halftone({…})', () => {
+ const undoPropChange = formatEffectPropChange({
+ effectName: 'halftone',
+ key: 'offsetX',
+ oldValueString: '37',
+ newValueString: '0',
+ defaultValueString: '0',
+ removedProps: [],
+ addedProps: [],
+ });
+
+ const struck = strikeThroughOrRemovedPrefix(
+ `${punctuation('offsetX')}${punctuation(': ')}${numberValue('37')}`,
+ );
+ const expectedUndo = `${attrName('halftone')}${punctuation('(')}${punctuation('{')}${struck}${punctuation('}')}${punctuation(')')}`;
+
+ expect(undoPropChange).toBe(expectedUndo);
+});
diff --git a/packages/studio-server/src/test/log-update.test.ts b/packages/studio-server/src/test/log-update.test.ts
index cdcec3acd0..ebe18169ef 100644
--- a/packages/studio-server/src/test/log-update.test.ts
+++ b/packages/studio-server/src/test/log-update.test.ts
@@ -14,8 +14,6 @@ import {
equals,
numberValue,
punctuation,
- strikeThrough,
- stringValue,
} from '../preview-server/routes/log-updates/formatting';
import {
logUpdate,
@@ -51,8 +49,6 @@ export const LightLeakExample: React.FC = () => {
`;
test('logUpdate emits Monokai-colored output after an AST update', async () => {
- expect(chalk.enabled()).toBe(true);
-
const {output, oldValueStrings, formatted, logLine} =
await updateSequenceProps({
input,
@@ -96,8 +92,6 @@ test('logUpdate emits Monokai-colored output after an AST update', async () => {
});
test('logUpdate emits change-from-default output for discriminated union enum change', async () => {
- expect(chalk.enabled()).toBe(true);
-
const fixture = readFileSync(
path.join(__dirname, 'snapshots', 'discriminated-union-with-style.tsx'),
'utf-8',
@@ -146,32 +140,20 @@ test('logUpdate emits change-from-default output for discriminated union enum ch
expect(consoleSpy).toHaveBeenCalledTimes(1);
const logged = consoleSpy.mock.calls[0].join(' ');
- const simpleProp = (key: string, value: string) =>
- `${attrName(key)}${equals('=')}${punctuation('{')}${stringValue(value)}${punctuation('}')}`;
-
- const simplePropPunctuation = (key: string, value: string) =>
- `${attrName(key)}${equals('=')}${punctuation('{')}${punctuation(value)}${punctuation('}')}`;
-
- const expectedPropChange = `${simpleProp('layout', "'none'")}, ${strikeThrough(simplePropPunctuation('style', '{ scale: 1.74 }'))}`;
- const expectedLine = `${chalk.blueBright(`src/Example.tsx:${logLine}`)} ${expectedPropChange}`;
-
- expect(logged).toBe(expectedLine);
-
- const normalizedOld = normalizeQuotes(oldValueStrings[0]);
- const normalizedNew = normalizeQuotes(newValueString);
- const normalizedDefault = normalizeQuotes(defaultValueString);
-
- const undoPropChange = formatPropChange({
+ const expectedPropChange = formatPropChange({
key: 'layout',
- oldValueString: normalizedNew,
- newValueString: normalizedOld,
- defaultValueString: normalizedDefault,
- removedProps: [],
- addedProps: removedProps,
+ oldValueString: normalizeQuotes(oldValueStrings[0]),
+ newValueString: normalizeQuotes(newValueString),
+ defaultValueString:
+ defaultValueString !== null
+ ? normalizeQuotes(defaultValueString)
+ : null,
+ removedProps,
+ addedProps: [],
});
+ const expectedLine = `${chalk.blueBright(`src/Example.tsx:${logLine}`)} ${expectedPropChange}`;
- const expectedUndoPropChange = `${strikeThrough(simpleProp('layout', "'none'"))}, ${simplePropPunctuation('style', '{ scale: 1.74 }')}`;
- expect(undoPropChange).toBe(expectedUndoPropChange);
+ expect(logged).toBe(expectedLine);
} finally {
consoleSpy.mockRestore();
}
diff --git a/packages/studio-server/src/test/update-effect-props.test.ts b/packages/studio-server/src/test/update-effect-props.test.ts
index 8ac28daabf..11b7550024 100644
--- a/packages/studio-server/src/test/update-effect-props.test.ts
+++ b/packages/studio-server/src/test/update-effect-props.test.ts
@@ -31,13 +31,14 @@ test('updateEffectProps updates an existing prop on the right effect', () => {
test('updateEffectProps adds a missing prop', () => {
const input = buildInput('[tint({color: "red"})]');
- const {serialized} = updateEffectPropsAst({
+ const {serialized, effectCallee} = updateEffectPropsAst({
input,
sequenceNodePath: lineColumnToNodePath(input, 6),
effectIndex: 0,
update: {key: 'opacity', value: 0.25, defaultValue: null},
});
+ expect(effectCallee).toBe('tint');
expect(serialized).toContain('opacity: 0.25');
expect(serialized).toContain('color: "red"');
});