Skip to content

🎥 Make videos programmatically with React

fetch project info

Jonny Burger4008361d789a
diff --git a/packages/bundler/src/project-info.ts b/packages/bundler/src/project-info.ts
new file mode 100644
index 0000000000..4caa3494a8
--- /dev/null
+++ b/packages/bundler/src/project-info.ts
@@ -0,0 +1,22 @@
+import {existsSync} from 'fs';
+import path from 'path';
+
+export type ProjectInfo = {
+ videoFile: string | null;
+ relativeVideoFile: string | null;
+};
+
+export const getProjectInfo = (): Promise<ProjectInfo> => {
+ const pathsToLookFor = ['src/Video.tsx', 'src/Video.jsx'].map((p) => {
+ return path.join(process.cwd(), p);
+ });
+
+ const videoFile = pathsToLookFor.find((p) => existsSync(p)) ?? null;
+
+ return Promise.resolve({
+ videoFile,
+ relativeVideoFile: videoFile
+ ? path.relative(process.cwd(), videoFile)
+ : null,
+ });
+};
diff --git a/packages/bundler/src/start-server.ts b/packages/bundler/src/start-server.ts
index d347c3c558..cc59d28ede 100644
--- a/packages/bundler/src/start-server.ts
+++ b/packages/bundler/src/start-server.ts
@@ -9,6 +9,7 @@ import webpack from 'webpack';
// @ts-expect-error
import webpackHotMiddleware from 'webpack-hot-middleware';
import {getDesiredPort} from './get-port';
+import {getProjectInfo} from './project-info';
import {isUpdateAvailableWithTimeout} from './update-available';
import {webpackConfig} from './webpack-config';
@@ -50,7 +51,7 @@ export const startServer = async (
})
);
- app.get('/update', (req, res) => {
+ app.get('/api/update', (req, res) => {
isUpdateAvailableWithTimeout()
.then((data) => {
res.json(data);
@@ -62,6 +63,18 @@ export const startServer = async (
});
});
+ app.get('/api/project-info', (req, res) => {
+ getProjectInfo()
+ .then((data) => {
+ res.json(data);
+ })
+ .catch((err) => {
+ res.status(500).json({
+ err: err.message,
+ });
+ });
+ });
+
app.use('favicon.png', (req, res) => {
res.sendFile(path.join(__dirname, '..', 'web', 'favicon.png'));
});
diff --git a/packages/cli/src/editor/components/NewComposition/CopyHint.tsx b/packages/cli/src/editor/components/NewComposition/CopyHint.tsx
index 70012d67b1..72ebb2f50e 100644
--- a/packages/cli/src/editor/components/NewComposition/CopyHint.tsx
+++ b/packages/cli/src/editor/components/NewComposition/CopyHint.tsx
@@ -1,4 +1,5 @@
-import React from 'react';
+import {ProjectInfo} from '@remotion/bundler/src/project-info';
+import React, {useEffect, useState} from 'react';
import {LIGHT_TEXT} from '../../helpers/colors';
const style: React.CSSProperties = {
@@ -7,9 +8,27 @@ const style: React.CSSProperties = {
};
export const CopyHint: React.FC = () => {
+ const [projectInfo, setProjectInfo] = useState<ProjectInfo | null>(null);
+
+ useEffect(() => {
+ fetch(`/api/project-info`)
+ .then((res) => res.json())
+ .then((res) => {
+ setProjectInfo(res);
+ })
+ .catch((err) => {
+ console.log('Error fetching info about the project');
+ setProjectInfo(null);
+ });
+ }, []);
+
+ if (!projectInfo) {
+ return null;
+ }
+
return (
<div style={style}>
- Copy this into <br /> your src/Video.tsx file.
+ Copy this into <br /> your {projectInfo.relativeVideoFile} file.
</div>
);
};
diff --git a/packages/cli/src/editor/components/UpdateCheck.tsx b/packages/cli/src/editor/components/UpdateCheck.tsx
index 2f200a8cb0..033251267b 100644
--- a/packages/cli/src/editor/components/UpdateCheck.tsx
+++ b/packages/cli/src/editor/components/UpdateCheck.tsx
@@ -27,7 +27,7 @@ export const UpdateCheck = () => {
const {tabIndex} = useZIndex();
const checkForUpdates = useCallback(() => {
- fetch('/update')
+ fetch('/api/update')
.then((res) => res.json())
.then((d) => setInfo(d))
.catch((err) => {