Skip to content

🎥 Make videos programmatically with React

validate public dir

Jonny Burger685eb363d78e
diff --git a/packages/bundler/package.json b/packages/bundler/package.json
index d6a9e25e1b..e0f6e65aba 100644
--- a/packages/bundler/package.json
+++ b/packages/bundler/package.json
@@ -13,7 +13,8 @@
"scripts": {
"lint": "eslint src --ext ts,tsx",
"build": "tsc -d",
- "watch": "tsc -w"
+ "watch": "tsc -w",
+ "test": "vitest --run"
},
"files": [
"dist",
@@ -50,7 +51,8 @@
"prettier-plugin-organize-imports": "^2.3.4",
"react": "^18.0.0",
"react-dom": "^18.0.0",
- "typescript": "^4.7.0"
+ "typescript": "^4.7.0",
+ "vitest": "^0.18.0"
},
"keywords": [
"remotion",
diff --git a/packages/bundler/src/bundle.ts b/packages/bundler/src/bundle.ts
index 105c70d40f..e84445839a 100644
--- a/packages/bundler/src/bundle.ts
+++ b/packages/bundler/src/bundle.ts
@@ -44,6 +44,7 @@ export type BundleOptions = {
enableCaching?: boolean;
publicPath?: string;
rootDir?: string;
+ publicDir?: string;
};
export const getConfig = ({
@@ -122,8 +123,9 @@ export const bundle = async (
.filter(Boolean)
.join('/');
- // TODO: Unhardcode public directory
- const from = path.join(resolvedRemotionRoot, 'public');
+ const from = options?.publicDir
+ ? path.resolve(resolvedRemotionRoot, options.publicDir)
+ : path.join(resolvedRemotionRoot, 'public');
const to = path.join(outDir, 'public');
if (fs.existsSync(from)) {
await copyDir(from, to);
diff --git a/packages/bundler/src/test/validate-public-dir.test.ts b/packages/bundler/src/test/validate-public-dir.test.ts
new file mode 100644
index 0000000000..1bbbbfa42b
--- /dev/null
+++ b/packages/bundler/src/test/validate-public-dir.test.ts
@@ -0,0 +1,26 @@
+import {describe, expect, test} from 'vitest';
+import {validatePublicDir} from '../validate-public-dir';
+
+describe('validatePublicDir()', () => {
+ test('Should not allow root directory as public dir.', () => {
+ expect(() => validatePublicDir('/')).toThrow(
+ /which is the root directory. This is not allowed./
+ );
+ });
+
+ test('Should not allow a path where the parent directory does not exist', () => {
+ const pathToPass =
+ process.platform === 'win32' ? 'C:\\foo\\bar' : '/foo/bar';
+ const expectedParent = process.platform === 'win32' ? 'C:\\foo' : '/foo';
+
+ expect(() => validatePublicDir(pathToPass)).toThrow(
+ `The public directory was specified as "${pathToPass}", but this folder does not exist and the parent directory "${expectedParent}" does also not exist.`
+ );
+ });
+
+ test('Should allow /foo as a path since that directory can be created', () => {
+ expect(() =>
+ validatePublicDir(process.platform === 'win32' ? 'C:\\foo' : '/foo')
+ ).not.toThrow();
+ });
+});
diff --git a/packages/bundler/src/validate-public-dir.ts b/packages/bundler/src/validate-public-dir.ts
new file mode 100644
index 0000000000..286c665463
--- /dev/null
+++ b/packages/bundler/src/validate-public-dir.ts
@@ -0,0 +1,31 @@
+import fs from 'fs';
+import path from 'path';
+
+export const validatePublicDir = (p: string) => {
+ const {root} = path.parse(process.cwd());
+
+ if (p === root) {
+ throw new Error(
+ `The public directory was specified as "${p}", which is the root directory. This is not allowed.`
+ );
+ }
+
+ try {
+ const stat = fs.lstatSync(p);
+ if (!stat.isDirectory()) {
+ throw new Error(
+ `The public directory was specified as "${p}", and while this path exists on the filesystem, it is not a directory.`
+ );
+ }
+ } catch (e) {
+ // Path does not exist
+ // Check if the parent path exists
+ const parentPath = path.dirname(p);
+ const exists = fs.existsSync(parentPath);
+ if (!exists) {
+ throw new Error(
+ `The public directory was specified as "${p}", but this folder does not exist and the parent directory "${parentPath}" does also not exist. Create at least the parent directory.`
+ );
+ }
+ }
+};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index e5de2c4c4a..7cd68e4a1a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -70,6 +70,7 @@ importers:
remotion: 3.2.12
style-loader: 2.0.0
typescript: ^4.7.0
+ vitest: ^0.18.0
webpack: 5.72.0
dependencies:
css-loader: [email protected]
@@ -95,6 +96,7 @@ importers:
react: 18.0.0
react-dom: [email protected]
typescript: 4.7.2
+ vitest: 0.18.0
packages/cli:
specifiers:
@@ -563,8 +565,6 @@ importers:
'@remotion/renderer': link:../renderer
aws-policies: 1.0.1
mime-types: 2.1.34
- react: 18.0.0
- react-dom: [email protected]
remotion: link:../core
devDependencies:
'@jonny/eslint-config': 3.0.266_6hvhxfirl4y3p6nuemvvvbadcu
@@ -577,6 +577,8 @@ importers:
prettier: 2.4.1
prettier-plugin-organize-imports: 2.3.4_pxx7xhvr2bexxfjjff3nukai7m
+ react: 18.0.0
+ react-dom: [email protected]
ts-jest: 27.0.5_g2mfyom6zaxqcgedxrl4lakegu
ts-node: 10.8.0_ysvagj6uoj2jcpx4fli7niyicq
typescript: 4.7.2
@@ -626,8 +628,6 @@ importers:
remotion: 3.2.12
typescript: ^4.7.0
dependencies:
- react: 18.0.0
- react-dom: [email protected]
remotion: link:../core
devDependencies:
'@jonny/eslint-config': 3.0.266_6hvhxfirl4y3p6nuemvvvbadcu
@@ -636,6 +636,8 @@ importers:
eslint: 8.13.0
prettier: 2.4.1
prettier-plugin-organize-imports: 2.3.4_pxx7xhvr2bexxfjjff3nukai7m
+ react: 18.0.0
+ react-dom: [email protected]
typescript: 4.7.2
packages/paths: