Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(helpers): rebase, Parser, applyExtends now blessed helpers #1733

Merged
merged 4 commits into from Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.