|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const obj = require('through2').obj; |
| 5 | +const pager = require('default-pager'); |
| 6 | +const msee = require('msee'); |
| 7 | +const join = require('path').join; |
| 8 | +const boxen = require('boxen'); |
| 9 | +const chalk = require('chalk'); |
| 10 | +const updateNotifier = require('update-notifier'); |
| 11 | +const pkg = require('./package.json'); |
| 12 | +const meow = require('meow'); |
| 13 | + |
| 14 | +const cli = meow([ |
| 15 | + 'Usage', |
| 16 | + ' bash-handbook', |
| 17 | + '', |
| 18 | + 'Options', |
| 19 | + ' --lang, -l Translation language', |
| 20 | + '', |
| 21 | + 'Examples', |
| 22 | + ' bash-handbook', |
| 23 | + ' bash-handbook --lang pt-br' |
| 24 | +], { |
| 25 | + string: [ |
| 26 | + 'lang' |
| 27 | + ], |
| 28 | + alias: { |
| 29 | + l: 'lang', |
| 30 | + h: 'help' |
| 31 | + }, |
| 32 | + default: { |
| 33 | + lang: '' |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +const boxenOpts = { |
| 38 | + borderColor: 'yellow', |
| 39 | + margin: { |
| 40 | + bottom: 1 |
| 41 | + }, |
| 42 | + padding: { |
| 43 | + right: 1, |
| 44 | + left: 1 |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +const mseeOpts = { |
| 49 | + paragraphEnd: '\n\n' |
| 50 | +}; |
| 51 | + |
| 52 | +const notifier = updateNotifier({ pkg }); |
| 53 | + |
| 54 | +process.env.PAGER = process.env.PAGER || 'less'; |
| 55 | +process.env.LESS = process.env.LESS || 'FRX'; |
| 56 | + |
| 57 | +const lang = cli.flags.lang |
| 58 | + .toLowerCase() |
| 59 | + .split('-') |
| 60 | + .map((l, i) => i === 0 ? l : l.toUpperCase()) |
| 61 | + .join('-'); |
| 62 | + |
| 63 | +const translation = join(__dirname, !lang ? './README.md' : `./README-${lang}.md`); |
| 64 | + |
| 65 | +fs.stat(translation, function (err, stats) { |
| 66 | + if (err) { |
| 67 | + console.log('The %s translation does not exist', chalk.bold(lang)); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + fs.createReadStream(translation) |
| 72 | + .pipe(obj(function (chunk, enc, cb) { |
| 73 | + const message = []; |
| 74 | + |
| 75 | + if (notifier.update) { |
| 76 | + message.push(`Update available: {green.bold ${notifier.update.latest}} {dim current: ${notifier.update.current}}`); |
| 77 | + message.push(`Run {blue npm install -g ${pkg.name}} to update.`); |
| 78 | + this.push(boxen(message.join('\n'), boxenOpts)); |
| 79 | + } |
| 80 | + |
| 81 | + this.push(msee.parse(chunk.toString(), mseeOpts)); |
| 82 | + cb(); |
| 83 | + })) |
| 84 | + .pipe(pager()); |
| 85 | +}); |
0 commit comments