🎥 Make videos programmatically with React
open in file explorer
Jonny Burgerd0b05318e422
diff --git a/packages/cli/src/editor/components/RenderModal/RenderModal.tsx b/packages/cli/src/editor/components/RenderModal/RenderModal.tsxindex 1daaf91bb6..9cd4ac6596 100644--- a/packages/cli/src/editor/components/RenderModal/RenderModal.tsx+++ b/packages/cli/src/editor/components/RenderModal/RenderModal.tsx@@ -41,9 +41,6 @@ type Action =}| {type: 'fail';- }- | {- type: 'reset';};const reducer = (state: State, action: Action): State => {@@ -59,12 +56,6 @@ const reducer = (state: State, action: Action): State => {};}- if (action.type === 'reset') {- return {- type: 'idle',- };- }-if (action.type === 'succeed') {return {type: 'success',@@ -129,11 +120,6 @@ export const RenderModal: React.FC<{composition: TCompMetadata}> = ({}).catch(() => {dispatchIfMounted({type: 'fail'});- })- .finally(() => {- setTimeout(() => {- dispatchIfMounted({type: 'reset'});- }, 2000);});}, [composition.id, dispatchIfMounted, outName, setSelectedModal]);diff --git a/packages/cli/src/editor/components/RenderQueue/RenderQueueItem.tsx b/packages/cli/src/editor/components/RenderQueue/RenderQueueItem.tsxindex 0eedeba045..4e3d3d057a 100644--- a/packages/cli/src/editor/components/RenderQueue/RenderQueueItem.tsx+++ b/packages/cli/src/editor/components/RenderQueue/RenderQueueItem.tsx@@ -1,6 +1,6 @@import React from 'react';import type {RenderJob} from '../../../preview-server/render-queue/job';-import {LIGHT_TEXT} from '../../helpers/colors';+import {RenderQueueOutputName} from './RenderQueueOutputName';const container: React.CSSProperties = {padding: 12,@@ -10,34 +10,13 @@ const title: React.CSSProperties = {fontSize: 14,};-const outputLocation: React.CSSProperties = {- fontSize: 14,- color: LIGHT_TEXT,-};--const formatOutputLocation = (location: string) => {- if (location.startsWith(window.remotion_cwd)) {- const out = location.substring(window.remotion_cwd.length);-- if (out.startsWith('/') || out.startsWith('\\')) {- return out.substring(1);- }-- return out;- }-- return location;-};-export const RenderQueueItem: React.FC<{job: RenderJob;}> = ({job}) => {return (<div style={container}><div style={title}>{job.compositionId}</div>- <div style={outputLocation}>- {formatOutputLocation(job.outputLocation)}- </div>+ <RenderQueueOutputName job={job} /></div>);};diff --git a/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsx b/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsxnew file mode 100644index 0000000000..204226b585--- /dev/null+++ b/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsx@@ -0,0 +1,74 @@+import React, {useCallback} from 'react';+import type {+ OpenInFileExplorerRequest,+ RenderJob,+} from '../../../preview-server/render-queue/job';+import {LIGHT_TEXT} from '../../helpers/colors';+import {notificationCenter} from '../Notifications/NotificationCenter';++const outputLocation: React.CSSProperties = {+ fontSize: 14,+ color: LIGHT_TEXT,+ appearance: 'none',+ border: 'none',+ padding: 0,+ cursor: 'pointer',+};++const formatOutputLocation = (location: string) => {+ if (location.startsWith(window.remotion_cwd)) {+ const out = location.substring(window.remotion_cwd.length);++ if (out.startsWith('/') || out.startsWith('\\')) {+ return out.substring(1);+ }++ return out;+ }++ return location;+};++export const RenderQueueOutputName: React.FC<{+ job: RenderJob;+}> = ({job}) => {+ const onClick = useCallback(() => {+ const body: OpenInFileExplorerRequest = {+ directory: job.outputLocation,+ };+ fetch(`/api/open-in-file-explorer`, {+ method: 'post',+ headers: {+ 'content-type': 'application/json',+ },+ body: JSON.stringify(body),+ })+ .then((res) => res.json())+ .then((data: {success: true} | {success: false; error: string}) => {+ if (data.success) {+ console.log('Opened file in explorer');+ } else {+ notificationCenter.current?.addNotification({+ content: `Could not open file: ${data.error}`,+ created: Date.now(),+ duration: 2000,+ id: String(Math.random()),+ });+ }+ })+ .catch((err) => {+ notificationCenter.current?.addNotification({+ content: `Could not open file: ${err.message}`,+ created: Date.now(),+ duration: 2000,+ id: String(Math.random()),+ });+ });+ }, [job.outputLocation]);++ return (+ <button onClick={onClick} type="button" style={outputLocation}>+ {formatOutputLocation(job.outputLocation)}+ </button>+ );+};diff --git a/packages/cli/src/preview-server/render-queue/job.ts b/packages/cli/src/preview-server/render-queue/job.tsindex 6e40c92f8c..df6458c440 100644--- a/packages/cli/src/preview-server/render-queue/job.ts+++ b/packages/cli/src/preview-server/render-queue/job.ts@@ -11,3 +11,8 @@ export type AddRenderRequest = {type: 'still' | 'composition';outName: string;};++export type OpenInFileExplorerRequest = {+ // TODO: Don't allow paths outside Remotion directory+ directory: string;+};diff --git a/packages/cli/src/preview-server/render-queue/open-directory-in-finder.ts b/packages/cli/src/preview-server/render-queue/open-directory-in-finder.tsnew file mode 100644index 0000000000..663246b6a9--- /dev/null+++ b/packages/cli/src/preview-server/render-queue/open-directory-in-finder.ts@@ -0,0 +1,32 @@+import {spawn} from 'child_process';+import {platform} from 'os';++export const openDirectoryInFinder = (dirToOpen: string) => {+ const command =+ platform() === 'darwin'+ ? 'open'+ : platform() === 'linux'+ ? 'xdg-open'+ : 'explorer';++ // TODO: What if file was deleted in the meanwhile?++ // TODO: '-R' only works on macOS+ const p = spawn(command, ['-R', dirToOpen]);++ return new Promise<void>((resolve, reject) => {+ p.on('exit', (code) => {+ if (code === 0) {+ resolve();+ } else {+ reject(new Error(`Exited with code ${code}`));+ }+ });+ p.on('error', (err) => {+ p.kill();+ if (err) {+ reject(err);+ }+ });+ });+};diff --git a/packages/cli/src/preview-server/render-queue/open-in-file-explorer.ts b/packages/cli/src/preview-server/render-queue/open-in-file-explorer.tsnew file mode 100644index 0000000000..d338a76327--- /dev/null+++ b/packages/cli/src/preview-server/render-queue/open-in-file-explorer.ts@@ -0,0 +1,40 @@+import type {IncomingMessage, ServerResponse} from 'http';+import {parseRequestBody} from '../parse-body';+import type {OpenInFileExplorerRequest} from './job';+import {openDirectoryInFinder} from './open-directory-in-finder';++export const handleOpenInFileExplorer = async (+ remotionRoot: string,+ req: IncomingMessage,+ res: ServerResponse+) => {+ if (req.method === 'OPTIONS') {+ res.statusCode = 200;+ res.end();+ }++ try {+ const body = (await parseRequestBody(req)) as OpenInFileExplorerRequest;++ await openDirectoryInFinder(body.directory);++ res.setHeader('content-type', 'application/json');+ res.writeHead(200);++ res.end(+ JSON.stringify({+ success: true,+ })+ );+ } catch (err) {+ res.setHeader('content-type', 'application/json');+ res.writeHead(200);++ res.end(+ JSON.stringify({+ success: false,+ error: (err as Error).message,+ })+ );+ }+};diff --git a/packages/cli/src/preview-server/routes.ts b/packages/cli/src/preview-server/routes.tsindex 9bc450f4cc..6fa6a85829 100644--- a/packages/cli/src/preview-server/routes.ts+++ b/packages/cli/src/preview-server/routes.ts@@ -16,6 +16,7 @@ import {parseRequestBody} from './parse-body';import {getProjectInfo} from './project-info';import {jobQueue} from './render-queue';import {handleAddRender} from './render-queue/add-render';+import {handleOpenInFileExplorer} from './render-queue/open-in-file-explorer';import {serveStatic} from './serve-static';import {isUpdateAvailableWithTimeout} from './update-available';@@ -221,6 +222,10 @@ export const handleRoutes = ({return handleOpenInEditor(remotionRoot, request, response);}+ if (url.pathname === '/api/open-in-file-explorer') {+ return handleOpenInFileExplorer(remotionRoot, request, response);+ }+if (url.pathname === '/api/render') {return handleAddRender(remotionRoot, request, response);}