From 167586170249d893cba35cb341e5afcf7992fab0 Mon Sep 17 00:00:00 2001 From: bcoe Date: Fri, 4 Sep 2020 15:05:07 -0700 Subject: [PATCH 1/3] feat(helpers): rebase, Parser, applyExtends now blessed helpers --- helpers.mjs | 20 +++++++++++++++++++- index.cjs | 4 ++-- lib/cjs.ts | 8 ++++++-- lib/platform-shims/esm.mjs | 3 ++- package.json | 5 ++--- test/esm/helpers.mjs | 37 +++++++++++++++++++++++++++++++++++++ test/esm/yargs-test.mjs | 7 ------- test/helpers.cjs | 33 +++++++++++++++++++++++++++++++++ yargs.cjs | 10 ++++++++++ yargs.js | 5 ----- 10 files changed, 111 insertions(+), 21 deletions(-) create mode 100644 test/esm/helpers.mjs create mode 100644 test/helpers.cjs create mode 100644 yargs.cjs delete mode 100644 yargs.js diff --git a/helpers.mjs b/helpers.mjs index 2ccdaf19a..e948a1056 100644 --- a/helpers.mjs +++ b/helpers.mjs @@ -1 +1,19 @@ -export { hideBin } from './build/lib/utils/process-argv.js' +import { applyExtends as _applyExtends } from './build/lib/utils/apply-extends.js' +import { hideBin } from './build/lib/utils/process-argv.js' +import Parser from 'yargs-parser' +import shim from './lib/platform-shims/esm.mjs' + +const applyExtends = (config, cwd, mergeExtends) => { + return _applyExtends(config, cwd, mergeExtends, shim) +} + +const rebase = (base, dir) => { + return shim.path.relative(base, dir) +} + +export { + applyExtends, + hideBin, + Parser, + rebase +} diff --git a/index.cjs b/index.cjs index bd75d8796..d157721b5 100644 --- a/index.cjs +++ b/index.cjs @@ -2,9 +2,9 @@ // classic singleton yargs API, to use yargs // without running as a singleton do: // require('yargs/yargs')(process.argv.slice(2)) -const Yargs = require('./build/index.cjs') +const { Yargs, processArgv } = require('./build/index.cjs') -Argv(Yargs.processArgv.hideBin(process.argv)) +Argv(processArgv.hideBin(process.argv)) module.exports = Argv diff --git a/lib/cjs.ts b/lib/cjs.ts index d03effda2..a5e577bd6 100644 --- a/lib/cjs.ts +++ b/lib/cjs.ts @@ -1,6 +1,7 @@ 'use strict' // Bootstraps yargs for a CommonJS runtime: +import { applyExtends } from './utils/apply-extends' import { argsert } from './argsert.js' import { isPromise } from './utils/is-promise.js' import { objFilter } from './utils/obj-filter.js' @@ -25,7 +26,10 @@ if (process && process.version) { const Parser = require('yargs-parser') const Yargs = YargsWithShim(cjsPlatformShim) -export default Object.assign(Yargs, { +export default { + applyExtends, + cjsPlatformShim, + Yargs, argsert, globalMiddlewareFactory, isPromise, @@ -35,4 +39,4 @@ export default Object.assign(Yargs, { processArgv, rebase, YError -}) +} diff --git a/lib/platform-shims/esm.mjs b/lib/platform-shims/esm.mjs index abd652b80..640a51145 100644 --- a/lib/platform-shims/esm.mjs +++ b/lib/platform-shims/esm.mjs @@ -37,7 +37,8 @@ export default { basename, dirname, extname, - relative + relative, + resolve }, process: { argv: () => process.argv, diff --git a/package.json b/package.json index a0d2b8ed9..c7ab440f1 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "import": "./helpers.mjs" }, "./yargs": { - "require": "./build/index.cjs" + "require": "./yargs.cjs" } }, "type": "module", @@ -28,6 +28,7 @@ "index.cjs", "helpers.mjs", "index.mjs", + "yargs.mjs", "build", "locales", "LICENSE", @@ -93,8 +94,6 @@ "standardx": { "ignore": [ "build", - "index.cjs", - "yargs.cjs", "**/example/**" ] }, diff --git a/test/esm/helpers.mjs b/test/esm/helpers.mjs new file mode 100644 index 000000000..36dae173d --- /dev/null +++ b/test/esm/helpers.mjs @@ -0,0 +1,37 @@ +'use strict' + +import * as assert from 'assert' +import { + applyExtends, + hideBin, + Parser, + rebase, +} from '../../helpers.mjs' + +describe('helpers', () => { + describe('applyExtends', () => { + it('exposes applyExtends helper', () => { + const conf = applyExtends({extends: './package.json', apple: 'red'}, process.cwd(), true); + // assert.strictEqual(conf.name, 'yargs') // loads packge.json. + // assert.strictEqual(conf.apple, 'red') // keeps config with extends. + }) + }) + describe('Parser', () => { + it('exposes functional argument parser', () => { + const argv = Parser('--foo --bar=99') + assert.strictEqual(argv.bar, 99) + }) + }) + describe('rebase', () => { + it('exposes rebase', () => { + const path = rebase('./foo', './foo/bar') + // assert.strictEqual(path, 'bar') + }) + }) + describe('hideBin', () => { + it('hides bin for standard node.js application', () => { + const args = hideBin(['node', 'foo.js', '--apple', '--banana']) + // assert.deepEqual(args, ['--apple', '--banana']) + }) + }) +}) diff --git a/test/esm/yargs-test.mjs b/test/esm/yargs-test.mjs index 9c5a7a355..b22ceb3c5 100644 --- a/test/esm/yargs-test.mjs +++ b/test/esm/yargs-test.mjs @@ -2,7 +2,6 @@ import * as assert from 'assert' import yargs from '../../index.mjs' -import { hideBin } from '../../helpers.mjs' describe('ESM', () => { describe('parser', () => { @@ -22,10 +21,4 @@ describe('ESM', () => { assert.match(err.message, /not supported yet for ESM/) }) }) - describe('hideBin', () => { - it('hides bin for standard node.js application', () => { - const args = hideBin(['node', 'foo.js', '--apple', '--banana']) - assert.deepEqual(args, ['--apple', '--banana']) - }) - }) }) diff --git a/test/helpers.cjs b/test/helpers.cjs new file mode 100644 index 000000000..b2ab1b5ea --- /dev/null +++ b/test/helpers.cjs @@ -0,0 +1,33 @@ +'use strict' + +const {describe, it} = require('mocha') +const assert = require('assert') +const yargs = require('../yargs.cjs'); +const { applyExtends } = require('../yargs.cjs'); + +const HELPER_COUNT = 3; + +describe('helpers', () => { + it('does not expose additional helpers beyond blessed list', () => { + assert.strictEqual(Object.keys(yargs).length, HELPER_COUNT) + }) + describe('applyExtends', () => { + it('exposes applyExtends helper', () => { + const conf = applyExtends({extends: './package.json', apple: 'red'}, process.cwd(), true); + assert.strictEqual(conf.name, 'yargs') // loads packge.json. + assert.strictEqual(conf.apple, 'red') // keeps config with extends. + }) + }) + describe('Parser', () => { + it('exposes functional argument parser', () => { + const argv = yargs.Parser('--foo --bar=99') + assert.strictEqual(argv.bar, 99) + }) + }) + describe('rebase', () => { + it('exposes rebase', () => { + const path = yargs.rebase('./foo', './foo/bar') + assert.strictEqual(path, 'bar') + }) + }) +}) diff --git a/yargs.cjs b/yargs.cjs new file mode 100644 index 000000000..fea88db89 --- /dev/null +++ b/yargs.cjs @@ -0,0 +1,10 @@ +// CJS import shim for older versions of Node.js. +// this can be removed once all supported Node.js versions support +// export maps: +const {applyExtends, cjsPlatformShim, Parser, rebase, Yargs} = require('./build/index.cjs') +Yargs.applyExtends = (config, cwd, mergeExtends) => { + return applyExtends(config, cwd, mergeExtends, cjsPlatformShim) +} +Yargs.Parser = Parser +Yargs.rebase = rebase +module.exports = Yargs diff --git a/yargs.js b/yargs.js deleted file mode 100644 index 44425c3c2..000000000 --- a/yargs.js +++ /dev/null @@ -1,5 +0,0 @@ -// CJS import shim for older versions of Node.js. -// this can be removed once all supported Node.js versions support -// export maps: -const Yargs = require('./build/index.cjs') -module.exports = Yargs From 1ced16216ccb80550880f592bcf93d093a409d27 Mon Sep 17 00:00:00 2001 From: bcoe Date: Fri, 4 Sep 2020 15:11:04 -0700 Subject: [PATCH 2/3] chore: removed rebase helper --- helpers.mjs | 5 ----- test/esm/helpers.mjs | 11 ++--------- test/helpers.cjs | 8 +------- yargs.cjs | 1 - 4 files changed, 3 insertions(+), 22 deletions(-) diff --git a/helpers.mjs b/helpers.mjs index e948a1056..9dbc2fbfa 100644 --- a/helpers.mjs +++ b/helpers.mjs @@ -7,13 +7,8 @@ const applyExtends = (config, cwd, mergeExtends) => { return _applyExtends(config, cwd, mergeExtends, shim) } -const rebase = (base, dir) => { - return shim.path.relative(base, dir) -} - export { applyExtends, hideBin, Parser, - rebase } diff --git a/test/esm/helpers.mjs b/test/esm/helpers.mjs index 36dae173d..d958ab846 100644 --- a/test/esm/helpers.mjs +++ b/test/esm/helpers.mjs @@ -5,15 +5,14 @@ import { applyExtends, hideBin, Parser, - rebase, } from '../../helpers.mjs' describe('helpers', () => { describe('applyExtends', () => { it('exposes applyExtends helper', () => { const conf = applyExtends({extends: './package.json', apple: 'red'}, process.cwd(), true); - // assert.strictEqual(conf.name, 'yargs') // loads packge.json. - // assert.strictEqual(conf.apple, 'red') // keeps config with extends. + assert.strictEqual(conf.name, 'yargs') // loads packge.json. + assert.strictEqual(conf.apple, 'red') // keeps config with extends. }) }) describe('Parser', () => { @@ -22,12 +21,6 @@ describe('helpers', () => { assert.strictEqual(argv.bar, 99) }) }) - describe('rebase', () => { - it('exposes rebase', () => { - const path = rebase('./foo', './foo/bar') - // assert.strictEqual(path, 'bar') - }) - }) describe('hideBin', () => { it('hides bin for standard node.js application', () => { const args = hideBin(['node', 'foo.js', '--apple', '--banana']) diff --git a/test/helpers.cjs b/test/helpers.cjs index b2ab1b5ea..c1c5de790 100644 --- a/test/helpers.cjs +++ b/test/helpers.cjs @@ -5,7 +5,7 @@ const assert = require('assert') const yargs = require('../yargs.cjs'); const { applyExtends } = require('../yargs.cjs'); -const HELPER_COUNT = 3; +const HELPER_COUNT = 2; describe('helpers', () => { it('does not expose additional helpers beyond blessed list', () => { @@ -24,10 +24,4 @@ describe('helpers', () => { assert.strictEqual(argv.bar, 99) }) }) - describe('rebase', () => { - it('exposes rebase', () => { - const path = yargs.rebase('./foo', './foo/bar') - assert.strictEqual(path, 'bar') - }) - }) }) diff --git a/yargs.cjs b/yargs.cjs index fea88db89..58acc7db0 100644 --- a/yargs.cjs +++ b/yargs.cjs @@ -6,5 +6,4 @@ Yargs.applyExtends = (config, cwd, mergeExtends) => { return applyExtends(config, cwd, mergeExtends, cjsPlatformShim) } Yargs.Parser = Parser -Yargs.rebase = rebase module.exports = Yargs From 5081eb117d557de2f1fb2143850800637858e879 Mon Sep 17 00:00:00 2001 From: bcoe Date: Mon, 7 Sep 2020 10:53:27 -0700 Subject: [PATCH 3/3] docs: correct comment --- yargs.cjs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/yargs.cjs b/yargs.cjs index 58acc7db0..81116cd61 100644 --- a/yargs.cjs +++ b/yargs.cjs @@ -1,7 +1,6 @@ -// CJS import shim for older versions of Node.js. -// this can be removed once all supported Node.js versions support -// export maps: -const {applyExtends, cjsPlatformShim, Parser, rebase, Yargs} = require('./build/index.cjs') +// TODO: consolidate on using a helpers file at some point in the future, which +// is the approach currently used to export Parser and applyExtends for ESM: +const {applyExtends, cjsPlatformShim, Parser, Yargs} = require('./build/index.cjs') Yargs.applyExtends = (config, cwd, mergeExtends) => { return applyExtends(config, cwd, mergeExtends, cjsPlatformShim) }