🎥 Make videos programmatically with React
handle files being deleted
Jonny Burgerb3c8943c09b9
diff --git a/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsx b/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsxindex 84b6ae9f32..4b151888fa 100644--- a/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsx+++ b/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsx@@ -1,12 +1,8 @@-import React, {useCallback} from 'react';+import React, {useCallback, useMemo} from 'react';import type {RenderJob} from '../../../preview-server/render-queue/job';import {openInFileExplorer} from './actions';import {renderQueueItemSubtitleStyle} from './item-style';-const outputLocation: React.CSSProperties = {- ...renderQueueItemSubtitleStyle,-};-const formatOutputLocation = (location: string) => {if (location.startsWith(window.remotion_cwd)) {const out = location.substring(window.remotion_cwd.length);@@ -28,12 +24,23 @@ export const RenderQueueOutputName: React.FC<{openInFileExplorer({directory: job.outputLocation});}, [job.outputLocation]);+ const style = useMemo((): React.CSSProperties => {+ return {+ ...renderQueueItemSubtitleStyle,+ cursor: job.deletedOutputLocation ? 'inherit' : 'pointer',+ textDecoration: job.deletedOutputLocation ? 'line-through' : 'none',+ };+ }, [job.deletedOutputLocation]);+return (<buttononClick={onClick}type="button"- style={outputLocation}- title={job.outputLocation}+ style={style}+ disabled={job.deletedOutputLocation}+ title={+ job.deletedOutputLocation ? 'File was deleted' : job.outputLocation+ }>{formatOutputLocation(job.outputLocation)}</button>diff --git a/packages/cli/src/preview-server/render-queue/add-render.ts b/packages/cli/src/preview-server/render-queue/add-render.tsindex 32fd2528e1..00d98021f8 100644--- a/packages/cli/src/preview-server/render-queue/add-render.ts+++ b/packages/cli/src/preview-server/render-queue/add-render.ts@@ -35,6 +35,8 @@ export const handleAddRender = async (quality: body.quality,frame: body.frame,scale: body.scale,+ cleanup: [],+ deletedOutputLocation: false,},entryPoint,remotionRoot,diff --git a/packages/cli/src/preview-server/render-queue/job.ts b/packages/cli/src/preview-server/render-queue/job.tsindex a4b6f438a9..2759aa7b76 100644--- a/packages/cli/src/preview-server/render-queue/job.ts+++ b/packages/cli/src/preview-server/render-queue/job.ts@@ -35,9 +35,14 @@ export type RenderJob = {compositionId: string;id: string;outputLocation: string;+ deletedOutputLocation: boolean;} & RenderJobDynamicStatus &RenderJobDynamicFields;+export type RenderJobWithCleanup = RenderJob & {+ cleanup: (() => void)[];+};+type AddRenderRequestDynamicFields =| {type: 'still';diff --git a/packages/cli/src/preview-server/render-queue/queue.ts b/packages/cli/src/preview-server/render-queue/queue.tsindex 85f5026439..b99e9d39bc 100644--- a/packages/cli/src/preview-server/render-queue/queue.ts+++ b/packages/cli/src/preview-server/render-queue/queue.ts@@ -5,15 +5,19 @@ import {renderStill,} from '@remotion/renderer';import {ConfigInternals} from '../../config';+import {installFileWatcher} from '../../file-watcher';import {getCliOptions} from '../../get-cli-options';import {Log} from '../../log';import {bundleOnCli} from '../../setup-cache';import {waitForLiveEventsListener} from '../live-events';-import type {RenderJob} from './job';+import type {RenderJob, RenderJobWithCleanup} from './job';-let jobQueue: RenderJob[] = [];+let jobQueue: RenderJobWithCleanup[] = [];-const updateJob = (id: string, updater: (job: RenderJob) => RenderJob) => {+const updateJob = (+ id: string,+ updater: (job: RenderJobWithCleanup) => RenderJobWithCleanup+) => {jobQueue = jobQueue.map((j) => {if (id === j.id) {return updater(j);@@ -24,15 +28,18 @@ const updateJob = (id: string, updater: (job: RenderJob) => RenderJob) => {notifyClientsOfJobUpdate();};-export const getRenderQueue = () => {- return jobQueue;+export const getRenderQueue = (): RenderJob[] => {+ return jobQueue.map((j) => {+ const {cleanup, ...rest} = j;+ return rest;+ });};export const notifyClientsOfJobUpdate = () => {waitForLiveEventsListener().then((listener) => {listener.sendEventToClient({type: 'render-queue-updated',- queue: jobQueue,+ queue: getRenderQueue(),});});};@@ -134,7 +141,7 @@ export const addJob = ({entryPoint,remotionRoot,}: {- job: RenderJob;+ job: RenderJobWithCleanup;entryPoint: string;remotionRoot: string;}) => {@@ -145,7 +152,16 @@ export const addJob = ({};export const removeJob = (jobId: string) => {- jobQueue = jobQueue.filter((job) => job.id !== jobId);+ jobQueue = jobQueue.filter((job) => {+ if (job.id === jobId) {+ job.cleanup.forEach((c) => {+ c();+ });+ return false;+ }++ return true;+ });notifyClientsOfJobUpdate();};@@ -172,12 +188,29 @@ export const processJobIfPossible = async ({};});await processJob({job: nextJob, entryPoint, remotionRoot});- updateJob(nextJob.id, (job) => {- return {- ...job,- status: 'done',- };+ const unwatch = installFileWatcher({+ file: nextJob.outputLocation,+ onChange: (type) => {+ if (type === 'created') {+ updateJob(nextJob.id, (job) => ({+ ...job,+ deletedOutputLocation: false,+ }));+ }++ if (type === 'deleted') {+ updateJob(nextJob.id, (job) => ({+ ...job,+ deletedOutputLocation: true,+ }));+ }+ },});+ updateJob(nextJob.id, (job) => ({+ ...job,+ status: 'done',+ cleanup: [...job.cleanup, unwatch],+ }));} catch (err) {Log.error('Job failed: ', err);updateJob(nextJob.id, (job) => {