diff --git a/helpers.mjs b/helpers.mjs index 2ccdaf19a..9dbc2fbfa 100644 --- a/helpers.mjs +++ b/helpers.mjs @@ -1 +1,14 @@ -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) +} + +export { + applyExtends, + hideBin, + Parser, +} 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 5ff4f2136..bc0479162 100644 --- a/lib/platform-shims/esm.mjs +++ b/lib/platform-shims/esm.mjs @@ -39,7 +39,8 @@ export default { basename, dirname, extname, - relative + relative, + resolve }, process: { argv: () => process.argv, diff --git a/package.json b/package.json index f7a21b076..190aab45f 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..d958ab846 --- /dev/null +++ b/test/esm/helpers.mjs @@ -0,0 +1,30 @@ +'use strict' + +import * as assert from 'assert' +import { + applyExtends, + hideBin, + Parser, +} 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('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..c1c5de790 --- /dev/null +++ b/test/helpers.cjs @@ -0,0 +1,27 @@ +'use strict' + +const {describe, it} = require('mocha') +const assert = require('assert') +const yargs = require('../yargs.cjs'); +const { applyExtends } = require('../yargs.cjs'); + +const HELPER_COUNT = 2; + +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) + }) + }) +}) diff --git a/yargs.cjs b/yargs.cjs new file mode 100644 index 000000000..81116cd61 --- /dev/null +++ b/yargs.cjs @@ -0,0 +1,8 @@ +// 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) +} +Yargs.Parser = Parser +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