Skip to content

Commit

Permalink
Fix color detection in edge runtime (#56)
Browse files Browse the repository at this point in the history
* fix color detection in edge runtime

* add env test for the edge

* use node lts for benchmarks
  • Loading branch information
alexeyraspopov authored Aug 16, 2023
1 parent 6b43e8e commit b626148
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
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 =
!("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

0 comments on commit b626148

Please sign in to comment.