Skip to content

Commit

Permalink
Fix up the dry run output a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Sep 3, 2020
1 parent 0d75f42 commit 3c0e513
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/log.js
@@ -1,6 +1,6 @@
const { EOL } = require('os');
const chalk = require('chalk');
const { isObject, last, filter, isString, lowerCase, upperFirst } = require('lodash');
const { isObject, last, filter, isString, lowerCase, upperFirst, isArray } = require('lodash');

class Logger {
constructor({ isCI = true, isVerbose = false, verbosityLevel = 0, isDryRun = false } = {}) {
Expand All @@ -9,37 +9,50 @@ class Logger {
this.verbosityLevel = verbosityLevel;
this.isDryRun = isDryRun;
}

shouldLog(isExternal) {
return this.verbosityLevel === 2 || (this.isVerbose && isExternal);
}

log(...args) {
console.log(...args); // eslint-disable-line no-console
}

error(...args) {
console.error(chalk.red('ERROR'), ...args); // eslint-disable-line no-console
}

info(...args) {
this.log(chalk.grey(...args));
}

warn(...args) {
this.log(chalk.yellow('WARNING'), ...args);
}

verbose(...args) {
const { isExternal } = isObject(last(args)) ? last(args) : {};
if (this.verbosityLevel === 2 || (this.isVerbose && (isExternal || this.isDryRun))) {
if (this.shouldLog(isExternal)) {
this.log(...filter(args, isString));
}
}

exec(...args) {
const { isDryRun: isExecutedInDryRun, isExternal, isCached } = isObject(last(args)) ? last(args) : {};
if (this.verbosityLevel === 2 || this.isDryRun || (this.isVerbose && isExternal)) {
if (this.shouldLog(isExternal) || this.isDryRun) {
const prefix = isExecutedInDryRun == null ? '$' : '!';
const message = [prefix, ...filter(args, isString), isCached ? '[cached]' : ''].join(' ').trim();
const command = args.map(cmd => (isString(cmd) ? cmd : isArray(cmd) ? cmd.join(' ') : '')).join(' ');
const message = [prefix, command, isCached ? '[cached]' : ''].join(' ').trim();
this.log(message);
}
}

obtrusive(...args) {
if (!this.isCI) this.log();
this.log(...args);
if (!this.isCI) this.log();
}

preview({ title, text }) {
if (text) {
const header = chalk.bold(upperFirst(title));
Expand Down
3 changes: 3 additions & 0 deletions lib/shell.js
Expand Up @@ -59,6 +59,9 @@ class Shell {
this.log.verbose(stdout, { isExternal });
debug({ command, options, code, stdout, stderr });
if (code === 0) {
if (stderr) {
this.log.verbose(stderr.toString().trim(), { isExternal });
}
resolve(stdout);
} else {
if (stdout && stderr) {
Expand Down

0 comments on commit 3c0e513

Please sign in to comment.