diff --git a/README.md b/README.md index 2331fca3..5901e46d 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ ncu --format repo ## Options ```text +--color Force color in terminal --concurrency Max number of concurrent HTTP requests to registry. (default: 8) --configFileName Config file name (default: .ncurc.{json,yml,js}) diff --git a/lib/cli-options.js b/lib/cli-options.js index 97eb510b..729215b6 100644 --- a/lib/cli-options.js +++ b/lib/cli-options.js @@ -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', diff --git a/lib/doctor.js b/lib/doctor.js index 50d3733f..fea6fd8b 100644 --- a/lib/doctor.js +++ b/lib/doctor.js @@ -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') @@ -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(' '))) } diff --git a/lib/index.d.ts b/lib/index.d.ts index e2795699..7a60e2d2 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -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) */ diff --git a/lib/index.js b/lib/index.js index b989ba59..2f9347fe 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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') @@ -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 @@ -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( @@ -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) @@ -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)) @@ -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') @@ -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 @@ -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) => ({ diff --git a/lib/logging.js b/lib/logging.js index 22d64c2d..1f68b6ca 100644 --- a/lib/logging.js +++ b/lib/logging.js @@ -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') @@ -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