Skip to content

Commit

Permalink
feat(helpers): rebase, Parser, applyExtends now blessed helpers (#1733)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Sep 7, 2020
1 parent c71783a commit c7debe8
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 21 deletions.
15 changes: 14 additions & 1 deletion 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,
}
4 changes: 2 additions & 2 deletions index.cjs
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions 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'
Expand All @@ -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,
Expand All @@ -35,4 +39,4 @@ export default Object.assign(Yargs, {
processArgv,
rebase,
YError
})
}
3 changes: 2 additions & 1 deletion lib/platform-shims/esm.mjs
Expand Up @@ -39,7 +39,8 @@ export default {
basename,
dirname,
extname,
relative
relative,
resolve
},
process: {
argv: () => process.argv,
Expand Down
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -12,7 +12,7 @@
"import": "./helpers.mjs"
},
"./yargs": {
"require": "./build/index.cjs"
"require": "./yargs.cjs"
}
},
"type": "module",
Expand All @@ -28,6 +28,7 @@
"index.cjs",
"helpers.mjs",
"index.mjs",
"yargs.mjs",
"build",
"locales",
"LICENSE",
Expand Down Expand Up @@ -93,8 +94,6 @@
"standardx": {
"ignore": [
"build",
"index.cjs",
"yargs.cjs",
"**/example/**"
]
},
Expand Down
30 changes: 30 additions & 0 deletions 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'])
})
})
})
7 changes: 0 additions & 7 deletions test/esm/yargs-test.mjs
Expand Up @@ -2,7 +2,6 @@

import * as assert from 'assert'
import yargs from '../../index.mjs'
import { hideBin } from '../../helpers.mjs'

describe('ESM', () => {
describe('parser', () => {
Expand All @@ -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'])
})
})
})
27 changes: 27 additions & 0 deletions 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)
})
})
})
8 changes: 8 additions & 0 deletions 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
5 changes: 0 additions & 5 deletions yargs.js

This file was deleted.

0 comments on commit c7debe8

Please sign in to comment.