From ec3b21c272c86862335115b42e6bde517a700f47 Mon Sep 17 00:00:00 2001 From: wellwelwel <46850407+wellwelwel@users.noreply.github.com> Date: Mon, 4 Nov 2024 02:58:54 -0300 Subject: [PATCH 1/4] feat: support CommonJS --- index.cjs | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 ++- 2 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 index.cjs diff --git a/index.cjs b/index.cjs new file mode 100644 index 0000000..b69a3d0 --- /dev/null +++ b/index.cjs @@ -0,0 +1,171 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + // If the importer is in node compatibility mode or this is not an ESM + // file that has been converted to a CommonJS file using a Babel- + // compatible transform (i.e. "__esModule" has not been set), then set + // "default" to the CommonJS "module.exports" for node compatibility. + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// index.js +var supports_color_exports = {}; +__export(supports_color_exports, { + createSupportsColor: () => createSupportsColor, + default: () => supports_color_default +}); +module.exports = __toCommonJS(supports_color_exports); +var import_node_process = __toESM(require("node:process"), 1); +var import_node_os = __toESM(require("node:os"), 1); +var import_node_tty = __toESM(require("node:tty"), 1); +function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : import_node_process.default.argv) { + const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--"; + const position = argv.indexOf(prefix + flag); + const terminatorPosition = argv.indexOf("--"); + return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition); +} +var { env } = import_node_process.default; +var flagForceColor; +if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) { + flagForceColor = 0; +} else if (hasFlag("color") || hasFlag("colors") || hasFlag("color=true") || hasFlag("color=always")) { + flagForceColor = 1; +} +function envForceColor() { + if (!("FORCE_COLOR" in env)) { + return; + } + if (env.FORCE_COLOR === "true") { + return 1; + } + if (env.FORCE_COLOR === "false") { + return 0; + } + if (env.FORCE_COLOR.length === 0) { + return 1; + } + const level = Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3); + if (![0, 1, 2, 3].includes(level)) { + return; + } + return level; +} +function translateLevel(level) { + if (level === 0) { + return false; + } + return { + level, + hasBasic: true, + has256: level >= 2, + has16m: level >= 3 + }; +} +function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) { + const noFlagForceColor = envForceColor(); + if (noFlagForceColor !== void 0) { + flagForceColor = noFlagForceColor; + } + const forceColor = sniffFlags ? flagForceColor : noFlagForceColor; + if (forceColor !== void 0) { + return forceColor; + } + if (sniffFlags) { + if (hasFlag("color=16m") || hasFlag("color=full") || hasFlag("color=truecolor")) { + return 3; + } + if (hasFlag("color=256")) { + return 2; + } + } + if ("TF_BUILD" in env && "AGENT_NAME" in env) { + return 1; + } + if (haveStream && !streamIsTTY && forceColor === void 0) { + return 0; + } + const min = 0; + if (env.TERM === "dumb") { + return min; + } + if (import_node_process.default.platform === "win32") { + const osRelease = import_node_os.default.release().split("."); + if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) { + return Number(osRelease[2]) >= 14931 ? 3 : 2; + } + return 1; + } + if ("CI" in env) { + if ("GITHUB_ACTIONS" in env || "GITEA_ACTIONS" in env) { + return 3; + } + if (["TRAVIS", "CIRCLECI", "APPVEYOR", "GITLAB_CI", "BUILDKITE", "DRONE"].some((sign) => sign in env) || env.CI_NAME === "codeship") { + return 1; + } + return min; + } + if ("TEAMCITY_VERSION" in env) { + return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0; + } + if (env.COLORTERM === "truecolor") { + return 3; + } + if (env.TERM === "xterm-kitty") { + return 3; + } + if ("TERM_PROGRAM" in env) { + const version = Number.parseInt((env.TERM_PROGRAM_VERSION || "").split(".")[0], 10); + switch (env.TERM_PROGRAM) { + case "iTerm.app": { + return version >= 3 ? 3 : 2; + } + case "Apple_Terminal": { + return 2; + } + } + } + if (/-256(color)?$/i.test(env.TERM)) { + return 2; + } + if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) { + return 1; + } + if ("COLORTERM" in env) { + return 1; + } + return min; +} +function createSupportsColor(stream, options = {}) { + const level = _supportsColor(stream, { + streamIsTTY: stream && stream.isTTY, + ...options + }); + return translateLevel(level); +} +var supportsColor = { + stdout: createSupportsColor({ isTTY: import_node_tty.default.isatty(1) }), + stderr: createSupportsColor({ isTTY: import_node_tty.default.isatty(2) }) +}; +var supports_color_default = supportsColor; +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + createSupportsColor +}); diff --git a/package.json b/package.json index d13e654..a17e38e 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "type": "module", "exports": { "types": "./index.d.ts", - "node": "./index.js", + "require": "./index.cjs", + "import": "./index.js", "default": "./browser.js" }, "sideEffects": false, @@ -21,7 +22,8 @@ "node": ">=18" }, "scripts": { - "test": "xo && ava && tsd" + "test": "xo && ava && tsd", + "build:cjs": "esbuild index.js --bundle --platform=node --format=cjs --target=node18 --outfile=index.cjs" }, "files": [ "index.js", @@ -54,6 +56,7 @@ "devDependencies": { "@types/node": "^20.11.17", "ava": "^6.1.1", + "esbuild": "^0.24.0", "tsd": "^0.30.4", "xo": "^0.57.0" }, From 7d79c85206052d3d1498ea8acbd8637ccdb22e25 Mon Sep 17 00:00:00 2001 From: wellwelwel <46850407+wellwelwel@users.noreply.github.com> Date: Mon, 4 Nov 2024 03:05:54 -0300 Subject: [PATCH 2/4] chore: fix lint --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a17e38e..24994a9 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "node": ">=18" }, "scripts": { - "test": "xo && ava && tsd", + "test": "xo --ignore=index.cjs && ava && tsd", "build:cjs": "esbuild index.js --bundle --platform=node --format=cjs --target=node18 --outfile=index.cjs" }, "files": [ From 7fe65b9fe37030a90d40ea6b3ba8af751d6c7d06 Mon Sep 17 00:00:00 2001 From: wellwelwel <46850407+wellwelwel@users.noreply.github.com> Date: Mon, 4 Nov 2024 03:30:56 -0300 Subject: [PATCH 3/4] chore: restore browser support --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 24994a9..a2a972d 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "exports": { "types": "./index.d.ts", "require": "./index.cjs", - "import": "./index.js", + "node": "./index.js", "default": "./browser.js" }, "sideEffects": false, From e3a2a23c6735c8a0739774321b9951eeee65ef9a Mon Sep 17 00:00:00 2001 From: wellwelwel <46850407+wellwelwel@users.noreply.github.com> Date: Mon, 4 Nov 2024 04:17:54 -0300 Subject: [PATCH 4/4] chore: include the index.cjs to npm package --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index a2a972d..f65e60c 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ }, "files": [ "index.js", + "index.cjs", "index.d.ts", "browser.js", "browser.d.ts"