Skip to content

Commit

Permalink
Update p-queue and ansi-styles
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Jul 4, 2023
1 parent 2c81236 commit 10b075c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
8 changes: 1 addition & 7 deletions lib/create-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const ansiStyles = require('ansi-styles')

// ------------------------------------------------------------------------------
// Public Interface
// ------------------------------------------------------------------------------
Expand All @@ -26,7 +20,7 @@ const ansiStyles = require('ansi-styles')
* @param {boolean} isTTY - The flag to color the header.
* @returns {string} The header of a given task.
*/
module.exports = function createHeader (nameAndArgs, packageInfo, isTTY) {
module.exports = function createHeader (nameAndArgs, packageInfo, isTTY, ansiStyles) {
if (!packageInfo) {
return `\n> ${nameAndArgs}\n\n`
}
Expand Down
16 changes: 9 additions & 7 deletions lib/run-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
// ------------------------------------------------------------------------------

const path = require('path')
const ansiStyles = require('ansi-styles')
const parseArgs = require('shell-quote').parse
const createHeader = require('./create-header')
const createPrefixTransform = require('./create-prefix-transform-stream')
const spawn = require('./spawn')
const ansiStylesPromise = import('ansi-styles')

// ------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -50,7 +50,7 @@ function selectColor (taskName) {
* @param {object} labelState - An label state for the transform stream.
* @returns {stream.Writable} `source` or the created wrapped stream.
*/
function wrapLabeling (taskName, source, labelState) {
function wrapLabeling (taskName, source, labelState, ansiStyles) {
if (source == null || !labelState.enabled) {
return source
}
Expand Down Expand Up @@ -133,12 +133,13 @@ function cleanTaskArg (arg) {
* This promise object has an extra method: `abort()`.
* @private
*/
module.exports = function runTask (task, options) {
module.exports = async function runTask (task, options) {
let cp = null
const ansiStyles = (await ansiStylesPromise).default
const promise = new Promise((resolve, reject) => {
const stdin = options.stdin
const stdout = wrapLabeling(task, options.stdout, options.labelState)
const stderr = wrapLabeling(task, options.stderr, options.labelState)
const stdout = wrapLabeling(task, options.stdout, options.labelState, ansiStyles)
const stderr = wrapLabeling(task, options.stderr, options.labelState, ansiStyles)
const stdinKind = detectStreamKind(stdin, process.stdin)
const stdoutKind = detectStreamKind(stdout, process.stdout)
const stderrKind = detectStreamKind(stderr, process.stderr)
Expand All @@ -149,7 +150,8 @@ module.exports = function runTask (task, options) {
stdout.write(createHeader(
task,
options.packageInfo,
options.stdout.isTTY
options.stdout.isTTY,
ansiStyles
))
}

Expand Down Expand Up @@ -203,5 +205,5 @@ module.exports = function runTask (task, options) {
}
}

return promise
return await promise
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"postversion": "git push --follow-tags && gh-release -y"
},
"dependencies": {
"ansi-styles": "^5.0.0",
"ansi-styles": "^6.2.1",
"cross-spawn": "^7.0.3",
"memorystream": "^0.3.1",
"minimatch": "^9.0.0",
Expand All @@ -46,7 +46,7 @@
"gh-release": "^7.0.0",
"jsdoc": "^4.0.0",
"mocha": "^10.0.0",
"p-queue": "^6.6.1",
"p-queue": "^7.3.4",
"yarn": "^1.12.3"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion test/bin/run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const spawn = require('child_process').spawn
const path = require('path')
const os = require('os')
const fs = require('fs-extra')
const PQueue = require('p-queue')

// ------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -136,6 +135,7 @@ async function runMochaWithWorkspace (filePath) {

(async () => {
const startInSec = process.uptime()
const { default: PQueue } = await import('p-queue')
const queue = new PQueue({ concurrency: os.cpus().length + 1 })
const results = await Promise.all(
(await fs.readdir(ROOT_PATH))
Expand Down
22 changes: 15 additions & 7 deletions test/print-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const createHeader = require('../lib/create-header')
const readPackageJson = require('../lib/read-package-json')
const BufferStream = require('./lib/buffer-stream')
const util = require('./lib/util')
const ansiStylesPromise = import('ansi-styles')
const runAll = util.runAll
const runPar = util.runPar
const runSeq = util.runSeq
Expand All @@ -37,51 +38,58 @@ describe('[print-name] npm-run-all', () => {

describe('should print names before running tasks:', () => {
it('Node API', async () => {
const ansiStyles = (await ansiStylesPromise).default
const stdout = new BufferStream()
await nodeApi('test-task:echo abc', { stdout, silent: true, printName: true })
const header = createHeader('test-task:echo abc', packageInfo, false)
const header = createHeader('test-task:echo abc', packageInfo, false, ansiStyles)
assert.equal(stdout.value.slice(0, header.length), header)
})

it('npm-run-all command (--print-name)', async () => {
const ansiStyles = (await ansiStylesPromise).default
const stdout = new BufferStream()
await runAll(['test-task:echo abc', '--silent', '--print-name'], stdout)
const header = createHeader('test-task:echo abc', packageInfo, false)
const header = createHeader('test-task:echo abc', packageInfo, false, ansiStyles)
assert.equal(stdout.value.slice(0, header.length), header)
})

it('run-s command (--print-name)', async () => {
const ansiStyles = (await ansiStylesPromise).default
const stdout = new BufferStream()
await runSeq(['test-task:echo abc', '--silent', '--print-name'], stdout)
const header = createHeader('test-task:echo abc', packageInfo, false)
const header = createHeader('test-task:echo abc', packageInfo, false, ansiStyles)
assert.equal(stdout.value.slice(0, header.length), header)
})

it('run-p command (--print-name)', async () => {
const ansiStyles = (await ansiStylesPromise).default
const stdout = new BufferStream()
await runPar(['test-task:echo abc', '--silent', '--print-name'], stdout)
const header = createHeader('test-task:echo abc', packageInfo, false)
const header = createHeader('test-task:echo abc', packageInfo, false, ansiStyles)
assert.equal(stdout.value.slice(0, header.length), header)
})

it('npm-run-all command (-n)', async () => {
const ansiStyles = (await ansiStylesPromise).default
const stdout = new BufferStream()
await runAll(['test-task:echo abc', '--silent', '-n'], stdout)
const header = createHeader('test-task:echo abc', packageInfo, false)
const header = createHeader('test-task:echo abc', packageInfo, false, ansiStyles)
assert.equal(stdout.value.slice(0, header.length), header)
})

it('run-s command (-n)', async () => {
const ansiStyles = (await ansiStylesPromise).default
const stdout = new BufferStream()
await runSeq(['test-task:echo abc', '--silent', '-n'], stdout)
const header = createHeader('test-task:echo abc', packageInfo, false)
const header = createHeader('test-task:echo abc', packageInfo, false, ansiStyles)
assert.equal(stdout.value.slice(0, header.length), header)
})

it('run-p command (-n)', async () => {
const ansiStyles = (await ansiStylesPromise).default
const stdout = new BufferStream()
await runPar(['test-task:echo abc', '--silent', '-n'], stdout)
const header = createHeader('test-task:echo abc', packageInfo, false)
const header = createHeader('test-task:echo abc', packageInfo, false, ansiStyles)
assert.equal(stdout.value.slice(0, header.length), header)
})
})
Expand Down

0 comments on commit 10b075c

Please sign in to comment.