mirror of
https://github.com/satwikkansal/wtfpython.git
synced 2024-09-11 12:06:01 +03:00
Add CLI
This commit is contained in:
parent
bfd82bee4b
commit
26c78f51fb
85
wtfpython
Executable file
85
wtfpython
Executable file
@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs');
|
||||
const obj = require('through2').obj;
|
||||
const pager = require('default-pager');
|
||||
const msee = require('msee');
|
||||
const join = require('path').join;
|
||||
const boxen = require('boxen');
|
||||
const chalk = require('chalk');
|
||||
const updateNotifier = require('update-notifier');
|
||||
const pkg = require('./package.json');
|
||||
const meow = require('meow');
|
||||
|
||||
const cli = meow([
|
||||
'Usage',
|
||||
' bash-handbook',
|
||||
'',
|
||||
'Options',
|
||||
' --lang, -l Translation language',
|
||||
'',
|
||||
'Examples',
|
||||
' bash-handbook',
|
||||
' bash-handbook --lang pt-br'
|
||||
], {
|
||||
string: [
|
||||
'lang'
|
||||
],
|
||||
alias: {
|
||||
l: 'lang',
|
||||
h: 'help'
|
||||
},
|
||||
default: {
|
||||
lang: ''
|
||||
}
|
||||
});
|
||||
|
||||
const boxenOpts = {
|
||||
borderColor: 'yellow',
|
||||
margin: {
|
||||
bottom: 1
|
||||
},
|
||||
padding: {
|
||||
right: 1,
|
||||
left: 1
|
||||
}
|
||||
};
|
||||
|
||||
const mseeOpts = {
|
||||
paragraphEnd: '\n\n'
|
||||
};
|
||||
|
||||
const notifier = updateNotifier({ pkg });
|
||||
|
||||
process.env.PAGER = process.env.PAGER || 'less';
|
||||
process.env.LESS = process.env.LESS || 'FRX';
|
||||
|
||||
const lang = cli.flags.lang
|
||||
.toLowerCase()
|
||||
.split('-')
|
||||
.map((l, i) => i === 0 ? l : l.toUpperCase())
|
||||
.join('-');
|
||||
|
||||
const translation = join(__dirname, !lang ? './README.md' : `./README-${lang}.md`);
|
||||
|
||||
fs.stat(translation, function (err, stats) {
|
||||
if (err) {
|
||||
console.log('The %s translation does not exist', chalk.bold(lang));
|
||||
return;
|
||||
}
|
||||
|
||||
fs.createReadStream(translation)
|
||||
.pipe(obj(function (chunk, enc, cb) {
|
||||
const message = [];
|
||||
|
||||
if (notifier.update) {
|
||||
message.push(`Update available: {green.bold ${notifier.update.latest}} {dim current: ${notifier.update.current}}`);
|
||||
message.push(`Run {blue npm install -g ${pkg.name}} to update.`);
|
||||
this.push(boxen(message.join('\n'), boxenOpts));
|
||||
}
|
||||
|
||||
this.push(msee.parse(chunk.toString(), mseeOpts));
|
||||
cb();
|
||||
}))
|
||||
.pipe(pager());
|
||||
});
|
Loading…
Reference in New Issue
Block a user