π₯ Make videos programmatically with React
cool
Jonny Burgerc674a782a361
diff --git a/packages/studio-server/src/preview-server/routes/log-update.ts b/packages/studio-server/src/preview-server/routes/log-update.tsindex acfd832d42..b114a12fe7 100644--- a/packages/studio-server/src/preview-server/routes/log-update.ts+++ b/packages/studio-server/src/preview-server/routes/log-update.ts@@ -30,10 +30,11 @@ export const normalizeQuotes = (str: string): string => {};// 24-bit ANSI helpers-const fg = (r: number, g: number, b: number, str: string) =>+export const fg = (r: number, g: number, b: number, str: string) =>`\u001b[38;2;${r};${g};${b}m${str}\u001b[39m`;-const bg = (r: number, g: number, b: number, str: string) =>+export const bg = (r: number, g: number, b: number, str: string) =>`\u001b[48;2;${r};${g};${b}m${str}\u001b[49m`;+export const strikeThrough = (str: string) => `\u001b[9m${str}\u001b[29m`;// Monokai-inspired syntax colorsconst attrName = (str: string) => fg(166, 226, 46, str);@@ -57,10 +58,6 @@ const colorValue = (str: string) => {return punctuation(str);};-// Subtle background tints-const removedBg = (str: string) => bg(80, 20, 20, str);-const addedBg = (str: string) => bg(30, 80, 30, str);-const colorEnabled = () => RenderInternals.chalk.enabled();// Format key={value} with Monokai syntax highlighting@@ -99,36 +96,25 @@ export const formatPropChange = ({return `${parent}={{${child}: ${oldValueString}}} \u2192 ${parent}={{${child}: ${newValueString}}}`;}- const isResetToDefault =- defaultValueString !== null && newValueString === defaultValueString;- const isChangeFromDefault =- defaultValueString !== null && oldValueString === defaultValueString;-const dotIndex = key.indexOf('.');- if (dotIndex === -1) {- if (isResetToDefault) {- return removedBg(formatSimpleProp(key, oldValueString));- }-- if (isChangeFromDefault) {- return addedBg(formatSimpleProp(key, newValueString));- }-- return `${removedBg(formatSimpleProp(key, oldValueString))} \u2192 ${addedBg(formatSimpleProp(key, newValueString))}`;- }-- const parentKey = key.slice(0, dotIndex);- const childKey = key.slice(dotIndex + 1);-- if (isResetToDefault) {- return removedBg(formatNestedProp(parentKey, childKey, oldValueString));+ const formatProp = (value: string) =>+ dotIndex === -1+ ? formatSimpleProp(key, value)+ : formatNestedProp(+ key.slice(0, dotIndex),+ key.slice(dotIndex + 1),+ value,+ );++ if (defaultValueString !== null && newValueString === defaultValueString) {+ return strikeThrough(formatProp(oldValueString));}- if (isChangeFromDefault) {- return addedBg(formatNestedProp(parentKey, childKey, newValueString));+ if (defaultValueString !== null && oldValueString === defaultValueString) {+ return formatProp(newValueString);}- return `${removedBg(formatNestedProp(parentKey, childKey, oldValueString))} \u2192 ${addedBg(formatNestedProp(parentKey, childKey, newValueString))}`;+ return `${formatProp(oldValueString)} \u2192 ${formatProp(newValueString)}`;};export const logUpdate = ({diff --git a/packages/studio-server/src/test/log-update.test.ts b/packages/studio-server/src/test/log-update.test.tsnew file mode 100644index 0000000000..94fe88899c--- /dev/null+++ b/packages/studio-server/src/test/log-update.test.ts@@ -0,0 +1,152 @@+import {afterAll, beforeAll, expect, spyOn, test} from 'bun:test';++const originalForceColor = process.env.FORCE_COLOR;+process.env.FORCE_COLOR = '1';++import {readFileSync} from 'node:fs';+import path from 'node:path';+import {RenderInternals} from '@remotion/renderer';+import {Internals} from 'remotion';+import {updateSequenceProps} from '../codemods/update-sequence-props';+import {bg, fg, logUpdate} from '../preview-server/routes/log-update';+import {lineColumnToNodePath} from './test-utils';++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 input = `import {LightLeak} from '@remotion/light-leaks';+import React from 'react';+import {AbsoluteFill} from 'remotion';++export const LightLeakExample: React.FC = () => {+ return (+ <AbsoluteFill style={{backgroundColor: 'black'}}>+ <LightLeak durationInFrames={60} seed={1} hueShift={30} />+ </AbsoluteFill>+ );+};+`;++test('logUpdate emits Monokai-colored output after an AST update', async () => {+ expect(chalk.enabled()).toBe(true);++ const {output, oldValueStrings, formatted, logLine} =+ await updateSequenceProps({+ input,+ nodePath: lineColumnToNodePath(input, 8),+ updates: [{key: 'hueShift', value: 90, defaultValue: null}],+ schema: Internals.sequenceSchema,+ });++ expect(oldValueStrings[0]).toBe('30');+ expect(output).toContain('hueShift={90}');++ const consoleSpy = spyOn(console, 'log').mockImplementation(() => undefined);++ try {+ logUpdate({+ fileRelativeToRoot: 'src/Example.tsx',+ line: logLine,+ key: 'hueShift',+ oldValueString: oldValueStrings[0],+ newValueString: '90',+ defaultValueString: null,+ formatted,+ logLevel: 'info',+ });++ expect(consoleSpy).toHaveBeenCalledTimes(1);+ const logged = consoleSpy.mock.calls[0].join(' ');++ // Mirror the Monokai palette from log-update.ts.+ const attrName = (s: string) => fg(166, 226, 46, s);+ const equals = (s: string) => fg(249, 38, 114, s);+ const punctuation = (s: string) => fg(248, 248, 242, s);+ const numberValue = (s: string) => fg(174, 129, 255, s);+ const removedBg = (s: string) => bg(80, 20, 20, s);+ const addedBg = (s: string) => bg(30, 80, 30, s);++ const simpleProp = (key: string, value: string) =>+ `${attrName(key)}${equals('=')}${punctuation('{')}${numberValue(value)}${punctuation('}')}`;++ const expectedPropChange = `${removedBg(simpleProp('hueShift', '30'))} β ${addedBg(simpleProp('hueShift', '90'))}`;+ const expectedLine = `${chalk.blueBright('src/Example.tsx:8:')} ${expectedPropChange}`;++ expect(logged).toBe(expectedLine);+ } finally {+ consoleSpy.mockRestore();+ }+});++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',+ );++ const {oldValueStrings, formatted, logLine} = await updateSequenceProps({+ input: fixture,+ nodePath: lineColumnToNodePath(fixture, 3),+ updates: [+ {+ key: 'layout',+ value: 'none',+ defaultValue: Internals.sequenceSchema.layout.default,+ },+ ],+ schema: Internals.sequenceSchema,+ });++ expect(oldValueStrings[0]).toBe('"absolute-fill"');++ const newValueString = JSON.stringify('none');+ const defaultValueString = JSON.stringify(+ Internals.sequenceSchema.layout.default,+ );++ const consoleSpy = spyOn(console, 'log').mockImplementation(() => undefined);++ try {+ logUpdate({+ fileRelativeToRoot: 'src/Example.tsx',+ line: logLine,+ key: 'layout',+ oldValueString: oldValueStrings[0],+ newValueString,+ defaultValueString,+ formatted,+ logLevel: 'info',+ });++ expect(consoleSpy).toHaveBeenCalledTimes(1);+ const logged = consoleSpy.mock.calls[0].join(' ');++ const attrName = (s: string) => fg(166, 226, 46, s);+ const equals = (s: string) => fg(249, 38, 114, s);+ const punctuation = (s: string) => fg(248, 248, 242, s);+ const stringValue = (s: string) => fg(230, 219, 116, s);++ const simpleProp = (key: string, value: string) =>+ `${attrName(key)}${equals('=')}${punctuation('{')}${stringValue(value)}${punctuation('}')}`;++ const expectedPropChange = simpleProp('layout', "'none'");+ const expectedLine = `${chalk.blueBright(`src/Example.tsx:${logLine}:`)} ${expectedPropChange}`;++ expect(logged).toBe(expectedLine);+ } finally {+ consoleSpy.mockRestore();+ }+});