Skip to content

🎥 Make videos programmatically with React

open in VS Code and other editors if. `code .`

Jonny Burgerf99137832765
diff --git a/packages/cli/src/preview-server/error-overlay/react-overlay/utils/open-in-editor.ts b/packages/cli/src/preview-server/error-overlay/react-overlay/utils/open-in-editor.ts
index f551bfdbf0..cae1243638 100644
--- a/packages/cli/src/preview-server/error-overlay/react-overlay/utils/open-in-editor.ts
+++ b/packages/cli/src/preview-server/error-overlay/react-overlay/utils/open-in-editor.ts
@@ -11,7 +11,7 @@
* LICENSE file in the root directory of this source tree.
*/
import type {ChildProcess} from 'child_process';
-import child_process, { exec} from 'child_process';
+import child_process, {exec} from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';
@@ -338,11 +338,16 @@ function getArgumentsForLineNumber(
}
}
-export async function guessEditor(): Promise<Editor[]> {
+type ProcessAndCommand = {
+ process: string;
+ command: Editor;
+};
+
+export async function guessEditor(): Promise<ProcessAndCommand[]> {
// We can find out which editor is currently running by:
// `ps x` on macOS and Linux
// `Get-Process` on Windows
- const availableEditors: Editor[] = [];
+ const availableEditors: ProcessAndCommand[] = [];
try {
if (process.platform === 'darwin') {
const output = (await execProm('ps x')).stdout.toString();
@@ -350,7 +355,10 @@ export async function guessEditor(): Promise<Editor[]> {
for (let i = 0; i < processNames.length; i++) {
const processName = processNames[i];
if (output.indexOf(processName) !== -1) {
- availableEditors.push(COMMON_EDITORS_OSX[processName]);
+ availableEditors.push({
+ process: processName,
+ command: COMMON_EDITORS_OSX[processName],
+ });
}
}
@@ -370,7 +378,10 @@ export async function guessEditor(): Promise<Editor[]> {
const processPath = runningProcesses[i].trim();
const processName = path.basename(processPath);
if (COMMON_EDITORS_WIN.indexOf(processName as Editor) !== -1) {
- availableEditors.push(processPath as Editor);
+ availableEditors.push({
+ process: processPath,
+ command: processPath as Editor,
+ });
}
}
@@ -388,7 +399,10 @@ export async function guessEditor(): Promise<Editor[]> {
for (let i = 0; i < processNames.length; i++) {
const processName = processNames[i];
if (output.indexOf(processName) !== -1) {
- availableEditors.push(COMMON_EDITORS_LINUX[processName]);
+ availableEditors.push({
+ process: processName,
+ command: COMMON_EDITORS_LINUX[processName],
+ });
}
}
@@ -400,11 +414,21 @@ export async function guessEditor(): Promise<Editor[]> {
// Last resort, use old skool env vars
if (process.env.VISUAL) {
- return [process.env.VISUAL as Editor];
+ return [
+ {
+ process: process.env.VISUAL as Editor,
+ command: process.env.VISUAL as Editor,
+ },
+ ];
}
if (process.env.EDITOR) {
- return [process.env.EDITOR as Editor];
+ return [
+ {
+ process: process.env.EDITOR as Editor,
+ command: process.env.EDITOR as Editor,
+ },
+ ];
}
return [];
@@ -412,7 +436,7 @@ export async function guessEditor(): Promise<Editor[]> {
let _childProcess: ChildProcess | null = null;
-export function launchEditor({
+export async function launchEditor({
colNumber,
editor,
fileName,
@@ -422,18 +446,18 @@ export function launchEditor({
fileName: string;
lineNumber: number;
colNumber: number;
- editor: Editor;
+ editor: ProcessAndCommand;
vsCodeNewWindow: boolean;
}): Promise<boolean> {
if (!fs.existsSync(fileName)) {
- return Promise.resolve(false);
+ return false;
}
// Sanitize lineNumber to prevent malicious use on win32
// via: https://github.com/nodejs/node/blob/c3bb4b1aa5e907d489619fb43d233c3336bfc03d/lib/child_process.js#L333
// and it should be a positive integer
if (!(Number.isInteger(lineNumber) && lineNumber > 0)) {
- return Promise.resolve(false);
+ return false;
}
// colNumber is optional, but should be a positive integer too
@@ -442,8 +466,8 @@ export function launchEditor({
colNumber = 1;
}
- if (editor.toLowerCase() === 'none') {
- return Promise.resolve(false);
+ if (editor.command.toLowerCase() === 'none') {
+ return false;
}
if (
@@ -478,38 +502,58 @@ export function launchEditor({
'dashes, slashes, and underscores.'
);
Log.error();
- return Promise.resolve(false);
+ return false;
}
const shouldOpenVsCodeNewWindow =
- isVsCodeDerivative(editor) && vsCodeNewWindow;
+ isVsCodeDerivative(editor.command) && vsCodeNewWindow;
const args = shouldOpenVsCodeNewWindow
? ['--new-window', fileName]
: lineNumber
- ? getArgumentsForLineNumber(editor, fileName, String(lineNumber), colNumber)
+ ? getArgumentsForLineNumber(
+ editor.command,
+ fileName,
+ String(lineNumber),
+ colNumber
+ )
: [fileName];
- if (_childProcess && isTerminalEditor(editor)) {
+ if (_childProcess && isTerminalEditor(editor.command)) {
// There's an existing editor process already and it's attached
// to the terminal, so go kill it. Otherwise two separate editor
// instances attach to the stdin/stdout which gets confusing.
_childProcess.kill('SIGKILL');
}
+ const isWin = os.platform() === 'win32';
+ const where = isWin ? 'where' : 'which';
+
+ const binaryToUse = await new Promise<string>((resolve) => {
+ if (editor.command === editor.process) {
+ resolve(editor.command);
+ return;
+ }
+
+ child_process.exec(`${where} "${editor.command}"`, (err) => {
+ if (err) {
+ resolve(editor.process);
+ } else {
+ resolve(editor.command);
+ }
+ });
+ });
+
if (process.platform === 'win32') {
// On Windows, launch the editor in a shell because spawn can only
// launch .exe files.
_childProcess = child_process.spawn(
'cmd.exe',
- ['/C', editor].concat(args),
- {stdio: 'inherit'}
+ ['/C', binaryToUse].concat(args),
+ {stdio: 'inherit', detached: true}
);
} else {
- _childProcess = child_process.spawn(editor, args, {
- stdio: 'inherit',
- detached: true,
- });
+ _childProcess = child_process.spawn(binaryToUse, args, {stdio: 'inherit'});
}
_childProcess.on('exit', (errorCode) => {
@@ -523,5 +567,5 @@ export function launchEditor({
_childProcess.on('error', (error) => {
Log.error('Error opening file in editor', fileName, error.message);
});
- return Promise.resolve(true);
+ return true;
}
diff --git a/packages/cli/src/preview-server/routes.ts b/packages/cli/src/preview-server/routes.ts
index 253b9d6548..b0df1a3aa1 100644
--- a/packages/cli/src/preview-server/routes.ts
+++ b/packages/cli/src/preview-server/routes.ts
@@ -38,7 +38,7 @@ const handleFallback = async (
getCurrentInputProps: () => object
) => {
const edit = await editorGuess;
- const displayName = getDisplayNameForEditor(edit[0]);
+ const displayName = getDisplayNameForEditor(edit[0].command);
response.setHeader('content-type', 'text/html');
response.writeHead(200);
diff --git a/packages/create-video/src/open-in-editor-flow.ts b/packages/create-video/src/open-in-editor-flow.ts
index 50b99795c3..4bcdf350ef 100644
--- a/packages/create-video/src/open-in-editor-flow.ts
+++ b/packages/create-video/src/open-in-editor-flow.ts
@@ -11,13 +11,13 @@ import {yesOrNo} from './yesno';
export const openInEditorFlow = async (projectRoot: string) => {
const editors = await guessEditor();
- const [guiEditor] = editors.filter((e) => !isTerminalEditor(e));
+ const [guiEditor] = editors.filter((e) => !isTerminalEditor(e.command));
if (!guiEditor) {
return;
}
- const displayName = getDisplayNameForEditor(guiEditor);
+ const displayName = getDisplayNameForEditor(guiEditor.command);
const should = await yesOrNo({
defaultValue: true,
@@ -25,18 +25,18 @@ export const openInEditorFlow = async (projectRoot: string) => {
});
if (should) {
- launchEditor({
+ await launchEditor({
colNumber: 1,
editor: guiEditor,
fileName: projectRoot,
vsCodeNewWindow: true,
lineNumber: 1,
});
- if (isVsCodeDerivative(guiEditor)) {
+ if (isVsCodeDerivative(guiEditor.command)) {
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
- launchEditor({
+ await launchEditor({
colNumber: 1,
editor: guiEditor,
fileName: path.join(projectRoot, 'README.md'),
diff --git a/packages/create-video/src/open-in-editor.ts b/packages/create-video/src/open-in-editor.ts
index 542fc09a9e..90e08a4e4e 100644
--- a/packages/create-video/src/open-in-editor.ts
+++ b/packages/create-video/src/open-in-editor.ts
@@ -337,11 +337,16 @@ function getArgumentsForLineNumber(
}
}
-export async function guessEditor(): Promise<Editor[]> {
+type ProcessAndCommand = {
+ process: string;
+ command: Editor;
+};
+
+export async function guessEditor(): Promise<ProcessAndCommand[]> {
// We can find out which editor is currently running by:
// `ps x` on macOS and Linux
// `Get-Process` on Windows
- const availableEditors: Editor[] = [];
+ const availableEditors: ProcessAndCommand[] = [];
try {
if (process.platform === 'darwin') {
const output = (await execProm('ps x')).stdout.toString();
@@ -349,7 +354,10 @@ export async function guessEditor(): Promise<Editor[]> {
for (let i = 0; i < processNames.length; i++) {
const processName = processNames[i];
if (output.indexOf(processName) !== -1) {
- availableEditors.push(COMMON_EDITORS_OSX[processName]);
+ availableEditors.push({
+ process: processName,
+ command: COMMON_EDITORS_OSX[processName],
+ });
}
}
@@ -369,7 +377,10 @@ export async function guessEditor(): Promise<Editor[]> {
const processPath = runningProcesses[i].trim();
const processName = path.basename(processPath);
if (COMMON_EDITORS_WIN.indexOf(processName as Editor) !== -1) {
- availableEditors.push(processPath as Editor);
+ availableEditors.push({
+ process: processPath,
+ command: processPath as Editor,
+ });
}
}
@@ -387,7 +398,10 @@ export async function guessEditor(): Promise<Editor[]> {
for (let i = 0; i < processNames.length; i++) {
const processName = processNames[i];
if (output.indexOf(processName) !== -1) {
- availableEditors.push(COMMON_EDITORS_LINUX[processName]);
+ availableEditors.push({
+ process: processName,
+ command: COMMON_EDITORS_LINUX[processName],
+ });
}
}
@@ -399,11 +413,21 @@ export async function guessEditor(): Promise<Editor[]> {
// Last resort, use old skool env vars
if (process.env.VISUAL) {
- return [process.env.VISUAL as Editor];
+ return [
+ {
+ process: process.env.VISUAL as Editor,
+ command: process.env.VISUAL as Editor,
+ },
+ ];
}
if (process.env.EDITOR) {
- return [process.env.EDITOR as Editor];
+ return [
+ {
+ process: process.env.EDITOR as Editor,
+ command: process.env.EDITOR as Editor,
+ },
+ ];
}
return [];
@@ -411,7 +435,7 @@ export async function guessEditor(): Promise<Editor[]> {
let _childProcess: ChildProcess | null = null;
-export function launchEditor({
+export async function launchEditor({
colNumber,
editor,
fileName,
@@ -421,9 +445,9 @@ export function launchEditor({
fileName: string;
lineNumber: number;
colNumber: number;
- editor: Editor;
+ editor: ProcessAndCommand;
vsCodeNewWindow: boolean;
-}): boolean {
+}): Promise<boolean> {
if (!fs.existsSync(fileName)) {
return false;
}
@@ -441,7 +465,7 @@ export function launchEditor({
colNumber = 1;
}
- if (editor.toLowerCase() === 'none') {
+ if (editor.command.toLowerCase() === 'none') {
return false;
}
@@ -483,31 +507,54 @@ export function launchEditor({
}
const shouldOpenVsCodeNewWindow =
- isVsCodeDerivative(editor) && vsCodeNewWindow;
+ isVsCodeDerivative(editor.command) && vsCodeNewWindow;
const args = shouldOpenVsCodeNewWindow
? ['--new-window', fileName]
: lineNumber
- ? getArgumentsForLineNumber(editor, fileName, String(lineNumber), colNumber)
+ ? getArgumentsForLineNumber(
+ editor.command,
+ fileName,
+ String(lineNumber),
+ colNumber
+ )
: [fileName];
- if (_childProcess && isTerminalEditor(editor)) {
+ if (_childProcess && isTerminalEditor(editor.command)) {
// There's an existing editor process already and it's attached
// to the terminal, so go kill it. Otherwise two separate editor
// instances attach to the stdin/stdout which gets confusing.
_childProcess.kill('SIGKILL');
}
+ const isWin = os.platform() === 'win32';
+ const where = isWin ? 'where' : 'which';
+
+ const binaryToUse = await new Promise<string>((resolve) => {
+ if (editor.command === editor.process) {
+ resolve(editor.command);
+ return;
+ }
+
+ child_process.exec(`${where} "${editor.command}"`, (err) => {
+ if (err) {
+ resolve(editor.process);
+ } else {
+ resolve(editor.command);
+ }
+ });
+ });
+
if (process.platform === 'win32') {
// On Windows, launch the editor in a shell because spawn can only
// launch .exe files.
_childProcess = child_process.spawn(
'cmd.exe',
- ['/C', editor].concat(args),
+ ['/C', binaryToUse].concat(args),
{stdio: 'inherit', detached: true}
);
} else {
- _childProcess = child_process.spawn(editor, args, {stdio: 'inherit'});
+ _childProcess = child_process.spawn(binaryToUse, args, {stdio: 'inherit'});
}
_childProcess.on('exit', (errorCode) => {
diff --git a/packages/example/src/AudioTesting/index.tsx b/packages/example/src/AudioTesting/index.tsx
index 0c209de017..0519b44a99 100644
--- a/packages/example/src/AudioTesting/index.tsx
+++ b/packages/example/src/AudioTesting/index.tsx
@@ -1,5 +1,4 @@
-import {staticFile} from 'remotion';
-import {Audio, interpolate, Sequence} from 'remotion';
+import {Audio, interpolate, Sequence, staticFile} from 'remotion';
const AudioTesting: React.FC = () => {
return (