Skip to content

🎥 Make videos programmatically with React

detect remotion root dir

Jonny Burger03bb537a957c
diff --git a/packages/cli/src/find-closest-package-json.ts b/packages/cli/src/find-closest-package-json.ts
new file mode 100644
index 0000000000..2fb4b87780
--- /dev/null
+++ b/packages/cli/src/find-closest-package-json.ts
@@ -0,0 +1,35 @@
+import fs from 'fs';
+import path from 'path';
+import {Log} from './log';
+
+const recursionLimit = 5;
+
+export const findClosestPackageJson = (): string | null => {
+ let currentDir = process.cwd();
+ let possiblePackageJson = '';
+ for (let i = 0; i < recursionLimit; i++) {
+ possiblePackageJson = path.join(currentDir, 'package.json');
+ const exists = fs.existsSync(possiblePackageJson);
+ if (exists) {
+ return possiblePackageJson;
+ }
+
+ currentDir = path.dirname(path.dirname(currentDir));
+ }
+
+ return null;
+};
+
+export const findRemotionRoot = (): string => {
+ const closestPackageJson = findClosestPackageJson();
+ if (closestPackageJson === null) {
+ Log.error(
+ 'Could not find a package.json in the current directory or any of the ' +
+ recursionLimit +
+ ' parent directories. Is this a Remotion project?'
+ );
+ process.exit(1);
+ }
+
+ return path.dirname(closestPackageJson);
+};
diff --git a/packages/cli/src/get-config-file-name.ts b/packages/cli/src/get-config-file-name.ts
index ab4298dc3e..d3a070a8dd 100644
--- a/packages/cli/src/get-config-file-name.ts
+++ b/packages/cli/src/get-config-file-name.ts
@@ -1,5 +1,6 @@
import {existsSync} from 'fs';
import path from 'path';
+import {findRemotionRoot} from './find-closest-package-json';
import {loadConfigFile} from './load-config';
import {Log} from './log';
import {parsedCli} from './parse-command-line';
@@ -19,11 +20,16 @@ export const loadConfig = (): Promise<string | null> => {
return loadConfigFile(parsedCli.config, fullPath.endsWith('.js'));
}
- if (existsSync(path.resolve(process.cwd(), defaultConfigFileTypescript))) {
+ const remotionRoot = findRemotionRoot();
+ if (remotionRoot === null) {
+ return Promise.resolve(null);
+ }
+
+ if (existsSync(path.resolve(remotionRoot, defaultConfigFileTypescript))) {
return loadConfigFile(defaultConfigFileTypescript, false);
}
- if (existsSync(path.resolve(process.cwd(), defaultConfigFileJavascript))) {
+ if (existsSync(path.resolve(remotionRoot, defaultConfigFileJavascript))) {
return loadConfigFile(defaultConfigFileJavascript, true);
}
diff --git a/packages/cli/src/get-env.ts b/packages/cli/src/get-env.ts
index 8a5fc2759b..f67f4277f6 100644
--- a/packages/cli/src/get-env.ts
+++ b/packages/cli/src/get-env.ts
@@ -2,6 +2,7 @@ import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';
import {Internals} from 'remotion';
+import {findRemotionRoot} from './find-closest-package-json';
import {Log} from './log';
import {parsedCli} from './parse-command-line';
@@ -52,8 +53,11 @@ export const getEnvironmentVariables = (): Promise<Record<string, string>> => {
}
const configFileSetting = Internals.getDotEnvLocation();
+
+ const remotionRoot = findRemotionRoot();
+
if (configFileSetting) {
- const envFile = path.resolve(process.cwd(), configFileSetting);
+ const envFile = path.resolve(remotionRoot, configFileSetting);
if (!fs.existsSync(envFile)) {
Log.error(
'You specifed a custom .env file using `Config.Rendering.setDotEnvLocation()` in the config file but it could not be found'
@@ -66,7 +70,7 @@ export const getEnvironmentVariables = (): Promise<Record<string, string>> => {
return getEnvForEnvFile(processEnv, envFile);
}
- const defaultEnvFile = path.resolve(process.cwd(), '.env');
+ const defaultEnvFile = path.resolve(remotionRoot, '.env');
if (!fs.existsSync(defaultEnvFile)) {
return Promise.resolve(processEnv);
}
diff --git a/packages/cli/src/load-config.ts b/packages/cli/src/load-config.ts
index 109837ee1d..f9f4317b04 100644
--- a/packages/cli/src/load-config.ts
+++ b/packages/cli/src/load-config.ts
@@ -5,6 +5,7 @@ import path from 'path';
import {Log} from './log';
export const loadConfigFile = async (
+ remotionRoot: string,
configFileName: string,
isJavascript: boolean
): Promise<string | null> => {
@@ -55,6 +56,7 @@ export const loadConfigFile = async (
// eslint-disable-next-line no-eval
eval(file);
+ Log.verbose('Loaded configuration from', resolved);
await fs.promises.unlink(out);
return resolved;
};
diff --git a/packages/cli/src/test/find-closest-package-json.test.ts b/packages/cli/src/test/find-closest-package-json.test.ts
new file mode 100644
index 0000000000..4e39cce613
--- /dev/null
+++ b/packages/cli/src/test/find-closest-package-json.test.ts
@@ -0,0 +1,11 @@
+import path from 'path';
+import {expect, test} from 'vitest';
+import {findClosestPackageJson} from '../find-closest-package-json';
+
+test('Find closests package.json', () => {
+ expect(
+ findClosestPackageJson()?.endsWith(
+ path.join('packages', 'cli', 'package.json')
+ )
+ ).toBe(true);
+});