Skip to content

Commit

Permalink
Created Log Indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchPierias committed Apr 22, 2019
1 parent 35040e0 commit 32e1032
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
100 changes: 100 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@types/mocha": "5.2.6",
"@types/node": "^11.13.4",
"@types/node-fetch": "^2.3.2",
"@types/ora": "^3.2.0",
"@types/rimraf": "2.0.2",
"@types/text-encoding": "0.0.35",
"eosjs": "20.0.0",
Expand All @@ -54,13 +55,15 @@
"dependencies": {
"axios": "0.18.0",
"chai": "^4.2.0",
"colors": "^1.3.3",
"commander": "2.20.0",
"deep-equal-in-any-order": "^1.0.13",
"docker-cli-js": "2.5.2",
"glob": "7.1.3",
"mkdirp": "0.5.1",
"mocha": "6.1.3",
"node-fetch": "2.3.0",
"ora": "^3.4.0",
"qrcode-terminal": "0.12.0",
"rimraf": "2.6.3",
"ts-mocha": "6.0.0"
Expand Down
66 changes: 66 additions & 0 deletions src/cli/logIndicator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import ora, { Ora } from 'ora';
import * as colors from 'colors';

/** Holds spinner instances */
const cache:{ spinner?:Ora } = {};

/** Alters output based on the process being piped */
const isTTY:boolean = process.env.CI ? false : process.stdout.isTTY!;

/**
* Creates a new spinner instance with the specified message
* @author Mitch Pierias <github.com/MitchPierias>
* @param text Output display message
*/
export const create = (text:string) => {
// Handle process piping
if (!isTTY) {
console.log(`lamington - ${text}`);
return;
}
// Cleanup existing spinner
if (cache.spinner) {
cache.spinner.succeed();
delete cache.spinner;
}
// Create and cache spinner
cache.spinner = ora({
text,
color: 'magenta'
}).start();
}

/**
* Terminates the current spinner with the specified output message
* @author Mitch Pierias <github.com/MitchPierias>
* @param message Output message
* @param isError Renders output as error toggle
*/
export const end = (message?:string, isError:boolean = false) => {
// Handle process piping
if (!isTTY) {
console.log(`create-react-app - ${message}`);
return;
}
// Handle existing spinner
if (cache.spinner) {
(isError ? cache.spinner.fail() : cache.spinner.succeed());
delete cache.spinner;
}
// Output closure message
if (!message || message == '') return;
const prefix = isError ? colors.red('ERROR:') : colors.green('DONE!');
console.log(`
${prefix} ${message}
`);
}

/**
* Terminates the current spinner as an error with output message
* @author Mitch Pierias <github.com/MitchPierias>
* @param message Spinner message.
*/
export const fail = (message:string) => {
end(message, true);
process.exit(1);
}

0 comments on commit 32e1032

Please sign in to comment.