Skip to content

Commit

Permalink
Update minimist-options and remove type coercion patch (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulken committed May 17, 2020
1 parent 1c251e8 commit 629af48
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
20 changes: 1 addition & 19 deletions index.js
Expand Up @@ -2,15 +2,13 @@
const path = require('path');
const buildParserOptions = require('minimist-options');
const parseArguments = require('yargs-parser');
const camelCase = require('camelcase');
const camelCaseKeys = require('camelcase-keys');
const decamelizeKeys = require('decamelize-keys');
const trimNewlines = require('trim-newlines');
const redent = require('redent');
const readPkgUp = require('read-pkg-up');
const hardRejection = require('hard-rejection');
const normalizePackageData = require('normalize-package-data');
const arrify = require('arrify');

// Prevent caching of this module so module.parent is always accurate
delete require.cache[__filename];
Expand Down Expand Up @@ -69,7 +67,7 @@ const buildParserFlags = ({flags, booleanDefault}) =>
}

if (flag.isMultiple) {
flag.type = 'array';
flag.type = flag.type ? `${flag.type}-array` : 'array';
delete flag.isMultiple;
}

Expand All @@ -78,15 +76,6 @@ const buildParserFlags = ({flags, booleanDefault}) =>
return parserFlags;
}, {});

/**
Convert to alternative syntax for coercing values to expected type, according to https://github.com/yargs/yargs-parser#requireyargs-parserargs-opts.
*/
const convertToTypedArrayOption = (arrayOption, flags) =>
arrify(arrayOption).map(flagKey => ({
key: flagKey,
[flags[camelCase(flagKey, '-')].type || 'string']: true
}));

const validateFlags = (flags, options) => {
for (const [flagKey, flagValue] of Object.entries(options.flags)) {
if (flagKey !== '--' && !flagValue.isMultiple && Array.isArray(flags[flagKey])) {
Expand Down Expand Up @@ -142,13 +131,6 @@ const meow = (helpText, options) => {
};
}

if (parserOptions.array !== undefined) {
// `yargs` supports 'string|number|boolean' arrays,
// but `minimist-options` only support 'string' as element type.
// Open issue to add support to `minimist-options`: https://github.com/vadimdemedes/minimist-options/issues/18.
parserOptions.array = convertToTypedArrayOption(parserOptions.array, options.flags);
}

const {pkg} = options;
const argv = parseArguments(options.argv, parserOptions);
let help = redent(trimNewlines((options.help || '').replace(/\t+\n*$/, '')), 2);
Expand Down
4 changes: 1 addition & 3 deletions package.json
Expand Up @@ -41,12 +41,10 @@
],
"dependencies": {
"@types/minimist": "^1.2.0",
"arrify": "^2.0.1",
"camelcase": "^6.0.0",
"camelcase-keys": "^6.2.2",
"decamelize-keys": "^1.1.0",
"hard-rejection": "^2.1.0",
"minimist-options": "^4.0.2",
"minimist-options": "4.1.0",
"normalize-package-data": "^2.5.0",
"read-pkg-up": "^7.0.1",
"redent": "^3.0.0",
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Expand Up @@ -367,6 +367,19 @@ test('single flag set more than once => throws', t => {
}, {message: 'The flag --foo can only be set once.'});
});

test('isMultiple - default to type string', t => {
t.deepEqual(meow({
argv: ['--foo=bar'],
flags: {
foo: {
isMultiple: true
}
}
}).flags, {
foo: ['bar']
});
});

test('isMultiple - boolean flag', t => {
t.deepEqual(meow({
argv: ['--foo', '--foo=false'],
Expand Down

0 comments on commit 629af48

Please sign in to comment.