diff --git a/bin/cli.js b/bin/cli.js index 1ad4546..840d813 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -11,12 +11,14 @@ const argv = require('minimist')(process.argv.slice(2), { rename: { _: 'files' } }); -const moduleDir = dim(`<${path.dirname(__dirname)}>`); -const name = pkg => bold(`delete-empty v${pkg.version}`); +const color = (colorFn, text) => process.stdout.isTTY ? colorFn(text) : text; + +const moduleDir = color(dim, `<${path.dirname(__dirname)}>`); +const name = pkg => color(bold, `delete-empty v${pkg.version}`); const help = pkg => ` Path: <${path.dirname(__dirname)}> -Usage: ${cyan('$ delete-empty [options]')} +Usage: ${color(cyan, '$ delete-empty [options]')} Directory: (optional) Initial directory to begin the search for empty directories. Otherwise, cwd is used. @@ -34,7 +36,7 @@ if (argv.help) { process.exit(); } -const ok = green(symbols.check); +const ok = color(green, symbols.check); const cwd = path.resolve(argv._[0] || argv.cwd || process.cwd()); const relative = filepath => { if (filepath.startsWith(cwd)) { diff --git a/index.js b/index.js index edcd6f7..393fe19 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,8 @@ const startsWith = require('path-starts-with'); const colors = require('ansi-colors'); const readdir = util.promisify(fs.readdir); +const color = (colorFn, text) => process.stdout.isTTY ? colorFn(text) : text; + /** * Helpers */ @@ -80,7 +82,7 @@ const deleteEmpty = (cwd, options, cb) => { await deleteDir(dir, opts); if (opts.verbose === true) { - console.log(colors.red('Deleted:'), path.relative(cwd, dir)); + console.log(color(colors.red, 'Deleted:'), path.relative(cwd, dir)); } if (typeof opts.onDelete === 'function') { @@ -125,7 +127,7 @@ deleteEmpty.sync = (cwd, options) => { deleteDirSync(dir, opts); if (opts.verbose === true) { - console.log(colors.red('Deleted:'), path.relative(cwd, dir)); + console.log(color(colors.red, 'Deleted:'), path.relative(cwd, dir)); } if (typeof opts.onDelete === 'function') {