From b5767c63de2810c269f69af286d0fa94fa4701f6 Mon Sep 17 00:00:00 2001 From: Dario Vladovic Date: Wed, 30 Jun 2021 22:57:44 +0200 Subject: [PATCH 1/4] Revert "Fix missing render path error (#354)" This reverts commit f1a4854d8195350b5fe57d7f9465f5eaefefbfa3. --- lib/errors.js | 14 ++------------ lib/tldr.js | 5 +---- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/lib/errors.js b/lib/errors.js index 882c4e5..617f1e8 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -29,26 +29,16 @@ class MissingPageError extends TldrError { Page not found. If you want to contribute it, feel free to send a pull request to: ${repo} `); - this.name = 'MissingPageError'; + this.name = 'MissingPageErrror'; // eslint-disable-next-line no-magic-numbers this.code = 3; } } -class MissingRenderPathError extends TldrError { - constructor() { - super('Option --render needs an argument.'); - this.name = 'MissingRenderPathError'; - // eslint-disable-next-line no-magic-numbers - this.code = 4; - } -} - module.exports = { TldrError, EmptyCacheError, - MissingPageError, - MissingRenderPathError + MissingPageError }; function trim(strings, ...values) { diff --git a/lib/tldr.js b/lib/tldr.js index 823e0fc..3c0ffcb 100644 --- a/lib/tldr.js +++ b/lib/tldr.js @@ -4,7 +4,7 @@ const sample = require('lodash/sample'); const fs = require('fs-extra'); const ms = require('ms'); const ora = require('ora'); -const { EmptyCacheError, MissingPageError, MissingRenderPathError } = require('./errors'); +const { EmptyCacheError, MissingPageError } = require('./errors'); const Cache = require('./cache'); const search = require('./search'); const platform = require('./platform'); @@ -73,9 +73,6 @@ class Tldr { } render(file) { - if (typeof file !== 'string') { - throw new MissingRenderPathError(); - } return fs.readFile(file, 'utf8') .then((content) => { // Getting the shortindex first to populate the shortindex var From 5546a4971706f2b2375a8a2871c82889b03551dd Mon Sep 17 00:00:00 2001 From: Dario Vladovic Date: Wed, 30 Jun 2021 23:00:42 +0200 Subject: [PATCH 2/4] fix(cli): require argument parameters --- bin/tldr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/tldr b/bin/tldr index f0f5c74..ba9f14c 100755 --- a/bin/tldr +++ b/bin/tldr @@ -20,15 +20,15 @@ program .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('-f, --render ', 'Render a specific markdown [file]') .option('-m, --markdown', 'Output in markdown format') - .option('-o, --os [type]', 'Override the operating system [linux, osx, sunos, windows]') + .option('-o, --os ', 'Override the operating system [linux, osx, sunos, windows]') .option('--linux', 'Override the operating system with Linux') .option('--osx', 'Override the operating system with OSX') .option('--sunos', 'Override the operating system with SunOS') .option('--windows', 'Override the operating system with Windows') - .option('-t, --theme [theme]', 'Color theme (simple, base16, ocean)') - .option('-s, --search [keywords]', 'Search pages using keywords') + .option('-t, --theme ', 'Color theme (simple, base16, ocean)') + .option('-s, --search ', 'Search pages using keywords') // // CACHE MANAGEMENT // From afce1318f6ec20bbde63a9f888aefcc9875591e5 Mon Sep 17 00:00:00 2001 From: Dario Vladovic Date: Wed, 30 Jun 2021 23:32:17 +0200 Subject: [PATCH 3/4] feat(cli): limit `-o, --os` option choices Upgrade `commander` to v8 to bring in support for `.choices()`. --- bin/tldr | 64 +++++++++++++++++++++++++---------------------- package-lock.json | 6 ++--- package.json | 2 +- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/bin/tldr b/bin/tldr index ba9f14c..d85507c 100755 --- a/bin/tldr +++ b/bin/tldr @@ -7,6 +7,8 @@ const config = require('../lib/config'); const platform = require('../lib/platform'); const { TldrError } = require('../lib/errors'); +const { Option } = program; + program .version(pkg.version, '-v, --version', 'Display version') .helpOption('-h, --help', 'Show this help message') @@ -22,7 +24,10 @@ program .option('-e, --random-example', 'Show a random example') .option('-f, --render ', 'Render a specific markdown [file]') .option('-m, --markdown', 'Output in markdown format') - .option('-o, --os ', 'Override the operating system [linux, osx, sunos, windows]') + .addOption( + new Option('-o, --os ', 'Override the operating system [linux, osx, sunos, windows]') + .choices(['linux', 'osx', 'sunos', 'windows']) + ) .option('--linux', 'Override the operating system with Linux') .option('--osx', 'Override the operating system with OSX') .option('--sunos', 'Override the operating system with SunOS') @@ -60,60 +65,59 @@ program.on('--help', () => { console.log(help); }); -program.parse(process.argv); +const options = program.parse(process.argv).opts(); -if (program.linux) { - program.os = 'linux'; +if (options.linux) { + options.os = 'linux'; } -if (program.osx) { - program.os = 'osx'; +if (options.osx) { + options.os = 'osx'; } -if (program.sunos) { - program.os = 'sunos'; +if (options.sunos) { + options.os = 'sunos'; } -if (program.windows) { - program.os = 'windows'; +if (options.windows) { + options.os = 'windows'; } let cfg = config.get(); -if (program.os) { - if (platform.isSupported(program.os)) { - cfg.platform = program.os; +if (options.os) { + if (platform.isSupported(options.os)) { + cfg.platform = options.os; } } -if (program.theme) { - cfg.theme = program.theme; +if (options.theme) { + cfg.theme = options.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) { +if (options.list) { + p = tldr.list(options.singleColumn); +} else if (options.listAll) { + p = tldr.listAll(options.singleColumn); +} else if (options.random) { + p = tldr.random(options); +} else if (options.randomExample) { p = tldr.randomExample(); -} else if (program.clearCache) { +} else if (options.clearCache) { p = tldr.clearCache(); -} else if (program.update) { +} else if (options.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 (options.render) { + p = tldr.render(options.render); +} else if (options.search) { + p = tldr.search([ options.search, ...program.args ]); } else if (program.args.length >= 1) { - p = tldr.get(program.args, program); + p = tldr.get(program.args, options); } if (p === null) { diff --git a/package-lock.json b/package-lock.json index 4d736c1..7dd9d20 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1499,9 +1499,9 @@ "dev": true }, "commander": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-6.1.0.tgz", - "integrity": "sha512-wl7PNrYWd2y5mp1OK/LhTlv8Ff4kQJQRXXAvF+uU/TPNiVJUxZLRYGj/B0y/lPGAVcSbJqH2Za/cvHmrPMC8mA==" + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-8.0.0.tgz", + "integrity": "sha512-Xvf85aAtu6v22+E5hfVoLHqyul/jyxh91zvqk/ioJTQuJR7Z78n7H558vMPKanPSRgIEeZemT92I2g9Y8LPbSQ==" }, "commondir": { "version": "1.0.1", diff --git a/package.json b/package.json index df87350..dcdeb9e 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "dependencies": { "axios": "^0.21.1", "chalk": "^4.1.0", - "commander": "^6.1.0", + "commander": "^8.0.0", "fs-extra": "^9.0.1", "glob": "^7.1.6", "lodash": "^4.17.20", From 108219d77bb80055327c3b438b307ca9ed561a5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Vladovi=C4=87?= Date: Sat, 3 Jul 2021 08:08:39 +0200 Subject: [PATCH 4/4] fix(cli): missing page error message typo Co-authored-by: Owen Voke --- lib/errors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/errors.js b/lib/errors.js index 617f1e8..fc1c511 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -29,7 +29,7 @@ class MissingPageError extends TldrError { Page not found. If you want to contribute it, feel free to send a pull request to: ${repo} `); - this.name = 'MissingPageErrror'; + this.name = 'MissingPageError'; // eslint-disable-next-line no-magic-numbers this.code = 3; }