From 26c78f51fb21f3e467a0005205e60c35690f8079 Mon Sep 17 00:00:00 2001 From: Satwik Kansal Date: Thu, 31 Aug 2017 02:44:01 +0530 Subject: [PATCH] Add CLI --- wtfpython | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 wtfpython diff --git a/wtfpython b/wtfpython new file mode 100755 index 0000000..b8a3fa6 --- /dev/null +++ b/wtfpython @@ -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()); +});