Skip to content

Commit

Permalink
Add --color to force color in terminal. Fixes #804.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Jan 25, 2021
1 parent 25bdcfb commit 9e8c2d6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ ncu --format repo
## Options

```text
--color Force color in terminal
--concurrency <n> Max number of concurrent HTTP requests to
registry. (default: 8)
--configFileName <filename> Config file name (default: .ncurc.{json,yml,js})
Expand Down
4 changes: 4 additions & 0 deletions lib/cli-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ other version numbers that are higher. Includes prereleases.`])

// store CLI options separately from bin file so that they can be used to build type definitions
const cliOptions = [
{
long: 'color',
description: 'Force color in terminal',
},
{
long: 'concurrency',
arg: 'n',
Expand Down
6 changes: 5 additions & 1 deletion lib/doctor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')
const spawn = require('spawn-please')
const chalk = require('chalk')
let chalk = require('chalk')
const rimraf = require('rimraf')
const { upgradePackageData } = require('./versionmanager')
const { printUpgrades } = require('./logging')
Expand All @@ -9,6 +9,10 @@ const { npm: spawnNpm } = require('./package-managers/npm')
/** Run the npm CLI. */
const npm = (args, options, print) => {

if (options.color) {
chalk = new chalk.Instance({ level: 1 })
}

if (print) {
console.log(chalk.blue([options.packageManager, ...args].join(' ')))
}
Expand Down
5 changes: 5 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ declare namespace ncu {

interface RunOptions {

/**
* Force color in terminal
*/
color?: boolean;

/**
* Max number of concurrent HTTP requests to registry. (default: 8)
*/
Expand Down
25 changes: 22 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const cint = require('cint')
const findUp = require('find-up')
const _ = require('lodash')
const getstdin = require('get-stdin')
const chalk = require('chalk')
let chalk = require('chalk')
const { rcFile } = require('rc-config-loader')
const jph = require('json-parse-helpfulerror')
const vm = require('./versionmanager')
Expand All @@ -31,7 +31,6 @@ const packageFileNames = {

// time to wait for stdin before printing a warning
const stdinWarningTime = 5000
const stdinWarningMessage = `Hmmmmm... this is taking a long time. Your console is telling me to wait for input \non stdin, but maybe that is not what you want.\nTry ${chalk.cyan('winpty ncu.cmd')}, or specify a package file explicitly with ${chalk.cyan('--packageFile package.json')}. \nSee https://github.com/raineorshine/npm-check-updates/issues/136#issuecomment-155721102`

//
// Helper functions
Expand Down Expand Up @@ -64,6 +63,10 @@ const writePackageFile = promisify(fs.writeFile)

async function analyzeGlobalPackages(options) {

if (options.color) {
chalk = new chalk.Instance({ level: 1 })
}

print(options, 'Getting installed packages...', 'verbose')

const globalPackages = await vm.getInstalledPackages(
Expand Down Expand Up @@ -107,6 +110,10 @@ async function analyzeProjectDependencies(options, pkgData, pkgFile) {

let pkg

if (options.color) {
chalk = new chalk.Instance({ level: 1 })
}

try {
if (!pkgData) {
throw new Error('pkgData: ' + pkgData)
Expand Down Expand Up @@ -222,6 +229,10 @@ async function analyzeProjectDependencies(options, pkgData, pkgFile) {
/** Initializes and consolidates program options. */
function initOptions(options) {

if (options.color) {
chalk = new chalk.Instance({ level: 1 })
}

const json = Object.keys(options)
.filter(option => option.startsWith('json'))
.some(_.propertyOf(options))
Expand Down Expand Up @@ -298,6 +309,10 @@ async function findPackage(options) {
let pkgFile
let stdinTimer

if (options.color) {
chalk = new chalk.Instance({ level: 1 })
}

print(options, 'Running in local mode...', 'verbose')
print(options, 'Finding package file data...', 'verbose')

Expand Down Expand Up @@ -332,7 +347,7 @@ async function findPackage(options) {
// warn the user after a while if still waiting for stdin
// this is a way to mitigate #136 where Windows unexpectedly waits for stdin
stdinTimer = setTimeout(() => {
console.log(stdinWarningMessage)
console.log(`Hmmmmm... this is taking a long time. Your console is telling me to wait for input \non stdin, but maybe that is not what you want.\nTry ${chalk.cyan('winpty ncu.cmd')}, or specify a package file explicitly with ${chalk.cyan('--packageFile package.json')}. \nSee https://github.com/raineorshine/npm-check-updates/issues/136#issuecomment-155721102`)
}, stdinWarningTime)

// get data from stdin
Expand Down Expand Up @@ -363,6 +378,10 @@ process.on('unhandledRejection', err => {
/** main entry point */
async function run(options = {}) {

if (options.color) {
chalk = new chalk.Instance({ level: 1 })
}

// if not executed on the command-line (i.e. executed as a node module), set some defaults
if (!options.cli) {
const cliDefaults = cliOptions.reduce((acc, curr) => ({
Expand Down
7 changes: 6 additions & 1 deletion lib/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

const Table = require('cli-table')
const chalk = require('chalk')
let chalk = require('chalk')
const { colorizeDiff, isGithubUrl, getGithubUrlTag, isNpmAlias, parseNpmAlias } = require('./version-util')
const { getRepoUrl } = require('./repo-url')

Expand Down Expand Up @@ -104,6 +104,11 @@ function toDependencyTable({ from: fromDeps, to: toDeps, ownersChangedDeps, form
* @param args.ownersChangedDeps - Boolean flag per dependency which announces if package owner changed.
*/
function printUpgrades(options, { current, upgraded, numUpgraded, total, ownersChangedDeps }) {

if (options.color) {
chalk = new chalk.Instance({ level: 1 })
}

print(options, '')

// print everything is up-to-date
Expand Down

0 comments on commit 9e8c2d6

Please sign in to comment.