Skip to content

Commit

Permalink
feat: change/simplify hasColors implementation
Browse files Browse the repository at this point in the history
It is now a constant, not a function. Dependency-free detection.

Credit to sindresorhus/yoctocolors#5
  • Loading branch information
kirillgroshkov committed Oct 31, 2021
1 parent 760fab6 commit 13f32d1
Show file tree
Hide file tree
Showing 5 changed files with 621 additions and 637 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"move-file": "^2.0.0",
"nanoid": "^3.0.0",
"sanitize-html": "^2.5.2",
"supports-color": "^8.0.0",
"through2-concurrent": "^2.0.0",
"yargs": "^17.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/colors/colors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ test('colors', () => {
})

test('hasColors', () => {
console.log(hasColors())
console.log(hasColors)
})
12 changes: 7 additions & 5 deletions src/colors/colors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const supporsColorLib = require('supports-color')
import * as tty from 'tty'

export function hasColors(): boolean {
if (process.env['NO_COLOR']) return false // https://no-color.org/
return !!supporsColorLib.stdout
}
/**
* Based on: https://github.com/sindresorhus/yoctocolors/pull/5
*
* @experimental
*/
export const hasColors = !process.env['NO_COLOR'] && tty.WriteStream.prototype.hasColors()
10 changes: 5 additions & 5 deletions src/stream/transform/transformLogProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export interface TransformLogProgressOptions<IN = any> extends TransformOptions
}

const inspectOpt: InspectOptions = {
colors: hasColors(),
colors: hasColors,
breakLength: 300,
}

Expand Down Expand Up @@ -163,16 +163,16 @@ export function transformLogProgress<IN = any>(
const mem = process.memoryUsage()

const now = Date.now()
const lastRPS = (processedLastSecond * batchSize) / ((now - lastSecondStarted) / 1000) || 0
const rpsTotal = Math.round((progress * batchSize) / ((now - started) / 1000)) || 0
const batchedProgress = progress * batchSize
const lastRPS =
(processedLastSecond * batchedProgress) / ((now - lastSecondStarted) / 1000) || 0
const rpsTotal = Math.round(batchedProgress / ((now - started) / 1000)) || 0
lastSecondStarted = now
processedLastSecond = 0

const rps10 = Math.round(sma.push(lastRPS))
if (mem.rss > peakRSS) peakRSS = mem.rss

const batchedProgress = progress * batchSize

console.log(
inspect(
{
Expand Down
Loading

0 comments on commit 13f32d1

Please sign in to comment.