Skip to content

Commit

Permalink
fix: stderr to support tty (colors in errors) (#21)
Browse files Browse the repository at this point in the history
fix: stderr with color
  • Loading branch information
privatenumber authored May 25, 2022
1 parent 85f5219 commit bd3ec4a
Showing 4 changed files with 23 additions and 60 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
"exports": {
".": "./dist/loader.js",
"./cli": "./dist/cli.js",
"./suppress-warnings": "./dist/suppress-warnings.cjs",
"./repl": "./dist/repl.js"
},
"bin": "./dist/cli.js",
53 changes: 0 additions & 53 deletions src/ignore-node-warnings.ts

This file was deleted.

11 changes: 4 additions & 7 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { StdioOptions } from 'child_process';
import { pathToFileURL } from 'url';
import spawn from 'cross-spawn';
import { ignoreNodeWarnings } from './ignore-node-warnings';

export function run(
argv: string[],
@@ -21,7 +20,7 @@ export function run(
const stdio: StdioOptions = [
'inherit', // stdin
'inherit', // stdout
'pipe', // stderr
'inherit', // stderr
];

if (options?.ipc) {
@@ -32,6 +31,9 @@ export function run(
const childProcess = spawn(
process.execPath,
[
'--require',
require.resolve('./suppress-warnings.cjs'),

'--loader',
pathToFileURL(require.resolve('./loader.js')).toString(),

@@ -43,10 +45,5 @@ export function run(
},
);

// Suppress warnings about using experimental features
childProcess.stderr!
.pipe(ignoreNodeWarnings())
.pipe(process.stderr);

return childProcess;
}
18 changes: 18 additions & 0 deletions src/suppress-warnings.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const ignoreWarnings = new Set([
'--experimental-loader is an experimental feature. This feature could change at any time',
'Custom ESM Loaders is an experimental feature. This feature could change at any time',
]);

const { emit } = process;

// @ts-expect-error emit type mismatch
process.emit = function (event: 'warning', warning: Error) {
if (
event === 'warning'
&& ignoreWarnings.has(warning.message)
) {
return;
}

return Reflect.apply(emit, this, arguments);
};

0 comments on commit bd3ec4a

Please sign in to comment.