Skip to content

🎥 Make videos programmatically with React

remove chalk

Jonny Burger27eb48119e73
diff --git a/packages/cli/package.json b/packages/cli/package.json
index bac8366430..597fccf11c 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -28,7 +28,6 @@
"@remotion/player": "3.0.23",
"@remotion/renderer": "3.0.23",
"better-opn": "2.1.1",
- "chalk": "4.1.2",
"dotenv": "9.0.2",
"memfs": "3.4.3",
"minimist": "1.2.6",
diff --git a/packages/cli/src/chalk/index.ts b/packages/cli/src/chalk/index.ts
new file mode 100644
index 0000000000..aafbfb00d0
--- /dev/null
+++ b/packages/cli/src/chalk/index.ts
@@ -0,0 +1,208 @@
+const hasColor = () => {
+ if (typeof process !== 'undefined') {
+ return process.env.FORCE_COLOR !== '0';
+ }
+
+ return false;
+};
+
+type Style = {
+ codes: [number, number];
+ name: string;
+ wrap?: (input: string, newline: boolean) => string;
+};
+
+type Method = (str: string) => string;
+
+type Colors = {
+ enabled: boolean;
+ visible: boolean;
+ styles: Record<string, Style>;
+ keys: Record<string, string[]>;
+ alias?: (name: string, col: string) => void;
+};
+
+type ColorsWithMethods = Colors & {
+ reset: Method;
+ bold: Method;
+ dim: Method;
+ italic: Method;
+ underline: Method;
+ inverse: Method;
+ hidden: Method;
+ strikethrough: Method;
+ black: Method;
+ red: Method;
+ green: Method;
+ yellow: Method;
+ blue: Method;
+ magenta: Method;
+ cyan: Method;
+ white: Method;
+ gray: Method;
+ bgBlack: Method;
+ bgRed: Method;
+ bgGreen: Method;
+ bgYellow: Method;
+ bgBlue: Method;
+ bgMagenta: Method;
+ bgCyan: Method;
+ bgWhite: Method;
+ blackBright: Method;
+ redBright: Method;
+ greenBright: Method;
+ yellowBright: Method;
+ blueBright: Method;
+ magentaBright: Method;
+ cyanBright: Method;
+ whiteBright: Method;
+ bgBlackBright: Method;
+ bgRedBright: Method;
+ bgGreenBright: Method;
+ bgYellowBright: Method;
+ bgBlueBright: Method;
+ bgMagentaBright: Method;
+ bgCyanBright: Method;
+ bgWhiteBright: Method;
+};
+
+export const chalk = (() => {
+ const colors: Colors = {
+ enabled: hasColor(),
+ visible: true,
+ styles: {},
+ keys: {},
+ };
+
+ const ansi = (st: Style) => {
+ const open = `\u001b[${st.codes[0]}m`;
+ const close = `\u001b[${st.codes[1]}m`;
+ const regex = new RegExp(`\\u001b\\[${st.codes[1]}m`, 'g');
+ st.wrap = (input: string, newline: boolean) => {
+ if (input.includes(close)) input = input.replace(regex, close + open);
+ const output = open + input + close;
+ // see https://github.com/chalk/chalk/pull/92, thanks to the
+ // chalk contributors for this fix. However, we've confirmed that
+ // this issue is also present in Windows terminals
+ return newline ? output.replace(/\r*\n/g, `${close}$&${open}`) : output;
+ };
+
+ return st;
+ };
+
+ const wrap = (sty: Style, input: string, newline: boolean) => {
+ return sty.wrap?.(input, newline) as string;
+ };
+
+ const style = (input: string | null | undefined, stack: string[]) => {
+ if (input === '' || input === null || input === undefined) return '';
+ if (colors.enabled === false) return input;
+ if (colors.visible === false) return '';
+ let str = String(input);
+ const nl = str.includes('\n');
+ let n = stack.length;
+
+ while (n-- > 0) str = wrap(colors.styles[stack[n]], str, nl);
+ return str;
+ };
+
+ const define = (name: string, codes: [number, number], type: string) => {
+ colors.styles[name] = ansi({name, codes});
+ const keys = colors.keys[type] || (colors.keys[type] = []);
+ keys.push(name);
+
+ Reflect.defineProperty(colors, name, {
+ configurable: true,
+ enumerable: true,
+ set(value) {
+ colors.alias?.(name, value);
+ },
+ get() {
+ const color = (input: string) => style(input, color.stack);
+ Reflect.setPrototypeOf(color, colors);
+ color.stack = this.stack ? this.stack.concat(name) : [name];
+ return color;
+ },
+ });
+ };
+
+ define('reset', [0, 0], 'modifier');
+ define('bold', [1, 22], 'modifier');
+ define('dim', [2, 22], 'modifier');
+ define('italic', [3, 23], 'modifier');
+ define('underline', [4, 24], 'modifier');
+ define('inverse', [7, 27], 'modifier');
+ define('hidden', [8, 28], 'modifier');
+ define('strikethrough', [9, 29], 'modifier');
+
+ define('black', [30, 39], 'color');
+ define('red', [31, 39], 'color');
+ define('green', [32, 39], 'color');
+ define('yellow', [33, 39], 'color');
+ define('blue', [34, 39], 'color');
+ define('magenta', [35, 39], 'color');
+ define('cyan', [36, 39], 'color');
+ define('white', [37, 39], 'color');
+ define('gray', [90, 39], 'color');
+ define('grey', [90, 39], 'color');
+
+ define('bgBlack', [40, 49], 'bg');
+ define('bgRed', [41, 49], 'bg');
+ define('bgGreen', [42, 49], 'bg');
+ define('bgYellow', [43, 49], 'bg');
+ define('bgBlue', [44, 49], 'bg');
+ define('bgMagenta', [45, 49], 'bg');
+ define('bgCyan', [46, 49], 'bg');
+ define('bgWhite', [47, 49], 'bg');
+
+ define('blackBright', [90, 39], 'bright');
+ define('redBright', [91, 39], 'bright');
+ define('greenBright', [92, 39], 'bright');
+ define('yellowBright', [93, 39], 'bright');
+ define('blueBright', [94, 39], 'bright');
+ define('magentaBright', [95, 39], 'bright');
+ define('cyanBright', [96, 39], 'bright');
+ define('whiteBright', [97, 39], 'bright');
+
+ define('bgBlackBright', [100, 49], 'bgBright');
+ define('bgRedBright', [101, 49], 'bgBright');
+ define('bgGreenBright', [102, 49], 'bgBright');
+ define('bgYellowBright', [103, 49], 'bgBright');
+ define('bgBlueBright', [104, 49], 'bgBright');
+ define('bgMagentaBright', [105, 49], 'bgBright');
+ define('bgCyanBright', [106, 49], 'bgBright');
+ define('bgWhiteBright', [107, 49], 'bgBright');
+
+ colors.alias = (name: string, color: string) => {
+ // @ts-expect-error
+ const fn = colors[color];
+
+ if (typeof fn !== 'function') {
+ throw new TypeError(
+ 'Expected alias to be the name of an existing color (string) or a function'
+ );
+ }
+
+ if (!fn.stack) {
+ Reflect.defineProperty(fn, 'name', {value: name});
+ colors.styles[name] = fn;
+ fn.stack = [name];
+ }
+
+ Reflect.defineProperty(colors, name, {
+ configurable: true,
+ enumerable: true,
+ set(value) {
+ colors.alias?.(name, value);
+ },
+ get() {
+ const col = (input: string) => style(input, col.stack);
+ Reflect.setPrototypeOf(col, colors);
+ col.stack = this.stack ? this.stack.concat(fn.stack) : fn.stack;
+ return col;
+ },
+ });
+ };
+
+ return colors as ColorsWithMethods;
+})();
diff --git a/packages/cli/src/code-frame.ts b/packages/cli/src/code-frame.ts
index 54a8001a2c..f76c867b82 100644
--- a/packages/cli/src/code-frame.ts
+++ b/packages/cli/src/code-frame.ts
@@ -1,6 +1,9 @@
-import type {ErrorWithStackFrame, SymbolicatedStackFrame} from '@remotion/renderer';
-import chalk from 'chalk';
+import type {
+ ErrorWithStackFrame,
+ SymbolicatedStackFrame,
+} from '@remotion/renderer';
import {Internals} from 'remotion';
+import {chalk} from './chalk';
import {Log} from './log';
const makeFileName = (firstFrame: SymbolicatedStackFrame) => {
diff --git a/packages/cli/src/index.tsx b/packages/cli/src/index.tsx
index eaf6b0ac13..c885686403 100644
--- a/packages/cli/src/index.tsx
+++ b/packages/cli/src/index.tsx
@@ -1,4 +1,4 @@
-import chalk from 'chalk';
+import {chalk} from './chalk';
import {checkNodeVersion} from './check-version';
import {listCompositionsCommand} from './compositions';
import {getCliOptions} from './get-cli-options';
diff --git a/packages/cli/src/log.ts b/packages/cli/src/log.ts
index 4ce549e625..f7fbfcc11e 100644
--- a/packages/cli/src/log.ts
+++ b/packages/cli/src/log.ts
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
-import chalk from 'chalk';
import {Internals} from 'remotion';
+import {chalk} from './chalk';
export const Log = {
verbose: (...args: Parameters<typeof console.log>) => {
@@ -11,7 +11,7 @@ export const Log = {
'verbose'
)
) {
- return console.log(chalk.blueBright(...args));
+ return console.log(...args.map((a) => chalk.blueBright(a)));
}
},
info: (...args: Parameters<typeof console.log>) => {
@@ -31,7 +31,7 @@ export const Log = {
'warn'
)
) {
- return console.warn(chalk.yellow(...args));
+ return console.warn(...args.map((a) => chalk.yellow(a)));
}
},
error: (...args: Parameters<typeof console.log>) => {
@@ -41,7 +41,7 @@ export const Log = {
'error'
)
) {
- return console.error(chalk.red(...args));
+ return console.error(...args.map((a) => chalk.red(a)));
}
},
};
diff --git a/packages/cli/src/print-error.ts b/packages/cli/src/print-error.ts
index ad16dfbbb1..00b07c9530 100644
--- a/packages/cli/src/print-error.ts
+++ b/packages/cli/src/print-error.ts
@@ -1,5 +1,5 @@
import {RenderInternals} from '@remotion/renderer';
-import chalk from 'chalk';
+import {chalk} from './chalk';
import {printCodeFrameAndStack} from './code-frame';
import {Log} from './log';
import {createOverwriteableCliOutput} from './progress-bar';
diff --git a/packages/cli/src/print-help.ts b/packages/cli/src/print-help.ts
index 0f188ea60c..92e0607919 100644
--- a/packages/cli/src/print-help.ts
+++ b/packages/cli/src/print-help.ts
@@ -1,4 +1,4 @@
-import chalk from 'chalk';
+import {chalk} from './chalk';
import {Log} from './log';
import {VERSIONS_COMMAND} from './versions';
diff --git a/packages/cli/src/progress-bar.ts b/packages/cli/src/progress-bar.ts
index 7ca171b5a6..b5902113fc 100644
--- a/packages/cli/src/progress-bar.ts
+++ b/packages/cli/src/progress-bar.ts
@@ -1,7 +1,7 @@
import type {StitchingState} from '@remotion/renderer';
-import chalk from 'chalk';
import {Internals} from 'remotion';
import {AnsiDiff} from './ansi/ansi-diff';
+import {chalk} from './chalk';
import {makeMultiDownloadProgress} from './download-progress';
import {makeProgressBar} from './make-progress-bar';
import type {RenderStep} from './step';
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index c18b267c19..85a4c502e3 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -6,11 +6,11 @@ import {
RenderInternals,
renderMedia,
} from '@remotion/renderer';
-import chalk from 'chalk';
import fs from 'fs';
import os from 'os';
import path from 'path';
import {Internals} from 'remotion';
+import {chalk} from './chalk';
import {getCliOptions} from './get-cli-options';
import {getCompositionId} from './get-composition-id';
import {initializeRenderCli} from './initialize-render-cli';
diff --git a/packages/cli/src/still.ts b/packages/cli/src/still.ts
index 38a0ab6851..958ed5dc76 100644
--- a/packages/cli/src/still.ts
+++ b/packages/cli/src/still.ts
@@ -1,22 +1,20 @@
-import type {
- RenderMediaOnDownload} from '@remotion/renderer';
+import type {RenderMediaOnDownload} from '@remotion/renderer';
import {
getCompositions,
openBrowser,
RenderInternals,
renderStill,
} from '@remotion/renderer';
-import chalk from 'chalk';
import {mkdirSync} from 'fs';
import path from 'path';
import {Config, Internals} from 'remotion';
+import {chalk} from './chalk';
import {getCliOptions} from './get-cli-options';
import {getCompositionId} from './get-composition-id';
import {initializeRenderCli} from './initialize-render-cli';
import {Log} from './log';
import {parsedCli, quietFlagProvided} from './parse-command-line';
-import type {
- DownloadProgress} from './progress-bar';
+import type {DownloadProgress} from './progress-bar';
import {
createOverwriteableCliOutput,
makeRenderingAndStitchingProgress,
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ef9b616558..baa0537cb5 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -113,7 +113,6 @@ importers:
'@typescript-eslint/eslint-plugin': 5.18.0
'@typescript-eslint/parser': 5.18.0
better-opn: 2.1.1
- chalk: 4.1.2
dotenv: 9.0.2
eslint: 8.13.0
eslint-plugin-10x: 1.5.2
@@ -137,7 +136,6 @@ importers:
'@remotion/player': link:../player
'@remotion/renderer': link:../renderer
better-opn: 2.1.1
- chalk: 4.1.2
dotenv: 9.0.2
memfs: 3.4.3
minimist: 1.2.6
@@ -203,7 +201,7 @@ importers:
react-dom: [email protected]
react-test-renderer: [email protected]
rimraf: 3.0.2
- ts-jest: 27.0.5_h5rbqvnmqo6ezjz2gx5x5dlwkm
+ ts-jest: 27.0.5_f7drhotxik6wwmizpivq74sxca
typescript: 4.7.2
webpack: 5.72.0
@@ -362,7 +360,7 @@ importers:
devDependencies:
eslint: 8.13.0
jest: 27.2.4
- ts-jest: 27.0.5_zwhdjizg2b4clj63vswakbrchm
+ ts-jest: 27.0.5_xnn4tb2boqbkyhoppxe3xlcolq
typescript: 4.7.2
packages/example:
@@ -19287,7 +19285,7 @@ packages:
yargs-parser: 20.2.9
dev: true
- /ts-jest/27.0.5_zwhdjizg2b4clj63vswakbrchm:
+ /ts-jest/27.0.5_xnn4tb2boqbkyhoppxe3xlcolq:
resolution: {integrity: sha512-lIJApzfTaSSbtlksfFNHkWOzLJuuSm4faFAfo5kvzOiRAuoN4/eKxVJ2zEAho8aecE04qX6K1pAzfH5QHL1/8w==}
engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0}
hasBin: true
@@ -19305,7 +19303,6 @@ packages:
babel-jest:
optional: true
dependencies:
- '@babel/core': 7.17.9
bs-logger: 0.2.6
fast-json-stable-stringify: 2.1.0
jest: 27.2.4