Last active
January 4, 2020 21:07
-
-
Save nainemom/05ac33f602cc41ca98afd05b8ced5276 to your computer and use it in GitHub Desktop.
Simple CLI Tool for Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline'); | |
const process = require('process'); | |
module.exports.ask = (question) => new Promise((resolve) => { | |
const createdInterface = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
createdInterface.question(`${question} `, (answer) => { | |
createdInterface.close(); | |
resolve(answer); | |
}); | |
}); | |
module.exports.write = console.log; | |
module.exports.loading = (prefix = '') => { | |
const std = process.stdout; | |
const spinnerData = ['. ', '.. ', '...', ' ..', ' .', ' ', ' ']; | |
let index = 0; | |
process.stdout.write('\x1B[?25l'); | |
const length = spinnerData[0].length + prefix.length + 1; | |
const interval = setInterval(() => { | |
const str = `${prefix} ${spinnerData[index]}`; | |
std.write(str); | |
index = index + 1 >= spinnerData.length ? 0 : index + 1; | |
readline.moveCursor(std, -1 * length, 0); | |
}, 90); | |
return { | |
finish() { | |
clearInterval(interval); | |
readline.clearLine(std); | |
}, | |
}; | |
}; | |
module.exports.getArg = (key) => { | |
const args = process.argv.slice(2); | |
const index = args.indexOf(key); | |
if (index !== -1 && typeof args[index + 1] !== 'undefined') { | |
return args[index + 1]; | |
} | |
}; | |
module.exports.hasArg = (key) => { | |
const args = process.argv.slice(2); | |
const index = args.indexOf(key); | |
return index !== -1; | |
}; | |
module.exports.args = () => { | |
return process.argv.slice(2); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment