Skip to content

πŸŽ₯ Make videos programmatically with React

@remotion/studio-server: Condense log update output (#8246)

Jonny Burger7c311607cc20
diff --git a/packages/studio-server/src/preview-server/routes/log-updates/format-prop-change.ts b/packages/studio-server/src/preview-server/routes/log-updates/format-prop-change.ts
index 2e9040106b..091f92b4d1 100644
--- a/packages/studio-server/src/preview-server/routes/log-updates/format-prop-change.ts
+++ b/packages/studio-server/src/preview-server/routes/log-updates/format-prop-change.ts
@@ -2,7 +2,7 @@ import {formatSideProps} from './format-side-props';
import {
formatAddition,
formatDeletion,
- formatPropDelta,
+ formatPropChangeDelta,
type PropDelta,
} from './formatting';
@@ -25,7 +25,7 @@ const formatInnerPropChange = ({
return formatAddition({valueString: newValueString, key});
}
- return `${formatPropDelta({valueString: oldValueString, key})} \u2192 ${formatPropDelta({valueString: newValueString, key})}`;
+ return formatPropChangeDelta({key, oldValueString, newValueString});
};
export const formatPropChange = ({
diff --git a/packages/studio-server/src/preview-server/routes/log-updates/formatting.ts b/packages/studio-server/src/preview-server/routes/log-updates/formatting.ts
index 030a968978..6dd5bd47b8 100644
--- a/packages/studio-server/src/preview-server/routes/log-updates/formatting.ts
+++ b/packages/studio-server/src/preview-server/routes/log-updates/formatting.ts
@@ -60,6 +60,46 @@ const formatNestedProp = (
return `${attrName(parentKey)}${equals('=')}${punctuation('{{')}${punctuation(childKey)}${punctuation(':')} ${colorValue(value)}${punctuation('}}')}`;
};
+const formatValueDelta = ({
+ oldValueString,
+ newValueString,
+}: {
+ oldValueString: string;
+ newValueString: string;
+}) => {
+ const shortened = shortenUnchangedInterpolateOptions({
+ oldValueString,
+ newValueString,
+ });
+
+ return `${colorValue(shortened.oldValueString)} ${punctuation('β†’')} ${colorValue(shortened.newValueString)}`;
+};
+
+const formatSimplePropChange = ({
+ key,
+ oldValueString,
+ newValueString,
+}: {
+ key: string;
+ oldValueString: string;
+ newValueString: string;
+}) => {
+ return `${attrName(key)}${equals('=')}${punctuation('{')}${formatValueDelta({oldValueString, newValueString})}${punctuation('}')}`;
+};
+
+const formatNestedPropChange = ({
+ key,
+ oldValueString,
+ newValueString,
+}: {
+ key: string;
+ oldValueString: string;
+ newValueString: string;
+}) => {
+ const dotIdx = key.indexOf('.');
+ return `${attrName(key.slice(0, dotIdx))}${equals('=')}${punctuation('{{')}${punctuation(key.slice(dotIdx + 1))}${punctuation(':')} ${formatValueDelta({oldValueString, newValueString})}${punctuation('}}')}`;
+};
+
export const formatPropDelta = ({key, valueString}: PropDelta) => {
const dotIdx = key.indexOf('.');
if (dotIdx === -1) {
@@ -73,6 +113,23 @@ export const formatPropDelta = ({key, valueString}: PropDelta) => {
);
};
+export const formatPropChangeDelta = ({
+ key,
+ oldValueString,
+ newValueString,
+}: {
+ key: string;
+ oldValueString: string;
+ newValueString: string;
+}) => {
+ const dotIdx = key.indexOf('.');
+ if (dotIdx === -1) {
+ return formatSimplePropChange({key, oldValueString, newValueString});
+ }
+
+ return formatNestedPropChange({key, oldValueString, newValueString});
+};
+
export const formatDeletion = (prop: PropDelta) => {
const formatted = formatPropDelta(prop);
return strikeThroughOrRemovedPrefix(formatted);
@@ -82,3 +139,141 @@ export const formatAddition = (prop: PropDelta) => {
const formatted = formatPropDelta(prop);
return addedPrefixIfNoColor(formatted);
};
+
+const callStart = 'interpolate(';
+
+type InterpolateCall = {
+ args: string[];
+};
+
+const normalizeArg = (arg: string) => {
+ return arg
+ .replace(/\s+/g, ' ')
+ .replace(/,(\s*[}\]])/g, '$1')
+ .trim();
+};
+
+const splitTopLevelArgs = (argsSource: string) => {
+ const args: string[] = [];
+ let depth = 0;
+ let quote: "'" | '"' | '`' | null = null;
+ let start = 0;
+
+ for (let i = 0; i < argsSource.length; i++) {
+ const char = argsSource[i];
+ const previous = argsSource[i - 1];
+
+ if (quote) {
+ if (char === quote && previous !== '\\') {
+ quote = null;
+ }
+
+ continue;
+ }
+
+ if (char === "'" || char === '"' || char === '`') {
+ quote = char;
+ continue;
+ }
+
+ if (char === '(' || char === '[' || char === '{') {
+ depth++;
+ continue;
+ }
+
+ if (char === ')' || char === ']' || char === '}') {
+ depth--;
+ continue;
+ }
+
+ if (char === ',' && depth === 0) {
+ args.push(argsSource.slice(start, i).trim());
+ start = i + 1;
+ }
+ }
+
+ args.push(argsSource.slice(start).trim());
+ return args;
+};
+
+const parseInterpolateCall = (valueString: string): InterpolateCall | null => {
+ const trimmed = valueString.trim();
+ if (!trimmed.startsWith(callStart) || !trimmed.endsWith(')')) {
+ return null;
+ }
+
+ let depth = 0;
+ let quote: "'" | '"' | '`' | null = null;
+
+ for (let i = 'interpolate'.length; i < trimmed.length; i++) {
+ const char = trimmed[i];
+ const previous = trimmed[i - 1];
+
+ if (quote) {
+ if (char === quote && previous !== '\\') {
+ quote = null;
+ }
+
+ continue;
+ }
+
+ if (char === "'" || char === '"' || char === '`') {
+ quote = char;
+ continue;
+ }
+
+ if (char === '(' || char === '[' || char === '{') {
+ depth++;
+ continue;
+ }
+
+ if (char === ')' || char === ']' || char === '}') {
+ depth--;
+ if (depth === 0 && i !== trimmed.length - 1) {
+ return null;
+ }
+ }
+ }
+
+ if (depth !== 0) {
+ return null;
+ }
+
+ return {
+ args: splitTopLevelArgs(trimmed.slice(callStart.length, -1)),
+ };
+};
+
+const shortenUnchangedInterpolateOptions = ({
+ oldValueString,
+ newValueString,
+}: {
+ oldValueString: string;
+ newValueString: string;
+}) => {
+ const oldCall = parseInterpolateCall(oldValueString);
+ const newCall = parseInterpolateCall(newValueString);
+
+ if (
+ !oldCall ||
+ !newCall ||
+ oldCall.args.length <= 3 ||
+ newCall.args.length <= 3
+ ) {
+ return {oldValueString, newValueString};
+ }
+
+ const oldOptions = oldCall.args.slice(3).map(normalizeArg);
+ const newOptions = newCall.args.slice(3).map(normalizeArg);
+ if (
+ oldOptions.length !== newOptions.length ||
+ oldOptions.some((option, index) => option !== newOptions[index])
+ ) {
+ return {oldValueString, newValueString};
+ }
+
+ return {
+ oldValueString: `interpolate(${oldCall.args.slice(0, 3).join(', ')})`,
+ newValueString: `interpolate(${newCall.args.slice(0, 3).join(', ')})`,
+ };
+};
diff --git a/packages/studio-server/src/test/log-update.test.ts b/packages/studio-server/src/test/log-update.test.ts
index 03c5d03ebc..fcafe759f6 100644
--- a/packages/studio-server/src/test/log-update.test.ts
+++ b/packages/studio-server/src/test/log-update.test.ts
@@ -81,9 +81,12 @@ test('logUpdate emits Monokai-colored output after an AST update', async () => {
const logged = consoleSpy.mock.calls[0].join(' ');
const simpleProp = (key: string, value: string) =>
- `${attrName(key)}${equals('=')}${punctuation('{')}${numberValue(value)}${punctuation('}')}`;
+ `${attrName(key)}${equals('=')}${punctuation('{')}${value}${punctuation('}')}`;
- const expectedPropChange = `${simpleProp('hueShift', '30')} β†’ ${simpleProp('hueShift', '90')}`;
+ const expectedPropChange = simpleProp(
+ 'hueShift',
+ `${numberValue('30')} β†’ ${numberValue('90')}`,
+ );
const expectedLine = `${chalk.blueBright('src/Example.tsx:8')} ${expectedPropChange}`;
expect(logged).toBe(expectedLine);
@@ -92,6 +95,29 @@ test('logUpdate emits Monokai-colored output after an AST update', async () => {
}
});
+test('formatPropChange condenses unchanged interpolate options', () => {
+ const formatted = formatPropChange({
+ key: 'width',
+ oldValueString: `interpolate(frame, [78], [244], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+})`,
+ newValueString: `interpolate(frame, [29, 78], [88, 244], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+})`,
+ defaultValueString: null,
+ removedProps: [],
+ addedProps: [],
+ });
+
+ expect(formatted).toBe(
+ `${attrName('width')}${equals('=')}${punctuation('{')}interpolate(frame, [78], [244]) β†’ interpolate(frame, [29, 78], [88, 244])${punctuation('}')}`,
+ );
+ expect(formatted).not.toContain('extrapolateLeft');
+ expect(formatted).not.toContain('extrapolateRight');
+});
+
test('logUpdate emits change-from-default output for discriminated union enum change', async () => {
const fixture = readFileSync(
path.join(__dirname, 'snapshots', 'discriminated-union-with-style.tsx'),