#!/usr/bin/env node const program = require('commander'); const pkg = require('../package'); const Tldr = require('../lib/tldr'); const config = require('../lib/config'); const platforms = require('../lib/platforms'); const { TldrError } = require('../lib/errors'); pkg.version = `v${pkg.version}\nClient Specification: 2.0`; program .version(pkg.version, '-v, --version', 'Display version') .helpOption('-h, --help', 'Show this help message') .description(pkg.description) .usage('command [options]') // // BASIC OPTIONS // .option('-l, --list', 'List all commands for the chosen platform in the cache') .option('-a, --list-all', 'List all commands in the cache') .option('-1, --single-column', 'List single command per line (use with options -l or -a)') .option('-r, --random', 'Show a random command') .option('-e, --random-example', 'Show a random example') .option('-f, --render [file]', 'Render a specific markdown [file]') .option('-m, --markdown', 'Output in markdown format') .option('-p, --platform [type]', `Override the current platform [${platforms.supportedPlatforms.join(', ')}]`); for (const platform of platforms.supportedPlatforms) { program.option(`--${platform}`, `Override the platform with ${platform}`); } program .option('-t, --theme [theme]', 'Color theme (simple, base16, ocean)') .option('-s, --search [keywords]', 'Search pages using keywords') // // CACHE MANAGEMENT // .option('-u, --update', 'Update the local cache') .option('-c, --clear-cache', 'Clear the local cache'); const help = ` Examples: $ tldr tar $ tldr du --platform=linux $ tldr --search "create symbolic link to file" $ tldr --list $ tldr --list-all $ tldr --random $ tldr --random-example To control the cache: $ tldr --update $ tldr --clear-cache To render a local file (for testing): $ tldr --render /path/to/file.md `; program.on('--help', () => { console.log(help); }); program.parse(process.argv); for (const platform of platforms.supportedPlatforms) { if (program[platform]) { program.platform = platform; } } let cfg = config.get(); if (program.platform && platforms.isSupported(program.platform)) { cfg.platform = program.platform; } if (program.theme) { cfg.theme = program.theme; } const tldr = new Tldr(cfg); let p = null; if (program.list) { p = tldr.list(program.singleColumn); } else if (program.listAll) { p = tldr.listAll(program.singleColumn); } else if (program.random) { p = tldr.random(program); } else if (program.randomExample) { p = tldr.randomExample(); } else if (program.clearCache) { p = tldr.clearCache(); } else if (program.update) { p = tldr.updateCache() .then(() => { return tldr.updateIndex(); }); } else if (program.render) { p = tldr.render(program.render); } else if (program.search) { program.args.unshift(program.search); p = tldr.search(program.args); } else if (program.args.length >= 1) { p = tldr.get(program.args, program); } if (p === null) { program.outputHelp(); process.exitCode = 1; } else { p.catch((err) => { let output = TldrError.isTldrError(err) ? err.message : err.stack; console.error(output); process.exitCode = err.code || 1; }); }