Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix color detection in edge runtime #56

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/benchmarks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Benchmarks

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

env:
FORCE_COLOR: 3
Expand All @@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
node-version: 18
- run: npm install
- name: Ensure color support detection
run: node tests/environments.js
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Testing

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
modern:
Expand All @@ -13,6 +13,7 @@ jobs:
strategy:
matrix:
node-version:
- 18
- 16
- 14
- 12
Expand Down
14 changes: 7 additions & 7 deletions picocolors.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
let tty = require("tty")

let argv = process.argv || [],
env = process.env
let isColorSupported =
Copy link

@hydRAnger hydRAnger Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, shall we also handle scenarios when process is undefined? (like in the browser)

env = typeof process !== 'undefined' ? process.env : {};

or would you like me to make another PR?

!("NO_COLOR" in process.env || process.argv.includes("--no-color")) &&
("FORCE_COLOR" in process.env ||
process.argv.includes("--color") ||
!("NO_COLOR" in env || argv.includes("--no-color")) &&
("FORCE_COLOR" in env ||
argv.includes("--color") ||
process.platform === "win32" ||
(tty.isatty(1) && process.env.TERM !== "dumb") ||
"CI" in process.env)
(require != null && require("tty").isatty(1) && env.TERM !== "dumb") ||
"CI" in env)

let formatter =
(open, close, replace = open) =>
Expand Down
8 changes: 7 additions & 1 deletion tests/environments.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ test("windows", () => {
assert.equal(pc.red("text"), pc.createColors(true).red("text"))
})

test("edge runtime", () => {
let pc = initModuleEnv({ env: { FORCE_COLOR: "1" }, argv: undefined, require: undefined })
assert.equal(pc.isColorSupported, true)
assert.equal(pc.red("text"), pc.createColors(true).red("text"))
})

function test(name, fn) {
try {
fn()
Expand All @@ -56,7 +62,7 @@ function test(name, fn) {
}
}

function initModuleEnv({ env, argv = [], platform = "darwin" }) {
function initModuleEnv({ env, argv = [], platform = "darwin", require = global.require }) {
let process = { env, argv, platform }
let context = vm.createContext({ require, process, module: { exports: {} } })
let script = new vm.Script(source)
Expand Down
Loading