Skip to content

πŸŽ₯ Make videos programmatically with React

validate everything has the correct version

Jonny Burger93341cebd05f
diff --git a/packages/cli/src/index.tsx b/packages/cli/src/index.tsx
index f398b54cc2..ffcbb5dcda 100644
--- a/packages/cli/src/index.tsx
+++ b/packages/cli/src/index.tsx
@@ -6,6 +6,11 @@ import {printHelp} from './print-help';
import {render} from './render';
import {still} from './still';
import {upgrade} from './upgrade';
+import {
+ validateVersionsBeforeCommand,
+ versionsCommand,
+ VERSIONS_COMMAND,
+} from './versions';
Error.stackTraceLimit = Infinity;
export const cli = async () => {
@@ -19,6 +24,10 @@ export const cli = async () => {
// To check node version and to warn if node version is <12.10.0
checkNodeVersion();
+ if (command !== VERSIONS_COMMAND) {
+ await validateVersionsBeforeCommand();
+ }
+
try {
if (command === 'preview') {
await previewCommand();
@@ -28,6 +37,8 @@ export const cli = async () => {
await still();
} else if (command === 'upgrade') {
await upgrade();
+ } else if (command === VERSIONS_COMMAND) {
+ await versionsCommand();
} else if (command === 'help') {
printHelp();
process.exit(0);
diff --git a/packages/cli/src/parse-command-line.ts b/packages/cli/src/parse-command-line.ts
index d702db89fe..b520032391 100644
--- a/packages/cli/src/parse-command-line.ts
+++ b/packages/cli/src/parse-command-line.ts
@@ -42,7 +42,7 @@ export const parsedCli = minimist<CommandLineOptions>(process.argv.slice(2), {
boolean: ['force', 'overwrite', 'sequence', 'help'],
});
-export const parseCommandLine = (type: 'still' | 'sequence') => {
+export const parseCommandLine = (type: 'still' | 'sequence' | 'versions') => {
if (parsedCli['pixel-format']) {
Config.Output.setPixelFormat(parsedCli['pixel-format']);
}
diff --git a/packages/cli/src/print-help.ts b/packages/cli/src/print-help.ts
index 41eb0a510c..f27af74121 100644
--- a/packages/cli/src/print-help.ts
+++ b/packages/cli/src/print-help.ts
@@ -1,5 +1,6 @@
import chalk from 'chalk';
import {Log} from './log';
+import {VERSIONS_COMMAND} from './versions';
const packagejson = require('../package.json');
@@ -42,6 +43,7 @@ export const printHelp = () => {
['--port', 'Custom port to use for the HTTP server'],
['--env-file', 'Specify a location for a dotenv file'],
]);
+ Log.info();
Log.info('remotion still <index-file.ts> <comp-name> <still.png>');
Log.info(chalk.gray('Render a still frame and save it as an image.'));
printFlags([
@@ -58,6 +60,11 @@ export const printHelp = () => {
['--env-file', 'Specify a location for a dotenv file'],
]);
Log.info();
+ Log.info('remotion ' + VERSIONS_COMMAND);
+ Log.info(
+ chalk.gray('Prints and validates versions of all Remotion packages.')
+ );
+ Log.info();
Log.info('remotion upgrade');
Log.info(chalk.gray('Ensure Remotion is on the newest version.'));
Log.info();
diff --git a/packages/cli/src/resolve-from.ts b/packages/cli/src/resolve-from.ts
new file mode 100644
index 0000000000..99c863be54
--- /dev/null
+++ b/packages/cli/src/resolve-from.ts
@@ -0,0 +1,31 @@
+import path from 'path';
+import Module = require('module');
+import fs from 'fs';
+
+export const resolveFrom = (
+ fromDirectory: string,
+ moduleId: string
+): string => {
+ try {
+ fromDirectory = fs.realpathSync(fromDirectory);
+ } catch (error) {
+ if ((error as {code: string}).code === 'ENOENT') {
+ fromDirectory = path.resolve(fromDirectory);
+ } else {
+ throw error;
+ }
+ }
+
+ const fromFile = path.join(fromDirectory, 'noop.js');
+
+ const resolveFileName = () =>
+ // @ts-expect-error
+ Module._resolveFilename(moduleId, {
+ id: fromFile,
+ filename: fromFile,
+ // @ts-expect-error
+ paths: Module._nodeModulePaths(fromDirectory),
+ });
+
+ return resolveFileName();
+};
diff --git a/packages/cli/src/versions.ts b/packages/cli/src/versions.ts
new file mode 100644
index 0000000000..65b8723cee
--- /dev/null
+++ b/packages/cli/src/versions.ts
@@ -0,0 +1,136 @@
+import {readFile} from 'fs/promises';
+import {Internals} from 'remotion';
+import {Log} from './log';
+import {parseCommandLine} from './parse-command-line';
+import {resolveFrom} from './resolve-from';
+
+const packages = [
+ '@remotion/bundler',
+ '@remotion/cli',
+ '@remotion/eslint-config',
+ '@remotion/renderer',
+ '@remotion/media-utils',
+ '@remotion/babel-loader',
+ '@remotion/lambda',
+ '@remotion/three',
+ '@remotion/gif',
+ 'remotion',
+];
+
+const getVersion = async (p: string): Promise<string | null> => {
+ try {
+ const remotionPkgJson = resolveFrom(process.cwd(), `${p}/package.json`);
+ const file = await readFile(remotionPkgJson, 'utf-8');
+ const packageJson = JSON.parse(file);
+ return packageJson.version;
+ } catch (err) {
+ return null;
+ }
+};
+
+const groupBy = (vals: [string, string][]) => {
+ const groups: {[key: string]: string[]} = {};
+ for (const [pkg, version] of vals) {
+ if (!groups[version]) {
+ groups[version] = [];
+ }
+
+ groups[version].push(pkg);
+ }
+
+ return groups;
+};
+
+const getAllVersions = async (): Promise<[string, string][]> => {
+ return (
+ await Promise.all(
+ packages.map(async (p) => [p, await getVersion(p)] as [string, string])
+ )
+ ).filter(([, version]) => version);
+};
+
+export const VERSIONS_COMMAND = 'versions';
+
+export const validateVersionsBeforeCommand = async () => {
+ const versions = await getAllVersions();
+
+ const grouped = groupBy(versions);
+
+ const installedVersions = Object.keys(grouped);
+
+ if (installedVersions.length === 1) {
+ return;
+ }
+
+ Log.warn('-------------');
+ Log.warn('Version mismatch:');
+ for (const version of installedVersions) {
+ Log.warn(`- On version: ${version}`);
+ for (const pkg of grouped[version]) {
+ Log.warn(` - ${pkg}`);
+ }
+
+ Log.info();
+ }
+
+ Log.warn('You may experience breakages such as:');
+ Log.warn('- React context and hooks not working');
+ Log.warn('- Type errors and feature incompatibilities');
+ Log.warn('- Failed renders and unclear errors');
+ Log.warn();
+ Log.warn('To resolve:');
+ Log.warn(
+ '- Make sure your package.json has all Remotion packages pointing to the same version.'
+ );
+ Log.warn(
+ '- Remove the `^` character in front of a version to pin a package.'
+ );
+ if (!Internals.Logging.isEqualOrBelowLogLevel('verbose')) {
+ Log.warn(
+ '- Run `npx remotion versions --log=verbose` to see the path of the modules resolved.'
+ );
+ }
+
+ Log.warn('-------------');
+ Log.info();
+};
+
+export const versionsCommand = async () => {
+ parseCommandLine('versions');
+ const versions = await getAllVersions();
+
+ const grouped = groupBy(versions);
+
+ const installedVersions = Object.keys(grouped);
+
+ for (const version of installedVersions) {
+ Log.info(`On version: ${version}`);
+ for (const pkg of grouped[version]) {
+ Log.info(`- ${pkg}`);
+ Log.verbose(` ${resolveFrom(process.cwd(), `${pkg}/package.json`)}`);
+ }
+
+ Log.info();
+ }
+
+ if (installedVersions.length === 1) {
+ Log.info(`βœ… Great! All packages have the same version.`);
+ } else {
+ Log.error(
+ 'Version mismatch: Not all Remotion packages have the same version.'
+ );
+ Log.info(
+ '- Make sure your package.json has all Remotion packages pointing to the same version.'
+ );
+ Log.info(
+ '- Remove the `^` character in front of a version to pin a package.'
+ );
+ if (!Internals.Logging.isEqualOrBelowLogLevel('verbose')) {
+ Log.info(
+ '- Rerun this command with --log=verbose to see the path of the modules resolved.'
+ );
+ }
+
+ process.exit(1);
+ }
+};