Skip to content

🎥 Make videos programmatically with React

better opn

Jonny Burger0dfdab5505b9
diff --git a/packages/cli/package.json b/packages/cli/package.json
index 1797a5df29..c8fa17762c 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -26,7 +26,7 @@
"@remotion/media-utils": "workspace:*",
"@remotion/player": "workspace:*",
"@remotion/renderer": "workspace:*",
- "better-opn": "2.1.1",
+ "open": "^8.4.2",
"dotenv": "9.0.2",
"memfs": "3.4.3",
"minimist": "1.2.6",
diff --git a/packages/cli/src/better-opn/index.ts b/packages/cli/src/better-opn/index.ts
new file mode 100644
index 0000000000..8e52f89c5b
--- /dev/null
+++ b/packages/cli/src/better-opn/index.ts
@@ -0,0 +1,229 @@
+// Copied from https://github.com/michaellzc/better-opn#readme
+
+import {execSync} from 'child_process';
+import open = require('open');
+const OSX_CHROME = 'google chrome';
+
+const Actions = Object.freeze({
+ NONE: 0,
+ BROWSER: 1,
+});
+
+type Options = {
+ wait: boolean;
+ app: {
+ name: string;
+ arguments: string[];
+ };
+};
+
+const getBrowserEnv = () => {
+ // Attempt to honor this environment variable.
+ // It is specific to the operating system.
+ // See https://github.com/sindresorhus/open#app for documentation.
+ const browser = process.env.BROWSER;
+ const args: string[] = process.env.BROWSER_ARGS
+ ? process.env.BROWSER_ARGS.split(' ')
+ : [];
+ let action;
+ if (!browser) {
+ // Default.
+ action = Actions.BROWSER;
+ } else if (browser.toLowerCase() === 'none') {
+ action = Actions.NONE;
+ } else {
+ action = Actions.BROWSER;
+ }
+
+ return {action, browser, args};
+};
+
+const normalizeURLToMatch = (target: string) => {
+ // We may encounter URL parse error but want to fallback to default behavior
+ try {
+ // Url module is deprecated on newer version of NodeJS, only use it when URL class is not supported (like Node 8)
+ const URL =
+ typeof global.URL === 'undefined' ? require('url').URL : global.URL;
+ const url = new URL(target);
+ return url.origin;
+ } catch {
+ return target;
+ }
+};
+
+// Copy from
+// https://github.com/facebook/create-react-app/blob/master/packages/react-dev-utils/openBrowser.js#L64
+const startBrowserProcess = (
+ browser: string | undefined,
+ url: string,
+ args: string[]
+) => {
+ // If we're on OS X, the user hasn't specifically
+ // requested a different browser, we can try opening
+ // Chrome with AppleScript. This lets us reuse an
+ // existing tab when possible instead of creating a new one.
+ const shouldTryOpenChromiumWithAppleScript =
+ process.platform === 'darwin' &&
+ (typeof browser !== 'string' || browser === OSX_CHROME);
+
+ if (shouldTryOpenChromiumWithAppleScript) {
+ let appleScriptDenied = false;
+
+ // Will use the first open browser found from list
+ const supportedChromiumBrowsers = [
+ 'Google Chrome',
+ 'Google Chrome Canary',
+ 'Microsoft Edge',
+ 'Brave Browser',
+ 'Vivaldi',
+ 'Chromium',
+ 'Arc',
+ ];
+ for (const chromiumBrowser of supportedChromiumBrowsers) {
+ if (appleScriptDenied) {
+ continue;
+ }
+
+ try {
+ // Try our best to reuse existing tab
+ // on OSX Chromium-based browser with AppleScript
+ const appleScript = `
+(*
+ Copyright (c) 2015-present, Facebook, Inc.
+ This source code is licensed under the MIT license found in the
+ LICENSE file in the root directory of this source tree.
+ *)
+
+ property targetTab: null
+ property targetTabIndex: -1
+ property targetWindow: null
+ property theProgram: "${chromiumBrowser}"
+
+ on run argv
+ set theURL to "${encodeURI(url)}"
+ set matchURL to "${
+ process.env.OPEN_MATCH_HOST_ONLY === 'true'
+ ? encodeURI(normalizeURLToMatch(url))
+ : encodeURI(url)
+ }"
+
+ using terms from application "Google Chrome"
+ tell application theProgram
+
+ if (count every window) = 0 then
+ make new window
+ end if
+
+ -- 1: Looking for tab running debugger
+ -- then, Reload debugging tab if found
+ -- then return
+ set found to my lookupTabWithUrl(matchURL)
+ if found then
+ set targetWindow's active tab index to targetTabIndex
+ tell targetTab to reload
+ tell targetWindow to activate
+ set index of targetWindow to 1
+ return
+ end if
+
+ -- 2: Looking for Empty tab
+ -- In case debugging tab was not found
+ -- We try to find an empty tab instead
+ set found to my lookupTabWithUrl("chrome://newtab/")
+ if found then
+ set targetWindow's active tab index to targetTabIndex
+ set URL of targetTab to theURL
+ tell targetWindow to activate
+ return
+ end if
+
+ -- 3: Create new tab
+ -- both debugging and empty tab were not found
+ -- make a new tab with url
+ tell window 1
+ activate
+ make new tab with properties {URL:theURL}
+ end tell
+ end tell
+ end using terms from
+ end run
+
+ -- Function:
+ -- Lookup tab with given url
+ -- if found, store tab, index, and window in properties
+ -- (properties were declared on top of file)
+ on lookupTabWithUrl(lookupUrl)
+ using terms from application "Google Chrome"
+ tell application theProgram
+ -- Find a tab with the given url
+ set found to false
+ set theTabIndex to -1
+ repeat with theWindow in every window
+ set theTabIndex to 0
+ repeat with theTab in every tab of theWindow
+ set theTabIndex to theTabIndex + 1
+ if (theTab's URL as string) contains lookupUrl then
+ -- assign tab, tab index, and window to properties
+ set targetTab to theTab
+ set targetTabIndex to theTabIndex
+ set targetWindow to theWindow
+ set found to true
+ exit repeat
+ end if
+ end repeat
+
+ if found then
+ exit repeat
+ end if
+ end repeat
+ end tell
+ end using terms from
+ return found
+ end lookupTabWithUrl
+`.trim();
+ execSync(`ps cax | grep "${chromiumBrowser}"`);
+ execSync(`osascript -`, {
+ stdio: ['pipe', 'ignore', 'pipe'],
+ input: appleScript,
+ });
+
+ return Promise.resolve(true);
+ } catch (error) {
+ const appleScriptError =
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ ((error as any)?.output?.[1]?.toString() as string) ?? '';
+ if (
+ appleScriptError
+ .toLowerCase()
+ .includes('not authorised to send apple events')
+ ) {
+ appleScriptDenied = true;
+ }
+
+ // Ignore errors.
+ // It it breaks, it will fallback to `opn` anyway
+ }
+ }
+ }
+
+ // Fallback to opn
+ // (It will always open new tab)
+ const options: Options = {
+ app: {name: browser as string, arguments: args},
+ wait: false,
+ };
+ return open(url, options);
+};
+
+export const betterOpn = (url: string) => {
+ const {action, browser, args} = getBrowserEnv();
+ switch (action) {
+ case Actions.NONE:
+ // Special case: BROWSER="none" will prevent opening completely.
+ return false;
+ case Actions.BROWSER:
+ return startBrowserProcess(browser, url, args);
+ default:
+ throw new Error('Not implemented.');
+ }
+};
diff --git a/packages/cli/src/preview.ts b/packages/cli/src/preview.ts
index 846ae33823..519c226ab7 100644
--- a/packages/cli/src/preview.ts
+++ b/packages/cli/src/preview.ts
@@ -1,6 +1,6 @@
-import betterOpn from 'better-opn';
import crypto from 'crypto';
import path from 'path';
+import {betterOpn} from './better-opn';
import {chalk} from './chalk';
import {ConfigInternals} from './config';
import {findEntryPoint} from './entry-point';
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index fbbbcc3856..fab776cde0 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -148,7 +148,6 @@ importers:
'@types/semver': ^7.3.4
'@typescript-eslint/eslint-plugin': 5.18.0
'@typescript-eslint/parser': 5.18.0
- better-opn: 2.1.1
dotenv: 9.0.2
eslint: 8.25.0
eslint-plugin-10x: 1.5.2
@@ -156,6 +155,7 @@ importers:
eslint-plugin-react-hooks: 4.4.0
memfs: 3.4.3
minimist: 1.2.6
+ open: ^8.4.2
prettier: ^2.7.1
prettier-plugin-organize-imports: ^2.3.4
prompts: 2.4.1
@@ -171,10 +171,10 @@ importers:
'@remotion/media-utils': link:../media-utils
'@remotion/player': link:../player
'@remotion/renderer': link:../renderer
- better-opn: 2.1.1
dotenv: 9.0.2
memfs: 3.4.3
minimist: 1.2.6
+ open: 8.4.2
prompts: 2.4.1
remotion: link:../core
semver: 7.3.5
@@ -11278,13 +11278,6 @@ packages:
resolution: {integrity: sha512-3pZEU3NT5BFUo/AD5ERPWOgQOCZITni6iavr5AUw5AUwQjMlI0kzu5btnyD39AF0gUEsDPwJT+oY1ORBJijPjQ==}
dev: false
- /better-opn/2.1.1:
- resolution: {integrity: sha512-kIPXZS5qwyKiX/HcRvDYfmBQUa8XP17I0mYZZ0y4UhpYOSvtsLHDYqmomS+Mj20aDvD3knEiQ0ecQy2nhio3yA==}
- engines: {node: '>8.0.0'}
- dependencies:
- open: 7.4.2
- dev: false
-
/bidi-js/1.0.2:
resolution: {integrity: sha512-rzSy/k7WdX5zOyeHHCOixGXbCHkyogkxPKL2r8QtzHmVQDiWCXUWa18bLdMWT9CYMLOYTjWpTHawuev2ouYJVw==}
dependencies:
@@ -19330,6 +19323,15 @@ packages:
is-wsl: 2.2.0
dev: false
+ /open/8.4.2:
+ resolution: {integrity: sha512-7x81NCL719oNbsq/3mh+hVrAWmFuEYUqrq/Iw3kUzH8ReypT9QQ0BLoJS7/G9k6N81XjW4qHWtjWwe/9eLy1EQ==}
+ engines: {node: '>=12'}
+ dependencies:
+ define-lazy-prop: 2.0.0
+ is-docker: 2.2.1
+ is-wsl: 2.2.0
+ dev: false
+
/opener/1.5.2:
resolution: {integrity: sha512-ur5UIdyw5Y7yEj9wLzhqXiy6GZ3Mwx0yGI+5sMn2r0N0v3cKJvUmFH5yPP+WXh9e0xfyzyJX95D8l088DNFj7A==}
hasBin: true