Skip to content

Commit

Permalink
fix: tips display crash in non-tty envs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Apr 25, 2023
1 parent e851624 commit afd121c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/cli/cmds/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const logger = require('../logger');
const checkUpdates = require('../checkUpdates');
const {outputFilenames, loadManifest} = require('../../files');
const handleCrash = require('../handleCrash');
const tip = require('../tips');

exports.command = ['audit', '*'];
exports.desc = "Security & License Compliance For Your App's Dependencies 🪱";
Expand Down Expand Up @@ -187,10 +188,7 @@ exports.handler = async (argv) => {
!(typeof fileConfig.skipCsv !== 'undefined' ? !!fileConfig.skipCsv : argv.skipCsv) &&
'csv',
].filter((o) => o),
onProgress: onProgress({
ora,
showTips: typeof fileConfig.showTips !== 'undefined' ? fileConfig.showTips : argv.showTips,
}),
onProgress: onProgress({ora}),
});

// ********************
Expand Down Expand Up @@ -318,6 +316,8 @@ exports.handler = async (argv) => {
`🔔 ${logger.colors.BG_CYAN}${logger.colors.BLACK}%s${logger.colors.RESET}\n`,
'New version available! Run "npm i -g @sandworm/audit" to update.',
);
} else if (typeof fileConfig.showTips !== 'undefined' ? fileConfig.showTips : argv.showTips) {
logger.log(tip());
}

process.exit();
Expand Down
19 changes: 1 addition & 18 deletions src/cli/progress.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const logger = require('./logger');
const tip = require('./tips');

const INITIAL_TIP_DELAY = 2000;

let currentSpinner;
let tipTimeout;
let tipDisplayed = false;

const getStartMessage = (stage) => {
switch (stage) {
Expand Down Expand Up @@ -50,23 +46,10 @@ const getEndMessage = (stage) => {
};

const onProgress =
({ora, showTips = true}) =>
({ora}) =>
({type, stage, message, progress}) => {
switch (type) {
case 'start':
if (showTips) {
if (stage === 'graph') {
tipTimeout = setTimeout(() => {
logger.log(tip());
tipDisplayed = true;
// Move up to continue counting graph packages
process.stdout.moveCursor(0, -6);
}, INITIAL_TIP_DELAY);
} else if (stage === 'vulnerabilities' && tipDisplayed) {
// Move down to skip over tips
process.stdout.moveCursor(0, 5);
}
}
currentSpinner = ora().start(getStartMessage(stage));
break;
case 'end':
Expand Down
10 changes: 3 additions & 7 deletions src/cli/tips.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@ const tip = () => {

const currentTip = `${logger.colors.DIM}\n//\n// ${logger.colors.RESET}💡 ${
logger.colors.DIM
}${message}${
example
? `\n// ${logger.colors.RESET}${logger.colors.BG_GRAY}${example}${logger.colors.RESET}${logger.colors.DIM}`
: ''
}${url ? `\n// ${logger.colors.UNDERLINE}${url}${logger.colors.RESET}` : ''}\n${
logger.colors.DIM
}//${logger.colors.RESET}`;
}${message}${example ? `\n// ${example}` : ''}${
url ? `\n// ${logger.colors.UNDERLINE}${url}${logger.colors.RESET}` : ''
}\n${logger.colors.DIM}//${logger.colors.RESET}\n`;

return currentTip;
};
Expand Down

0 comments on commit afd121c

Please sign in to comment.