This commit is contained in:
Lev
2021-06-16 19:39:43 -05:00
parent 2dc386e7fd
commit 51ce0e142a
2210 changed files with 263077 additions and 0 deletions

View File

@ -0,0 +1,82 @@
# Changelog
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
## [2.0.0](https://www.github.com/yargs/yargs-unparser/compare/v1.6.4...v2.0.0) (2020-10-02)
### ⚠ BREAKING CHANGES
* upgrade deps drop Node 6/8 (#71)
### Code Refactoring
* upgrade deps drop Node 6/8 ([#71](https://www.github.com/yargs/yargs-unparser/issues/71)) ([c686882](https://www.github.com/yargs/yargs-unparser/commit/c686882f5ad554be44169e3745e741cb4ec898d0))
### [1.6.4](https://www.github.com/yargs/yargs-unparser/compare/v1.6.3...v1.6.4) (2020-10-01)
### Bug Fixes
* **security:** upgraded flat to version ^5.0.2 ([9bd7c67](https://www.github.com/yargs/yargs-unparser/commit/9bd7c672e12417319c5d4de79070d9c7cd5107f2))
### [1.6.3](https://www.github.com/yargs/yargs-unparser/compare/v1.6.2...v1.6.3) (2020-06-17)
### Bug Fixes
* test automatic publish ([209a487](https://www.github.com/yargs/yargs-unparser/commit/209a4870af799f4b200c2a89d7b7e50c9fd5fd1f))
### [1.6.2](https://www.github.com/yargs/yargs-unparser/compare/v1.6.1...v1.6.2) (2020-06-17)
### Bug Fixes
* **readme:** marketing was injected dubiously into README ([#60](https://www.github.com/yargs/yargs-unparser/issues/60)) ([1167667](https://www.github.com/yargs/yargs-unparser/commit/1167667886fcb103c747e3c9855f353ee0e41c03))
### [1.6.1](https://www.github.com/yargs/yargs-unparser/compare/v1.6.0...v1.6.1) (2020-06-17)
### Bug Fixes
* **deps:** downgrade yargs, such that we continue supporting Node 6 ([#57](https://www.github.com/yargs/yargs-unparser/issues/57)) ([f69406c](https://www.github.com/yargs/yargs-unparser/commit/f69406c34bead63011590f7b51a24a6f311c1a48))
## 1.6.0 (2019-07-30)
### Bug Fixes
* **security:** update deps addressing recent audit vulnerabilities ([#40](https://github.com/yargs/yargs-unparser/issues/40)) ([2e74f1b](https://github.com/yargs/yargs-unparser/commit/2e74f1b))
* address bug with camelCased flattened keys ([#32](https://github.com/yargs/yargs-unparser/issues/32)) ([981533a](https://github.com/yargs/yargs-unparser/commit/981533a))
* **deps:** updated the lodash version to fix vulnerability ([#39](https://github.com/yargs/yargs-unparser/issues/39)) ([7375966](https://github.com/yargs/yargs-unparser/commit/7375966))
* **package:** update yargs to version 10.0.3 ([f1eb4cb](https://github.com/yargs/yargs-unparser/commit/f1eb4cb))
* **package:** update yargs to version 11.0.0 ([6aa7c91](https://github.com/yargs/yargs-unparser/commit/6aa7c91))
### Features
* add interoperation with minimist ([ba477f5](https://github.com/yargs/yargs-unparser/commit/ba477f5))
<a name="1.5.0"></a>
# 1.5.0 (2018-11-30)
### Bug Fixes
* **package:** update yargs to version 10.0.3 ([f1eb4cb](https://github.com/yargs/yargs-unparser/commit/f1eb4cb))
* **package:** update yargs to version 11.0.0 ([6aa7c91](https://github.com/yargs/yargs-unparser/commit/6aa7c91))
### Features
* add interoperation with minimist ([ba477f5](https://github.com/yargs/yargs-unparser/commit/ba477f5))
<a name="1.4.0"></a>
# [1.4.0](https://github.com/moxystudio/yargs-unparser/compare/v1.3.0...v1.4.0) (2017-12-30)
### Features
* add interoperation with minimist ([ba477f5](https://github.com/moxystudio/yargs-unparser/commit/ba477f5))

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2017 Made With MOXY Lda <hello@moxy.studio>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,86 @@
# yargs-unparser
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url]
[npm-url]:https://npmjs.org/package/yargs-unparser
[npm-image]:http://img.shields.io/npm/v/yargs-unparser.svg
[downloads-image]:http://img.shields.io/npm/dm/yargs-unparser.svg
Converts back a `yargs` argv object to its original array form.
Probably the unparser word doesn't even exist, but it sounds nice and goes well with [yargs-parser](https://github.com/yargs/yargs-parser).
The code originally lived in [MOXY](https://github.com/moxystudio)'s GitHub but was later moved here for discoverability.
## Installation
`$ npm install yargs-unparser`
## Usage
```js
const parse = require('yargs-parser');
const unparse = require('yargs-unparser');
const argv = parse(['--no-boolean', '--number', '4', '--string', 'foo'], {
boolean: ['boolean'],
number: ['number'],
string: ['string'],
});
// { boolean: false, number: 4, string: 'foo', _: [] }
const unparsedArgv = unparse(argv);
// ['--no-boolean', '--number', '4', '--string', 'foo'];
```
The second argument of `unparse` accepts an options object:
- `alias`: The [aliases](https://github.com/yargs/yargs-parser#requireyargs-parserargs-opts) so that duplicate options aren't generated
- `default`: The [default](https://github.com/yargs/yargs-parser#requireyargs-parserargs-opts) values so that the options with default values are omitted
- `command`: The [command](https://github.com/yargs/yargs/blob/master/docs/advanced.md#commands) first argument so that command names and positional arguments are handled correctly
### Example with `command` options
```js
const yargs = require('yargs');
const unparse = require('yargs-unparser');
const argv = yargs
.command('my-command <positional>', 'My awesome command', (yargs) =>
yargs
.option('boolean', { type: 'boolean' })
.option('number', { type: 'number' })
.option('string', { type: 'string' })
)
.parse(['my-command', 'hello', '--no-boolean', '--number', '4', '--string', 'foo']);
// { positional: 'hello', boolean: false, number: 4, string: 'foo', _: ['my-command'] }
const unparsedArgv = unparse(argv, {
command: 'my-command <positional>',
});
// ['my-command', 'hello', '--no-boolean', '--number', '4', '--string', 'foo'];
```
### Caveats
The returned array can be parsed again by `yargs-parser` using the default configuration. If you used custom configuration that you want `yargs-unparser` to be aware, please fill an [issue](https://github.com/yargs/yargs-unparser/issues).
If you `coerce` in weird ways, things might not work correctly.
## Tests
`$ npm test`
`$ npm test -- --watch` during development
## Supported Node.js Versions
Libraries in this ecosystem make a best effort to track
[Node.js' release schedule](https://nodejs.org/en/about/releases/). Here's [a
post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).
## License
[MIT License](http://opensource.org/licenses/MIT)

View File

@ -0,0 +1,166 @@
'use strict';
const flatten = require('flat');
const camelcase = require('camelcase');
const decamelize = require('decamelize');
const isPlainObj = require('is-plain-obj');
function isAlias(key, alias) {
// TODO Switch to Object.values one Node.js 6 is dropped
return Object.keys(alias).some((id) => [].concat(alias[id]).indexOf(key) !== -1);
}
function hasDefaultValue(key, value, defaults) {
return value === defaults[key];
}
function isCamelCased(key, argv) {
return /[A-Z]/.test(key) && camelcase(key) === key && // Is it camel case?
argv[decamelize(key, '-')] != null; // Is the standard version defined?
}
function keyToFlag(key) {
return key.length === 1 ? `-${key}` : `--${key}`;
}
function parseCommand(cmd) {
const extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' ');
const splitCommand = extraSpacesStrippedCommand.split(/\s+(?![^[]*]|[^<]*>)/);
const bregex = /\.*[\][<>]/g;
const firstCommand = splitCommand.shift();
if (!firstCommand) { throw new Error(`No command found in: ${cmd}`); }
const parsedCommand = {
cmd: firstCommand.replace(bregex, ''),
demanded: [],
optional: [],
};
splitCommand.forEach((cmd, i) => {
let variadic = false;
cmd = cmd.replace(/\s/g, '');
if (/\.+[\]>]/.test(cmd) && i === splitCommand.length - 1) { variadic = true; }
if (/^\[/.test(cmd)) {
parsedCommand.optional.push({
cmd: cmd.replace(bregex, '').split('|'),
variadic,
});
} else {
parsedCommand.demanded.push({
cmd: cmd.replace(bregex, '').split('|'),
variadic,
});
}
});
return parsedCommand;
}
function unparseOption(key, value, unparsed) {
if (typeof value === 'string') {
unparsed.push(keyToFlag(key), value);
} else if (value === true) {
unparsed.push(keyToFlag(key));
} else if (value === false) {
unparsed.push(`--no-${key}`);
} else if (Array.isArray(value)) {
value.forEach((item) => unparseOption(key, item, unparsed));
} else if (isPlainObj(value)) {
const flattened = flatten(value, { safe: true });
for (const flattenedKey in flattened) {
if (!isCamelCased(flattenedKey, flattened)) {
unparseOption(`${key}.${flattenedKey}`, flattened[flattenedKey], unparsed);
}
}
// Fallback case (numbers and other types)
} else if (value != null) {
unparsed.push(keyToFlag(key), `${value}`);
}
}
function unparsePositional(argv, options, unparsed) {
const knownPositional = [];
// Unparse command if set, collecting all known positional arguments
// e.g.: build <first> <second> <rest...>
if (options.command) {
const { 0: cmd, index } = options.command.match(/[^<[]*/);
const { demanded, optional } = parseCommand(`foo ${options.command.substr(index + cmd.length)}`);
// Push command (can be a deep command)
unparsed.push(...cmd.trim().split(/\s+/));
// Push positional arguments
[...demanded, ...optional].forEach(({ cmd: cmds, variadic }) => {
knownPositional.push(...cmds);
const cmd = cmds[0];
const args = (variadic ? argv[cmd] || [] : [argv[cmd]])
.filter((arg) => arg != null)
.map((arg) => `${arg}`);
unparsed.push(...args);
});
}
// Unparse unkown positional arguments
argv._ && unparsed.push(...argv._.slice(knownPositional.length));
return knownPositional;
}
function unparseOptions(argv, options, knownPositional, unparsed) {
for (const key of Object.keys(argv)) {
const value = argv[key];
if (
// Remove positional arguments
knownPositional.includes(key) ||
// Remove special _, -- and $0
['_', '--', '$0'].includes(key) ||
// Remove aliases
isAlias(key, options.alias) ||
// Remove default values
hasDefaultValue(key, value, options.default) ||
// Remove camel-cased
isCamelCased(key, argv)
) {
continue;
}
unparseOption(key, argv[key], unparsed);
}
}
function unparseEndOfOptions(argv, options, unparsed) {
// Unparse ending (--) arguments if set
argv['--'] && unparsed.push('--', ...argv['--']);
}
// ------------------------------------------------------------
function unparser(argv, options) {
options = Object.assign({
alias: {},
default: {},
command: null,
}, options);
const unparsed = [];
// Unparse known & unknown positional arguments (foo <first> <second> [rest...])
// All known positional will be returned so that they are not added as flags
const knownPositional = unparsePositional(argv, options, unparsed);
// Unparse option arguments (--foo hello --bar hi)
unparseOptions(argv, options, knownPositional, unparsed);
// Unparse "end-of-options" arguments (stuff after " -- ")
unparseEndOfOptions(argv, options, unparsed);
return unparsed;
}
module.exports = unparser;

View File

@ -0,0 +1,83 @@
{
"_from": "yargs-unparser@2.0.0",
"_id": "yargs-unparser@2.0.0",
"_inBundle": false,
"_integrity": "sha512-7pRTIA9Qc1caZ0bZ6RYRGbHJthJWuakf+WmHK0rVeLkNrrGhfoabBNdue6kdINI6r4if7ocq9aD/n7xwKOdzOA==",
"_location": "/yargs-unparser",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "yargs-unparser@2.0.0",
"name": "yargs-unparser",
"escapedName": "yargs-unparser",
"rawSpec": "2.0.0",
"saveSpec": null,
"fetchSpec": "2.0.0"
},
"_requiredBy": [
"/mocha"
],
"_resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-2.0.0.tgz",
"_shasum": "f131f9226911ae5d9ad38c432fe809366c2325eb",
"_spec": "yargs-unparser@2.0.0",
"_where": "C:\\Files\\Repositories\\UoL\\CM2010 Software Design and Development\\Topic 4\\8.2.4\\node_modules\\mocha",
"author": {
"name": "André Cruz",
"email": "andre@moxy.studio"
},
"bugs": {
"url": "https://github.com/yargs/yargs-unparser/issues"
},
"bundleDependencies": false,
"dependencies": {
"camelcase": "^6.0.0",
"decamelize": "^4.0.0",
"flat": "^5.0.2",
"is-plain-obj": "^2.1.0"
},
"deprecated": false,
"description": "Converts back a yargs argv object to its original array form",
"devDependencies": {
"eslint": "^6.1.0",
"eslint-config-moxy": "^7.1.0",
"husky": "^3.0.1",
"jest": "^24.9.0",
"lint-staged": "^9.5.0",
"minimist": "^1.2.5",
"yargs-parser": "^18.1.3"
},
"engines": {
"node": ">=10"
},
"files": [],
"homepage": "https://github.com/yargs/yargs-unparser",
"keywords": [
"yargs",
"unparse",
"expand",
"inverse",
"argv"
],
"license": "MIT",
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
]
},
"main": "index.js",
"name": "yargs-unparser",
"repository": {
"type": "git",
"url": "git+https://github.com/yargs/yargs-unparser.git"
},
"scripts": {
"fix": "eslint . --fix",
"lint": "eslint .",
"precommit": "lint-staged",
"prerelease": "npm t && npm run lint",
"test": "jest --env node --coverage"
},
"version": "2.0.0"
}