From 7b9a26884db1cbf480a3d165447222f6e8296118 Mon Sep 17 00:00:00 2001 From: Alhadis Date: Sat, 6 Oct 2018 18:06:18 +1000 Subject: [PATCH] Stick to CommonJS as entry-points for executables This is considerably less complicated than the hacky workaround required to run `bin/html-tty` with Node's ESM flag. Not to mention it eliminates risk of breakage in future releases. --- bin/html-tty | 9 ++++----- lib/adapters/esm.js | 38 -------------------------------------- 2 files changed, 4 insertions(+), 43 deletions(-) delete mode 100644 lib/adapters/esm.js diff --git a/bin/html-tty b/bin/html-tty index 1477aca..874b3fc 100755 --- a/bin/html-tty +++ b/bin/html-tty @@ -1,7 +1,7 @@ #!/usr/bin/env node "use strict"; -require("../lib/adapters/esm.js")(() => new Promise(resolve => { +new Promise(resolve => { const fs = require("fs"); if(process.argv[2]) resolve(fs.readFileSync(process.argv[2], "utf8")); @@ -13,9 +13,8 @@ require("../lib/adapters/esm.js")(() => new Promise(resolve => { null !== chunk ? input += chunk : resolve(input); }); } -}).then(async data => { - const TTYRenderer = (await import("../lib/postproc/tty/renderer.mjs")).default; - const htmlTTY = new TTYRenderer(); +}).then(data => { + const htmlTTY = new (require("..").TTYRenderer)(); const isRaw = /\x08/.test(data); let output = htmlTTY.process(data, isRaw); if(process.stdout.isTTY) @@ -38,4 +37,4 @@ require("../lib/adapters/esm.js")(() => new Promise(resolve => { }).catch(error => { console.error(error); process.exit(1); -})); +}); diff --git a/lib/adapters/esm.js b/lib/adapters/esm.js deleted file mode 100644 index d9e1c99..0000000 --- a/lib/adapters/esm.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Hack for executables needing an entry-point into an ESM graph. - * - * Executes the given handler only if the current process appears - * to be running with ESM support enabled. Otherwise, a new process - * is forked with Node's `--experimental-modules` flag set. - * - * @param {Function} handler - * @internal - */ -module.exports = handler => { - "use strict"; - - // Raise an exception for Node versions older than v8.5.0 - const [major, minor] = process.version.replace(/^\v/, "").split(".").map(Number); - if(major < 8 || 8 === major && minor < 5){ - console.error("This program requires Node.js v8.5.0 or later."); - process.exit(1); - } - - // ESM support enabled - const opts = (process.env.NODE_OPTIONS || "").split(/\s+/).filter(Boolean); - if(-1 !== opts.indexOf("--experimental-modules")) - return handler(); - - // ESM unsupported; perform a hacky workaround - const {spawn} = require("child_process"); - const proc = spawn("node", process.argv.slice(1), { - stdio: ["inherit", "inherit", "pipe"], - env: Object.assign({}, process.env, { - NODE_OPTIONS: (process.env.NODE_OPTIONS || "") + " --experimental-modules", - }), - }); - proc.stderr.on("data", data => { - data = String(data).replace(/^.*ExperimentalWarning: The ESM module loader is experimental.*\n/m, ""); - process.stderr.write(data); - }); -};