🎥 Make videos programmatically with React
show copy progress
Jonny Burger07d4897fb449
diff --git a/packages/bundler/src/bundle.ts b/packages/bundler/src/bundle.tsindex 7214985870..1224fe41d5 100644--- a/packages/bundler/src/bundle.ts+++ b/packages/bundler/src/bundle.ts@@ -43,6 +43,8 @@ export type LegacyBundleOptions = {publicPath?: string;rootDir?: string;publicDir?: string | null;+ onPublicDirCopyProgress?: (bytes: number) => void;+ onSymlinkDetected?: (path: string) => void;};export const getConfig = ({@@ -187,17 +189,25 @@ export async function bundle(...args: Arguments): Promise<string> {return;}+ const absolutePath = path.join(src, ent.name);+ if (options.onSymlinkDetected) {+ options.onSymlinkDetected(absolutePath);+ return;+ }+symlinkWarningShown = true;console.warn(- `\nFound a symbolic link in the public folder (${path.join(- src,- ent.name- )}). The symlink will be forwarded into the bundle.`+ `\nFound a symbolic link in the public folder (${absolutePath}). The symlink will be forwarded into the bundle.`);};if (fs.existsSync(from)) {- await copyDir({src: from, dest: to, onSymlinkDetected: showSymlinkWarning});+ await copyDir({+ src: from,+ dest: to,+ onSymlinkDetected: showSymlinkWarning,+ onProgress: (prog) => options.onPublicDirCopyProgress?.(prog),+ });}const html = indexHtml({diff --git a/packages/bundler/src/copy-dir.ts b/packages/bundler/src/copy-dir.tsindex 2ce8c4aebc..72565ccc47 100644--- a/packages/bundler/src/copy-dir.ts+++ b/packages/bundler/src/copy-dir.ts@@ -5,26 +5,37 @@ export async function copyDir({src,dest,onSymlinkDetected,+ onProgress,}: {src: string;dest: string;onSymlinkDetected: (entry: fs.Dirent, dir: string) => void;+ onProgress: (bytes: number) => void;}) {await fs.promises.mkdir(dest, {recursive: true});const entries = await fs.promises.readdir(src, {withFileTypes: true});+ let copied = 0;+for (const entry of entries) {const srcPath = path.join(src, entry.name);const destPath = path.join(dest, entry.name);if (entry.isDirectory()) {- await copyDir({src: srcPath, dest: destPath, onSymlinkDetected});+ await copyDir({+ src: srcPath,+ dest: destPath,+ onSymlinkDetected,+ onProgress,+ });} else if (entry.isSymbolicLink()) {const realpath = await fs.promises.realpath(srcPath);onSymlinkDetected(entry, src);await fs.promises.symlink(realpath, destPath);} else {await fs.promises.copyFile(srcPath, destPath);+ copied += fs.statSync(srcPath).size;+ onProgress(copied);}}}diff --git a/packages/cli/src/progress-bar.ts b/packages/cli/src/progress-bar.tsindex b4e5f86478..6277c28931 100644--- a/packages/cli/src/progress-bar.ts+++ b/packages/cli/src/progress-bar.ts@@ -3,7 +3,10 @@ import {RenderInternals} from '@remotion/renderer';import {AnsiDiff} from './ansi/ansi-diff';import {chalk} from './chalk';import {ConfigInternals} from './config';-import {makeMultiDownloadProgress} from './download-progress';+import {+ getFileSizeDownloadBar,+ makeMultiDownloadProgress,+} from './download-progress';import {makeProgressBar} from './make-progress-bar';import type {RenderStep} from './step';import {truthy} from './truthy';@@ -38,7 +41,7 @@ export const createOverwriteableCliOutput = (quiet: boolean) => {};};-export const makeBundlingProgress = ({+const makeBundlingProgress = ({progress,steps,doneIn,@@ -58,6 +61,74 @@ export const makeBundlingProgress = ({.filter(truthy).join(' ');+const makeCopyingProgress = (options: CopyingState) => {+ // Don't show copy progress lower than 100MB+ if (options.bytes < 1000 * 1000 * 100) {+ return null;+ }++ return [+ ' +',+ options.doneIn ? makeProgressBar(1) : getFileSizeDownloadBar(options.bytes),+ 'Copying public dir',+ options.doneIn === null ? null : chalk.gray(`${options.doneIn}ms`),+ ]+ .filter(truthy)+ .join(' ');+};++const makeSymlinkProgress = (options: SymbolicLinksState) => {+ if (options.symlinks.length === 0) {+ return null;+ }++ if (options.symlinks.length === 1) {+ return [+ chalk.gray(` Found a symbolic link in the public folder:`),+ chalk.gray(' ' + options.symlinks[0]),+ chalk.gray(' The symlink will be forwarded in to the bundle.'),+ ].join('\n');+ }++ return [+ chalk.gray(+ ` Found ${options.symlinks.length} symbolic links in the public folder.`+ ),+ chalk.gray(' The symlinks will be forwarded in to the bundle.'),+ ].join('\n');+};++export type CopyingState = {+ bytes: number;+ doneIn: number | null;+};++export type BundlingState = {+ progress: number;+ steps: RenderStep[];+ doneIn: number | null;+};++export type SymbolicLinksState = {symlinks: string[]};++export const makeBundlingAndCopyProgress = ({+ bundling,+ copying,+ symLinks,+}: {+ bundling: BundlingState;+ copying: CopyingState;+ symLinks: SymbolicLinksState;+}) => {+ return [+ makeBundlingProgress(bundling),+ makeCopyingProgress(copying),+ makeSymlinkProgress(symLinks),+ ]+ .filter(truthy)+ .join('\n');+};+type RenderingProgressInput = {frames: number;totalFrames: number;diff --git a/packages/cli/src/setup-cache.ts b/packages/cli/src/setup-cache.tsindex 95601bf0e6..f8a132e284 100644--- a/packages/cli/src/setup-cache.ts+++ b/packages/cli/src/setup-cache.ts@@ -4,9 +4,14 @@ import {RenderInternals} from '@remotion/renderer';import {ConfigInternals} from './config';import {Log} from './log';import {quietFlagProvided} from './parse-command-line';+import type {+ BundlingState,+ CopyingState,+ SymbolicLinksState,+} from './progress-bar';import {createOverwriteableCliOutput,- makeBundlingProgress,+ makeBundlingAndCopyProgress,} from './progress-bar';import type {RenderStep} from './step';@@ -52,21 +57,66 @@ export const bundleOnCli = async ({}) => {const shouldCache = ConfigInternals.getWebpackCaching();+ const symlinkState: SymbolicLinksState = {+ symlinks: [],+ };+const onProgress = (progress: number) => {+ bundlingState = {+ progress: progress / 100,+ steps,+ doneIn: null,+ };bundlingProgress.update(- makeBundlingProgress({- progress: progress / 100,- steps,- doneIn: null,+ makeBundlingAndCopyProgress({+ bundling: bundlingState,+ copying: copyingState,+ symLinks: symlinkState,}));};+ let copyingState: CopyingState = {+ bytes: 0,+ doneIn: null,+ };++ let copyStart: number | null = null;++ const updateProgress = (newline: boolean) => {+ bundlingProgress.update(+ makeBundlingAndCopyProgress({+ bundling: bundlingState,+ copying: copyingState,+ symLinks: symlinkState,+ }) + (newline ? '\n' : '')+ );+ };++ const onPublicDirCopyProgress = (bytes: number) => {+ if (copyStart === null) {+ copyStart = Date.now();+ }++ copyingState = {+ bytes,+ doneIn: null,+ };+ updateProgress(false);+ };++ const onSymlinkDetected = (absPath: string) => {+ symlinkState.symlinks.push(absPath);+ updateProgress(false);+ };+const options: LegacyBundleOptions = {enableCaching: shouldCache,webpackOverride: ConfigInternals.getWebpackOverrideFn() ?? ((f) => f),rootDir: remotionRoot,publicDir,+ onPublicDirCopyProgress,+ onSymlinkDetected,};const [hash] = BundlerInternals.getConfig({@@ -95,26 +145,36 @@ export const bundleOnCli = async ({const bundleStartTime = Date.now();const bundlingProgress = createOverwriteableCliOutput(quietFlagProvided());+ let bundlingState: BundlingState = {+ progress: 0,+ steps,+ doneIn: null,+ };+const bundled = await bundle({entryPoint: fullPath,onProgress: (progress) => {- bundlingProgress.update(- makeBundlingProgress({- progress: progress / 100,- steps,- doneIn: null,- })- );+ bundlingState = {+ progress: progress / 100,+ steps,+ doneIn: null,+ };+ updateProgress(false);},...options,});- bundlingProgress.update(- makeBundlingProgress({- progress: 1,- steps,- doneIn: Date.now() - bundleStartTime,- }) + '\n'- );++ bundlingState = {+ progress: 1,+ steps,+ doneIn: Date.now() - bundleStartTime,+ };+ copyingState = {+ ...copyingState,+ doneIn: copyStart ? Date.now() - copyStart : null,+ };+ updateProgress(true);+Log.verbose('Bundled under', bundled);const cacheExistedAfter =BundlerInternals.cacheExists(remotionRoot, 'production', hash) === 'exists';