Skip to content

🎥 Make videos programmatically with React

Share Studio app opening operations with terminal shortcuts

Jonny Burgerf13314ebcbc2
diff --git a/packages/studio-server/src/helpers/open-in-app.ts b/packages/studio-server/src/helpers/open-in-app.ts
new file mode 100644
index 0000000000..bda5c4919c
--- /dev/null
+++ b/packages/studio-server/src/helpers/open-in-app.ts
@@ -0,0 +1,113 @@
+import path from 'node:path';
+import {
+ defaultEditorIds,
+ type BuiltInEditor,
+ type DefaultEditor,
+ type LogLevel,
+} from '@remotion/renderer';
+import type {
+ OpenInCodingAgentRequest,
+ OpenInCodingAgentResponse,
+ OpenInEditorRequest,
+ OpenInEditorResponse,
+} from '@remotion/studio-shared';
+import {
+ getAvailableCodingAgents,
+ launchCodingAgent,
+} from './coding-agent-registry';
+import {launchCustomEditor} from './custom-editor';
+import {launchEditor} from './open-in-editor';
+import {resolveEditor} from './resolve-editor';
+
+export const openInEditor = async ({
+ input,
+ remotionRoot,
+ logLevel,
+ getDefaultEditor,
+}: {
+ input: OpenInEditorRequest;
+ remotionRoot: string;
+ logLevel: LogLevel;
+ getDefaultEditor: () => DefaultEditor | null;
+}): Promise<OpenInEditorResponse> => {
+ try {
+ if (!('stack' in input)) {
+ throw new TypeError('Need to pass stack');
+ }
+
+ const {stack} = input;
+ if (
+ input.editorId !== null &&
+ input.editorId !== 'custom' &&
+ !defaultEditorIds.includes(input.editorId as BuiltInEditor)
+ ) {
+ return {success: false};
+ }
+
+ const editor = await resolveEditor({
+ defaultEditor: getDefaultEditor(),
+ logLevel,
+ preferredEditor: input.editorId,
+ });
+ if (!editor) {
+ return {success: false};
+ }
+
+ const fileName = path.resolve(
+ remotionRoot,
+ stack.originalFileName as string,
+ );
+ const didOpen =
+ editor.type === 'custom'
+ ? await launchCustomEditor({
+ editor: editor.editor,
+ resolvedExecutable: editor,
+ targetPath: fileName,
+ lineNumber: stack.originalLineNumber as number,
+ columnNumber: stack.originalColumnNumber as number,
+ logLevel,
+ spawnProcess: null,
+ })
+ : await launchEditor(
+ {
+ colNumber: stack.originalColumnNumber as number,
+ editor,
+ fileName,
+ lineNumber: stack.originalLineNumber as number,
+ vsCodeNewWindow: false,
+ logLevel,
+ },
+ remotionRoot,
+ );
+
+ return {success: didOpen};
+ } catch {
+ return {success: false};
+ }
+};
+
+export const openInCodingAgent = async ({
+ input,
+ logLevel,
+ remotionRoot,
+}: {
+ input: OpenInCodingAgentRequest;
+ logLevel: LogLevel;
+ remotionRoot: string;
+}): Promise<OpenInCodingAgentResponse> => {
+ const codingAgent = (await getAvailableCodingAgents()).find(
+ (agent) => agent.id === input.codingAgentId,
+ );
+ if (!codingAgent) {
+ return {success: false};
+ }
+
+ return {
+ success: await launchCodingAgent({
+ codingAgent,
+ projectPath: remotionRoot,
+ logLevel,
+ prompt: input.prompt,
+ }),
+ };
+};
diff --git a/packages/studio-server/src/preview-server/routes/default-coding-agent.ts b/packages/studio-server/src/preview-server/routes/default-coding-agent.ts
index 5becc111da..beea5cd986 100644
--- a/packages/studio-server/src/preview-server/routes/default-coding-agent.ts
+++ b/packages/studio-server/src/preview-server/routes/default-coding-agent.ts
@@ -5,11 +5,8 @@ import type {
OpenInCodingAgentResponse,
} from '@remotion/studio-shared';
import {getAppDiscovery} from '../../helpers/app-discovery';
-import {
- getAvailableCodingAgents,
- launchCodingAgent,
-} from '../../helpers/coding-agent-registry';
import {getAvailableGitClients} from '../../helpers/git-client-registry';
+import {openInCodingAgent} from '../../helpers/open-in-app';
import {getRecentlyUsedApps} from '../../helpers/recently-used-apps';
import {getAvailableTerminals} from '../../helpers/terminal-registry';
import type {ApiHandler} from '../api-types';
@@ -44,20 +41,4 @@ export const getDefaultCodingAgentInfoHandler: ApiHandler<
export const openInCodingAgentHandler: ApiHandler<
OpenInCodingAgentRequest,
OpenInCodingAgentResponse
-> = async ({input, logLevel, remotionRoot}) => {
- const codingAgent = (await getAvailableCodingAgents()).find(
- (agent) => agent.id === input.codingAgentId,
- );
- if (!codingAgent) {
- return {success: false};
- }
-
- return {
- success: await launchCodingAgent({
- codingAgent,
- projectPath: remotionRoot,
- logLevel,
- prompt: input.prompt,
- }),
- };
-};
+> = openInCodingAgent;
diff --git a/packages/studio-server/src/preview-server/routes/open-in-editor.ts b/packages/studio-server/src/preview-server/routes/open-in-editor.ts
index 2109d07242..c5b10ff0ac 100644
--- a/packages/studio-server/src/preview-server/routes/open-in-editor.ts
+++ b/packages/studio-server/src/preview-server/routes/open-in-editor.ts
@@ -1,16 +1,9 @@
-import path from 'node:path';
-import {
- defaultEditorIds,
- type BuiltInEditor,
- type DefaultEditor,
- type LogLevel,
-} from '@remotion/renderer';
+import type {DefaultEditor, LogLevel} from '@remotion/renderer';
import type {
OpenInEditorRequest,
OpenInEditorResponse,
} from '@remotion/studio-shared';
-import {launchCustomEditor} from '../../helpers/custom-editor';
-import {launchEditor} from '../../helpers/open-in-editor';
+import {openInEditor} from '../../helpers/open-in-app';
import {resolveEditor} from '../../helpers/resolve-editor';
import type {ApiHandler} from '../api-types';
@@ -32,63 +25,4 @@ export const getEditorName = async ({
export const openInEditorHandler: ApiHandler<
OpenInEditorRequest,
OpenInEditorResponse
-> = async ({input, remotionRoot, logLevel, getDefaultEditor}) => {
- try {
- if (!('stack' in input)) {
- throw new TypeError('Need to pass stack');
- }
-
- const {stack} = input;
- if (
- input.editorId !== null &&
- input.editorId !== 'custom' &&
- !defaultEditorIds.includes(input.editorId as BuiltInEditor)
- ) {
- return {success: false};
- }
-
- const editor = await resolveEditor({
- defaultEditor: getDefaultEditor(),
- logLevel,
- preferredEditor: input.editorId,
- });
- if (!editor) {
- return {success: false};
- }
-
- const fileName = path.resolve(
- remotionRoot,
- stack.originalFileName as string,
- );
- const didOpen =
- editor.type === 'custom'
- ? await launchCustomEditor({
- editor: editor.editor,
- resolvedExecutable: editor,
- targetPath: fileName,
- lineNumber: stack.originalLineNumber as number,
- columnNumber: stack.originalColumnNumber as number,
- logLevel,
- spawnProcess: null,
- })
- : await launchEditor(
- {
- colNumber: stack.originalColumnNumber as number,
- editor,
- fileName,
- lineNumber: stack.originalLineNumber as number,
- vsCodeNewWindow: false,
- logLevel,
- },
- remotionRoot,
- );
-
- return {
- success: didOpen,
- };
- } catch {
- return {
- success: false,
- };
- }
-};
+> = openInEditor;
diff --git a/packages/studio-server/src/studio-shortcuts.ts b/packages/studio-server/src/studio-shortcuts.ts
index 785478d5da..da55e0093f 100644
--- a/packages/studio-server/src/studio-shortcuts.ts
+++ b/packages/studio-server/src/studio-shortcuts.ts
@@ -8,17 +8,13 @@ import {RenderInternals} from '@remotion/renderer';
import {
getPreferredApp,
preferredFallbackEditorIds,
+ type GetDefaultEditorInfoResponse,
} from '@remotion/studio-shared';
import {focusBrowserTabByOrigin} from './better-opn';
import {getAppDiscovery, getEditorInfo} from './helpers/app-discovery';
-import {
- launchCodingAgent,
- type InstalledCodingAgent,
-} from './helpers/coding-agent-registry';
-import {launchCustomEditor} from './helpers/custom-editor';
-import {launchEditor} from './helpers/open-in-editor';
+import type {InstalledCodingAgent} from './helpers/coding-agent-registry';
+import {openInCodingAgent, openInEditor} from './helpers/open-in-app';
import {getRecentlyUsedApps} from './helpers/recently-used-apps';
-import {resolveEditor, type ResolvedEditor} from './helpers/resolve-editor';
import {maybeOpenBrowser} from './maybe-open-browser';
import {clearPrintPortMessageTimeout} from './preview-server/live-events';
@@ -57,7 +53,8 @@ export const registerStudioShortcuts = ({
const shouldPauseAfterCleanup = process.stdin.isPaused();
let cleanedUp = false;
let isOpeningBrowser = false;
- let editor: ResolvedEditor | null = null;
+ let editor: GetDefaultEditorInfoResponse['installedEditors'][number] | null =
+ null;
let codingAgent: InstalledCodingAgent | null = null;
const openingApps = new Set<string>();
@@ -77,20 +74,13 @@ export const registerStudioShortcuts = ({
recentlyUsedIds: null,
}),
]);
- const preferredEditor = getPreferredApp({
+ editor = getPreferredApp({
installedApps: editorInfo.installedEditors,
configuredId: editorInfo.defaultEditor,
runningIds: editorInfo.runningEditors ?? [],
recentlyUsedIds: recentEditors,
fallbackIds: preferredFallbackEditorIds,
});
- editor = preferredEditor
- ? await resolveEditor({
- defaultEditor: getDefaultEditor(),
- preferredEditor: preferredEditor.id,
- logLevel,
- })
- : null;
codingAgent = getPreferredApp({
installedApps: discovery.installedCodingAgents,
configuredId: getDefaultCodingAgent(),
@@ -127,37 +117,35 @@ export const registerStudioShortcuts = ({
openingApps.add(key);
try {
- if (key === 'a' && codingAgent) {
- await launchCodingAgent({
- codingAgent,
- projectPath: remotionRoot,
- logLevel,
- prompt: null,
- });
- } else if (key === 'e' && editor) {
- if (editor.type === 'custom') {
- await launchCustomEditor({
- editor: editor.editor,
- resolvedExecutable: editor,
- targetPath: remotionRoot,
- lineNumber: 1,
- columnNumber: 1,
- logLevel,
- spawnProcess: null,
- });
- } else {
- await launchEditor(
- {
- editor,
- fileName: remotionRoot,
- lineNumber: 1,
- colNumber: 1,
- vsCodeNewWindow: false,
+ const response =
+ key === 'a' && codingAgent
+ ? await openInCodingAgent({
+ input: {codingAgentId: codingAgent.id, prompt: null},
+ remotionRoot,
logLevel,
- },
- remotionRoot,
- );
- }
+ })
+ : key === 'e' && editor
+ ? await openInEditor({
+ input: {
+ editorId: editor.id,
+ stack: {
+ originalFileName: remotionRoot,
+ originalLineNumber: 1,
+ originalColumnNumber: 1,
+ originalFunctionName: null,
+ originalScriptCode: null,
+ },
+ },
+ remotionRoot,
+ logLevel,
+ getDefaultEditor,
+ })
+ : null;
+ if (response && !response.success) {
+ RenderInternals.Log.error(
+ {indent: false, logLevel},
+ `Could not open ${key === 'a' ? codingAgent?.name : editor?.name}`,
+ );
}
} catch (err) {
RenderInternals.Log.error(