Skip to content

🎥 Make videos programmatically with React

4.6 KB
Raw Blame
import {readFileSync} from 'node:fs';
import {duplicateNodes} from '@remotion/codemods';
import {RenderInternals} from '@remotion/renderer';
import type {
	DuplicateNodesRequest,
	DuplicateNodesResponse,
} from '@remotion/studio-shared';
import {writeFileAndNotifyFileWatchers} from '../../file-watcher';
import {resolveFileInsideProject} from '../../helpers/resolve-file-inside-project';
import type {ApiHandler} from '../api-types';
import {formatLogFileLocation} from '../format-log-file-location';
import {broadcastSequenceNodePathMutation} from '../sequence-node-path-mutation';
import {
	printUndoHint,
	pushTransactionToUndoStack,
	suppressUndoStackInvalidation,
} from '../undo-stack';
import {
	getCodemodTimingPrefix,
	withSourceFileWriteQueue,
} from './source-file-write-queue';

export const duplicateNodesHandler: ApiHandler<
	DuplicateNodesRequest,
	DuplicateNodesResponse
> = ({input: {nodes}, remotionRoot, logLevel}) => {
	return withSourceFileWriteQueue(async () => {
		try {
			if (nodes.length === 0) {
				throw new Error('No JSX nodes were specified for duplication');
			}

			RenderInternals.Log.trace(
				{indent: false, logLevel},
				`[duplicate-nodes] Received request to duplicate ${nodes.length} JSX node${nodes.length === 1 ? '' : 's'}`,
			);

			const itemsByFileName = new Map<string, typeof nodes>();
			for (const item of nodes) {
				const fileItems = itemsByFileName.get(item.fileName) ?? [];
				fileItems.push(item);
				itemsByFileName.set(item.fileName, fileItems);
			}

			const updates = await Promise.all(
				[...itemsByFileName.entries()].map(async ([fileName, fileItems]) => {
					const {absolutePath, fileRelativeToRoot} = resolveFileInsideProject({
						remotionRoot,
						fileName,
						action: 'modify',
					});
					const fileContents = readFileSync(absolutePath, 'utf-8');
					const result = await duplicateNodes({
						project: {
							files: {[absolutePath]: fileContents},
							rootDir: remotionRoot,
						},
						nodes: fileItems.map((item) => ({
							filePath: absolutePath,
							nodePath: item.nodePath,
						})),
					});
					const output = result.changes[0]?.nextContents ?? fileContents;
					const {nodeLabels, logLines} = result.editDetails[0];
					const nodePathRemappings = result.nodePathRemappings.map(
						({oldNodePath, newNodePath, oldJsxName, newJsxName}) => ({
							oldNodePath,
							newNodePath,
							oldJsxName,
							newJsxName,
						}),
					);

					return {
						absolutePath,
						fileRelativeToRoot,
						fileContents,
						logLine: Math.min(...logLines),
						nodeLabels,
						nodePathRemappings,
						output,
					};
				}),
			);
			const nodePathMutation = broadcastSequenceNodePathMutation(
				updates.map((update) => ({
					absolutePath: update.absolutePath,
					remappings: update.nodePathRemappings,
				})),
				null,
			);
			const duplicatedNodeDescription =
				nodes.length === 1
					? updates[0].nodeLabels[0]
					: `${nodes.length} JSX nodes`;

			pushTransactionToUndoStack({
				snapshots: updates.map((update) => ({
					filePath: update.absolutePath,
					oldContents: update.fileContents,
					newContents: null,
					logLine: update.logLine,
					nodePathRemappings: update.nodePathRemappings,
				})),
				logLevel,
				remotionRoot,
				description: {
					undoMessage: `↩️  Duplication of ${duplicatedNodeDescription}`,
					redoMessage: `↪️  Duplication of ${duplicatedNodeDescription}`,
				},
				entryType: 'duplicate-nodes',
				suppressHmrOnFileRestore: false,
				undoRedoNavigation: null,
			});

			for (const update of updates) {
				suppressUndoStackInvalidation(update.absolutePath);
				writeFileAndNotifyFileWatchers({
					file: update.absolutePath,
					content: update.output,
					originatorClientId: undefined,
					metadata: {skipSequencePropsUpdate: true},
				});

				const locationLabel = formatLogFileLocation({
					remotionRoot,
					absolutePath: update.absolutePath,
					line: update.logLine,
				});
				const fileDescription =
					update.nodeLabels.length === 1
						? update.nodeLabels[0]
						: `${update.nodeLabels.length} JSX nodes`;
				RenderInternals.Log.info(
					{indent: false, logLevel},
					`${getCodemodTimingPrefix(logLevel)}${RenderInternals.chalk.blueBright(`${locationLabel}`)} Duplicated ${fileDescription}`,
				);

				RenderInternals.Log.verbose(
					{indent: false, logLevel},
					`[duplicate-nodes] Wrote ${update.fileRelativeToRoot}`,
				);
			}

			printUndoHint(logLevel);

			return {
				success: true,
				nodePathMutation,
			};
		} catch (err) {
			return {
				success: false,
				reason: (err as Error).message,
				stack: (err as Error).stack as string,
			};
		}
	});
};