From 0d24f1943e0069f260f05f89c16c6e776e3be14b Mon Sep 17 00:00:00 2001 From: Osman Altun <17157809+OsmanAltun@users.noreply.github.com> Date: Wed, 9 Dec 2020 15:34:36 +0100 Subject: [PATCH 1/5] Add showVersion function --- lib/usage.ts | 10 ++++++---- lib/yargs-factory.ts | 11 ++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/usage.ts b/lib/usage.ts index c5da825d8..98d135a66 100644 --- a/lib/usage.ts +++ b/lib/usage.ts @@ -656,9 +656,11 @@ export function usage(yargs: YargsInstance, y18n: Y18N, shim: PlatformShim) { version = ver; }; - self.showVersion = () => { + self.showVersion = level => { const logger = yargs._getLoggerInstance(); - logger.log(version); + if (!level) level = 'error'; + const emit = typeof level === 'function' ? level : logger[level]; + emit(version); }; self.reset = function reset(localLookup) { @@ -732,9 +734,9 @@ export interface UsageInstance { getUsageDisabled(): boolean; help(): string; reset(localLookup: Dictionary): UsageInstance; - showHelp(level: 'error' | 'log' | ((message: string) => void)): void; + showHelp(level?: 'error' | 'log' | ((message: string) => void)): void; showHelpOnFail(enabled?: boolean | string, message?: string): UsageInstance; - showVersion(): void; + showVersion(level?: 'error' | 'log' | ((message: string) => void)): void; stringifiedValues(values?: any[], separator?: string): string; unfreeze(): void; usage(msg: string | null, description?: string | false): UsageInstance; diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts index 5622dd628..b25222aab 100644 --- a/lib/yargs-factory.ts +++ b/lib/yargs-factory.ts @@ -1238,6 +1238,12 @@ function Yargs( return self; }; + self.showVersion = function (level) { + argsert('[string|function]', [level], arguments.length); + usage.showVersion(level); + return self; + }; + let versionOpt: string | null = null; self.version = function version( opt?: string | false, @@ -1594,7 +1600,7 @@ function Yargs( if (exitProcess) setBlocking(true); skipValidation = true; - usage.showVersion(); + usage.showVersion('log'); self.exit(0); } }); @@ -1958,6 +1964,9 @@ export interface YargsInstance { scriptName(scriptName: string): YargsInstance; showCompletionScript($0?: string, cmd?: string): YargsInstance; showHelp(level: 'error' | 'log' | ((message: string) => void)): YargsInstance; + showVersion( + level: 'error' | 'log' | ((message: string) => void) + ): YargsInstance; showHelpOnFail: { (message?: string): YargsInstance; (enabled: boolean, message: string): YargsInstance; From c375d2aad3bccdec8d6fd0268d71e03ed36fef47 Mon Sep 17 00:00:00 2001 From: Osman Altun <17157809+OsmanAltun@users.noreply.github.com> Date: Wed, 9 Dec 2020 17:57:31 +0100 Subject: [PATCH 2/5] Add tests related to showVersion function --- test/usage.cjs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/test/usage.cjs b/test/usage.cjs index 5df51561e..9e4afadc2 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -3131,6 +3131,61 @@ describe('usage tests', () => { }); }); + describe('showVersion', () => { + // see #143. + it('should show version regardless of whether argv has been called', () => { + const r = checkUsage(() => { + const y = yargs() + .version('1.0.0') + .wrap(null); + + y.showVersion(); + }); + + r.errors + .join('\n') + .split(/\n+/) + .should.deep.equal([ + '1.0.0' + ]); + }); + + it('should call the correct console.log method when specified', () => { + const r = checkUsage(() => { + const y = yargs() + .version('1.0.0') + .wrap(null); + + y.showVersion('log'); + }); + + r.errors.length.should.eql(0); + r.logs + .join('\n') + .split(/\n+/) + .should.deep.equal([ + '1.0.0' + ]); + }); + + it('should call the callback to print when specified', done => { + const y = yargs() + .version('1.0.0') + .wrap(null); + + y.showVersion(printCallback); + + function printCallback(msg) { + msg + .split(/\n+/) + .should.deep.equal([ + '1.0.0' + ]); + return done(); + } + }); + }); + describe('$0', () => { function mockProcessArgv(argv, cb) { const argvOld = process.argv; From dda73ac9e9b1f4213192dcda9881944da354c907 Mon Sep 17 00:00:00 2001 From: Osman Altun <17157809+OsmanAltun@users.noreply.github.com> Date: Wed, 9 Dec 2020 18:15:07 +0100 Subject: [PATCH 3/5] Add showVersion function to docs --- docs/api.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docs/api.md b/docs/api.md index 1f88a0c07..c25d0a2ad 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1500,6 +1500,33 @@ yargs.showHelp(s => myStream.write(s)); //prints to myStream Later on, `argv` can be retrieved with `yargs.argv`. +.showVersion([consoleLevel | printCallback]) +--------------------------- + +Print the version data. + +If no argument is provided, version data is printed using `console.error`. + +```js +var yargs = require("yargs") + .version('1.0.0'); +yargs.showVersion(); //prints to stderr using console.error() +``` + +If a string is specified, version data is printed using the [`console`](https://nodejs.org/api/console.html) function `consoleLevel`. + +```js +yargs.showVersion("log"); //prints to stdout using console.log() +``` + +If a function is specified, it is called with a single argument - the version data as a string. + +```js +yargs.showVersion(s => myStream.write(s)); //prints to myStream +``` + +Later on, `argv` can be retrieved with `yargs.argv`. + .showHelpOnFail(enable, [message]) ---------------------------------- From 8a9838f14aa7f3872854f3cfc6048a8b568048c5 Mon Sep 17 00:00:00 2001 From: OsmanAltun Date: Wed, 3 Mar 2021 14:47:35 +0100 Subject: [PATCH 4/5] npm run fix --- test/usage.cjs | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/test/usage.cjs b/test/usage.cjs index 1c01710c8..144c31f66 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -3202,52 +3202,32 @@ describe('usage tests', () => { // see #143. it('should show version regardless of whether argv has been called', () => { const r = checkUsage(() => { - const y = yargs() - .version('1.0.0') - .wrap(null); + const y = yargs().version('1.0.0').wrap(null); y.showVersion(); }); - r.errors - .join('\n') - .split(/\n+/) - .should.deep.equal([ - '1.0.0' - ]); + r.errors.join('\n').split(/\n+/).should.deep.equal(['1.0.0']); }); it('should call the correct console.log method when specified', () => { const r = checkUsage(() => { - const y = yargs() - .version('1.0.0') - .wrap(null); + const y = yargs().version('1.0.0').wrap(null); y.showVersion('log'); }); r.errors.length.should.eql(0); - r.logs - .join('\n') - .split(/\n+/) - .should.deep.equal([ - '1.0.0' - ]); + r.logs.join('\n').split(/\n+/).should.deep.equal(['1.0.0']); }); it('should call the callback to print when specified', done => { - const y = yargs() - .version('1.0.0') - .wrap(null); + const y = yargs().version('1.0.0').wrap(null); y.showVersion(printCallback); function printCallback(msg) { - msg - .split(/\n+/) - .should.deep.equal([ - '1.0.0' - ]); + msg.split(/\n+/).should.deep.equal(['1.0.0']); return done(); } }); From 61358777af617762f8ce1ba4a20d3215ece0a006 Mon Sep 17 00:00:00 2001 From: OsmanAltun Date: Wed, 3 Mar 2021 15:02:22 +0100 Subject: [PATCH 5/5] Update showVersion() docs --- docs/api.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api.md b/docs/api.md index 94b3ccf42..90052f7a0 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1533,8 +1533,8 @@ Print the version data. If no argument is provided, version data is printed using `console.error`. ```js -var yargs = require("yargs") - .version('1.0.0'); +var yargs = require('yargs/yargs')(process.argv.slice(2)); +yargs.version('1.0.0'); yargs.showVersion(); //prints to stderr using console.error() ```