New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

argufy

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

argufy - npm Package Compare versions

Comparing version 1.7.3 to 1.7.4

src/commands/argufy.js

5

CHANGELOG.md
## 7 December 2019
### [1.7.4](https://github.com/artdecocode/argufy/compare/v1.7.3...v1.7.4)
- [fix] Refactor multiple files parsing.
- [fix] Correct type for multiple command.
### [1.7.3](https://github.com/artdecocode/argufy/compare/v1.7.2...v1.7.3)

@@ -4,0 +9,0 @@

251

compile/argufy.js
#!/usr/bin/env node
const fs = require('fs');
const {readFileSync:u, writeFileSync:v} = fs;
function w(a, c, b) {
const d = [];
c.replace(a, (h, ...l) => {
h = l.slice(0, l.length - 2).reduce((m, g, k) => {
k = b[k];
if (!k || void 0 === g) {
return m;
}
m[k] = g;
return m;
}, {});
d.push(h);
});
return d;
}
;const x = new RegExp(`${/([^\s>=/]+)/.source}(?:\\s*=\\s*${/(?:"([\s\S]*?)"|'([\s\S]*?)')/.source})?`, "g"), y = new RegExp(`(?:\\s+((?:${x.source}\\s*)*))`);
const A = (a, c) => w(new RegExp(`<(${(Array.isArray(a) ? a : [a]).join("|")})${y.source}?(?:${/\s*\/>/.source}|${/>([\s\S]+?)?<\/\1>/.source})`, "g"), c, "t a v v1 v2 c".split(" ")).map(({t:b, a:d = "", c:h = ""}) => {
d = d.replace(/\/$/, "").trim();
d = z(d);
return {content:h, props:d, tag:b};
}), z = a => w(x, a, ["key", "val", "def", "f"]).reduce((c, {key:b, val:d}) => {
if (void 0 === d) {
return c[b] = !0, c;
}
c[b] = "true" == d ? !0 : "false" == d ? !1 : /^\d+$/.test(d) ? parseInt(d, 10) : d;
return c;
}, {});
function B(a = {usage:{}}) {
const {usage:c = {}, description:b, line:d, example:h} = a;
function u(a = {usage:{}}) {
const {usage:c = {}, description:d, line:b, example:h} = a;
a = Object.keys(c);
const l = Object.values(c), [m] = a.reduce(([f = 0, e = 0], n) => {
const m = Object.values(c), [l] = a.reduce(([f = 0, e = 0], n) => {
const r = c[n].split("\n").reduce((p, q) => q.length > p ? q.length : p, 0);

@@ -45,11 +17,11 @@ r > e && (e = r);

a = a.reduce((f, e, n) => {
n = l[n].split("\n");
e = g(e, m);
n = m[n].split("\n");
e = g(e, l);
const [r, ...p] = n;
e = `${e}\t${r}`;
const q = g("", m);
const q = g("", l);
n = p.map(t => `${q}\t${t}`);
return [...f, e, ...n];
}, []).map(f => `\t${f}`);
const k = [b, ` ${d || ""}`].filter(f => f ? f.trim() : f).join("\n\n");
const k = [d, ` ${b || ""}`].filter(f => f ? f.trim() : f).join("\n\n");
a = `${k ? `${k}\n` : ""}

@@ -64,53 +36,53 @@ ${a.join("\n")}

}
;const C = (a, c, b, d = !1, h = !1) => {
const l = b ? new RegExp(`^-(${b}|-${c})$`) : new RegExp(`^--${c}$`);
c = a.findIndex(m => l.test(m));
;const v = (a, c, d, b = !1, h = !1) => {
const m = d ? new RegExp(`^-(${d}|-${c})$`) : new RegExp(`^--${c}$`);
c = a.findIndex(l => m.test(l));
if (-1 == c) {
return {argv:a};
}
if (d) {
if (b) {
return {value:!0, argv:[...a.slice(0, c), ...a.slice(c + 1)]};
}
d = c + 1;
b = a[d];
if (!b || "string" == typeof b && b.startsWith("--")) {
b = c + 1;
d = a[b];
if (!d || "string" == typeof d && d.startsWith("--")) {
return {argv:a};
}
h && (b = parseInt(b, 10));
return {value:b, argv:[...a.slice(0, c), ...a.slice(d + 1)]};
}, D = a => {
h && (d = parseInt(d, 10));
return {value:d, argv:[...a.slice(0, c), ...a.slice(b + 1)]};
}, w = a => {
const c = [];
for (let b = 0; b < a.length; b++) {
const d = a[b];
if (d.startsWith("-")) {
for (let d = 0; d < a.length; d++) {
const b = a[d];
if (b.startsWith("-")) {
break;
}
c.push(d);
c.push(b);
}
return c;
}, F = () => {
var a = E;
return Object.keys(a).reduce((c, b) => {
const d = a[b];
if ("string" == typeof d) {
return c[`-${d}`] = "", c;
}, y = () => {
var a = x;
return Object.keys(a).reduce((c, d) => {
const b = a[d];
if ("string" == typeof b) {
return c[`-${b}`] = "", c;
}
b = d.command ? b : `--${b}`;
d.short && (b = `${b}, -${d.short}`);
let h = d.description;
d.default && (h = `${h}\nDefault: ${d.default}.`);
c[b] = h;
d = b.command ? d : `--${d}`;
b.short && (d = `${d}, -${b.short}`);
let h = b.description;
b.default && (h = `${h}\nDefault: ${b.default}.`);
c[d] = h;
return c;
}, {});
};
const E = {input:{description:"The location of the `arguments.xml` file.", command:!0, multiple:!0, default:"types/arguments.xml"}, output:{description:"The destination where to save output.\nIf not passed, prints to stdout.", short:"o"}, help:{description:"Print the help information and exit.", boolean:!0, short:"h"}, version:{description:"Show the version's number and exit.", boolean:!0, short:"v"}}, G = function(a = {}, c = process.argv) {
const x = {inputs:{description:"The locations of `arguments.xml` files.", command:!0, multiple:!0, default:"types/arguments.xml"}, output:{description:"The destination where to save output.\nIf not passed, prints to stdout.", short:"o"}, help:{description:"Print the help information and exit.", boolean:!0, short:"h"}, version:{description:"Show the version's number and exit.", boolean:!0, short:"v"}}, z = function(a = {}, c = process.argv) {
[, , ...c] = c;
const b = D(c);
c = c.slice(b.length);
let d = !b.length;
const d = w(c);
c = c.slice(d.length);
let b = !d.length;
const h = Object.entries(a).reduce((g, [k, f]) => {
g[k] = "string" == typeof f ? {short:f} : f;
return g;
}, {}), l = Object.entries(h).reduce(({b:g, ...k}, [f, e]) => {
if (0 == g.length && d) {
}, {}), m = Object.entries(h).reduce(({b:g, ...k}, [f, e]) => {
if (0 == g.length && b) {
return {b:g, ...k};

@@ -120,4 +92,4 @@ }

try {
const {short:r, boolean:p, number:q, command:t, multiple:J} = e;
t && J && b.length ? (n = b, d = !0) : t && b.length ? (n = b[0], d = !0) : {value:n, argv:g} = C(g, f, r, p, q);
const {short:r, boolean:p, number:q, command:t, multiple:E} = e;
t && E && d.length ? (n = d, b = !0) : t && d.length ? (n = d[0], b = !0) : {value:n, argv:g} = v(g, f, r, p, q);
} catch (r) {

@@ -127,3 +99,3 @@ return {b:g, ...k};

return void 0 === n ? {b:g, ...k} : {b:g, ...k, [f]:n};
}, {b:c}), m = Object.entries(a).reduce((g, [k, f]) => {
}, {b:c}), l = Object.entries(a).reduce((g, [k, f]) => {
const {short:e} = f;

@@ -137,3 +109,3 @@ if (!e) {

}, {});
Object.entries(l).reduce((g, [k, f]) => {
Object.entries(m).reduce((g, [k, f]) => {
const e = h[k];

@@ -144,23 +116,51 @@ if (!e) {

const {short:n, boolean:r} = e;
(m[n] || []).forEach(({name:p, boolean:q, number:t}) => {
p == k || r && !q || (t ? f = parseInt(f, 10) : q || (f = `${f}`), l[p] = f);
(l[n] || []).forEach(({name:p, boolean:q, number:t}) => {
p == k || r && !q || (t ? f = parseInt(f, 10) : q || (f = `${f}`), m[p] = f);
});
}, {});
return l;
}(E), H = G.input || "types/arguments.xml", I = G.output, K = G.version;
return m;
}(x), A = z.inputs || ["types/arguments.xml"], B = z.output, C = z.version;
const {readFileSync:D, writeFileSync:F} = fs;
function G(a, c, d) {
const b = [];
c.replace(a, (h, ...m) => {
h = m.slice(0, m.length - 2).reduce((l, g, k) => {
k = d[k];
if (!k || void 0 === g) {
return l;
}
l[k] = g;
return l;
}, {});
b.push(h);
});
return b;
}
;const H = new RegExp(`${/([^\s>=/]+)/.source}(?:\\s*=\\s*${/(?:"([\s\S]*?)"|'([\s\S]*?)')/.source})?`, "g"), I = new RegExp(`(?:\\s+((?:${H.source}\\s*)*))`);
const K = (a, c) => G(new RegExp(`<(${(Array.isArray(a) ? a : [a]).join("|")})${I.source}?(?:${/\s*\/>/.source}|${/>([\s\S]+?)?<\/\1>/.source})`, "g"), c, "t a v v1 v2 c".split(" ")).map(({t:d, a:b = "", c:h = ""}) => {
b = b.replace(/\/$/, "").trim();
b = J(b);
return {content:h, props:b, tag:d};
}), J = a => G(H, a, ["key", "val", "def", "f"]).reduce((c, {key:d, val:b}) => {
if (void 0 === b) {
return c[d] = !0, c;
}
c[d] = "true" == b ? !0 : "false" == b ? !1 : /^\d+$/.test(b) ? parseInt(b, 10) : b;
return c;
}, {});
const L = a => {
(a = a.map(({name:c, short:b, boolean:d, number:h, command:l, multiple:m, description:g, default:k}) => {
(a = a.map(({name:c, short:d, boolean:b, number:h, command:m, multiple:l, description:g, default:k}) => {
const f = g.replace(/'/g, "\\'").replace(/\n\s*/g, "\\n");
let e = [];
g && e.push(`description: '${f}'`);
l && e.push("command: true");
m && e.push("multiple: true");
d && e.push("boolean: true");
m && e.push("command: true");
l && e.push("multiple: true");
b && e.push("boolean: true");
h && e.push("number: true");
k && e.push(`default: '${k}'`);
if (!e.length && b) {
return `'${c}': '${b}'`;
if (!e.length && d) {
return `'${c}': '${d}'`;
}
if (e.length || b) {
b && e.push(`short: '${b}'`);
if (e.length || d) {
d && e.push(`short: '${d}'`);
} else {

@@ -177,61 +177,62 @@ return `'${c}': { }`;

}`;
}, N = (a, c, b = "") => {
const d = L(a), h = `argsConfig${b}`, l = `args${b}`;
a = a.map(m => M(m, l));
return {i:l, h:`
export const ${h} = ${d}
const ${l} = argufy(${h}${c ? `, [process.argv[0], process.argv[1], ...${c}._argv]` : ""})
${a.join("\n\n")}
`};
}, M = ({name:a, number:c, boolean:b, description:d, default:h, multiple:l}, m = "args") => {
let g = "string";
c && (g = "number");
b && (g = "boolean");
l && (g = "(!Array<string>|string)");
l = a.replace(/-(.)/g, (f, e) => e.toUpperCase());
}, N = a => {
a = a.map(({i:b = "", h}) => {
const m = L(h);
b = `argsConfig${b}`;
h = h.map(l => M(l));
return {f:b, j:`export const ${b} = ${m}`, g:h};
});
const c = `const args = argufy(${1 < a.length ? `{ ${a.map(({f:b}) => `...${b}`).join(", ")} }` : a[0].f})`;
let d = a.map(({j:b}) => b).join("\n\n");
return d = d + `\n\n${c}\n\n` + a.map(({g:b}) => b.join("\n\n")).join("\n\n");
}, M = ({name:a, number:c, boolean:d, description:b, default:h, multiple:m}) => {
let l = "string";
c && (l = "number");
d && (l = "boolean");
m && (l = "!Array<string>");
const g = a.replace(/-(.)/g, (f, e) => e.toUpperCase());
let k = `'${h}'`;
if (c || b) {
if (c || d) {
k = h;
}
m && (k = `[${k}]`);
c = void 0 !== h;
return `/**
* ${d}${c ? ` Default \`${h}\`.` : ""}
* ${b}${c ? ` Default \`${h}\`.` : ""}
*/
export const _${l} = /** @type {${g}} */ (${m}['${a}']${c ? ` || ${k}` : ""})`;
export const _${g} = /** @type {${l}} */ (${"args"}['${a}']${c ? ` || ${k}` : ""})`;
};
if (G.help) {
const a = F();
console.log(B({usage:a, description:"Generates the JavaScript file that exports all arguments\nbased on the configuration found in the arguments.xml file.\nhttps://artdecocode.com/argufy/", line:"argufy input [-o output] [-vh]", example:"argufy types/arguments.xml -o src/bin/get-args.js"}));
process.exit();
} else {
K && (console.log(require("../package.json").version), process.exit());
}
const O = a => {
a = `${u(a)}`;
a = A("arguments", a);
a = D(a, "utf8");
a = K("arguments", a);
if (!a.length) {
throw Error("XML file should contain root arguments element.");
}
const [{content:c, props:{name:b}}] = a;
a = A("arg", c).map(({content:d, props:{command:h, name:l, "boolean":m, number:g, multiple:k, "short":f, "default":e}}) => ({description:d.trim(), command:h, boolean:m, number:g, multiple:k, short:f, name:l, default:e}));
return {g:b, f:a};
const [{content:c, props:{name:d}}] = a;
a = K("arg", c).map(({content:b, props:{command:h, name:m, "boolean":l, number:g, multiple:k, "short":f, "default":e}}) => ({description:b.trim(), command:h, boolean:l, number:g, multiple:k, short:f, name:m, default:e}));
return {i:d, h:a};
};
try {
let a = "";
debugger;
const c = `import argufy from 'argufy'
${(Array.isArray(H) ? H : [H]).map(b => O(b)).reduce((b, {g:d = "", f:h}) => {
const {h:l, i:m} = N(h, a, d);
a = m;
b.push(l);
return b;
}, []).join("")}
function P() {
var a = A.map(c => O(c));
a = `import argufy from 'argufy'
${N(a)}
/**
* The additional arguments passed to the program.
*/
export const _argv = /** @type {!Array<string>} */ (${a}._argv)`;
I ? (v(I, c), console.log("Output saved to %s", I)) : console.log(c);
export const _argv = /** @type {!Array<string>} */ (${"args"}._argv)`;
B ? (F(B, a), console.log("Output saved to %s", B)) : null !== B && console.log(a);
}
;if (z.help) {
const a = y();
console.log(u({usage:a, description:"Generates the JavaScript file that exports all arguments\nbased on the configuration found in the arguments.xml file.\nhttps://artdecocode.com/argufy/", line:"argufy input [-o output] [-vh]", example:"argufy types/arguments.xml -o src/bin/get-args.js"}));
process.exit();
} else {
C && (console.log(require("../package.json").version), process.exit());
}
try {
P();
} catch (a) {
process.env.DEBUG ? console.log(a.stack) : console.log(a.message);
process.env.DEBUG ? console.log(a.stack) : console.log(a.message), process.exit(1);
}

@@ -238,0 +239,0 @@ ;

{
"name": "argufy",
"version": "1.7.3",
"version": "1.7.4",
"description": "Parses Command Line Arguments To Node.JS CLI Programs, Keeps Them In Arguments.xml File To Paste Into README Documentation And Generate Google Closure Compatible Exports.",

@@ -56,2 +56,3 @@ "main": "build",

"rexml": "^2.2.2",
"spawncommand": "^2.2.0",
"temp-context": "^2.1.3",

@@ -58,0 +59,0 @@ "usually": "^1.2.0",

@@ -45,4 +45,4 @@ # argufy

<arguments>
<arg command multiple name="input" default="types/arguments.xml">
The location of the `arguments.xml` file.
<arg command multiple name="inputs" default="types/arguments.xml">
The locations of `arguments.xml` files.
</arg>

@@ -76,4 +76,4 @@ <arg name="output" short="o">

export const argsConfig = {
'input': {
description: 'The location of the `arguments.xml` file.',
'inputs': {
description: 'The locations of `arguments.xml` files.',
command: true,

@@ -98,8 +98,9 @@ multiple: true,

}
const args = argufy(argsConfig)
/**
* The location of the `arguments.xml` file. Default `types/arguments.xml`.
* The locations of `arguments.xml` files. Default `types/arguments.xml`.
*/
export const _input = /** @type {(!Array<string>|string)} */ (args['input'] || 'types/arguments.xml')
export const _inputs = /** @type {!Array<string>} */ (args['inputs'] || ['types/arguments.xml'])

@@ -168,3 +169,3 @@ /**

input The location of the `arguments.xml` file.
inputs The locations of `arguments.xml` files.
Default: types/arguments.xml.

@@ -200,5 +201,5 @@ --output, -o The destination where to save output.

<tr>
<td>input</td>
<td>inputs</td>
<td></td>
<td>The location of the <code>arguments.xml</code> file. Default <code>types/arguments.xml</code>.</td>
<td>The locations of <code>arguments.xml</code> files. Default <code>types/arguments.xml</code>.</td>
</tr>

@@ -269,9 +270,3 @@ <tr>

}
const args = argufy(argsConfig)
/**
* The path to the source code.
*/
export const _source = /** @type {string} */ (args['source'])
export const argsConfigBundle = {

@@ -283,8 +278,14 @@ 'bundle': {

}
const argsBundle = argufy(argsConfigBundle, [process.argv[0], process.argv[1], ...args._argv])
const args = argufy({ ...argsConfig, ...argsConfigBundle })
/**
* The path to the source code.
*/
export const _source = /** @type {string} */ (args['source'])
/**
* Whether to bundle the source.
*/
export const _bundle = /** @type {boolean} */ (argsBundle['bundle'])
export const _bundle = /** @type {boolean} */ (args['bundle'])

@@ -294,3 +295,3 @@ /**

*/
export const _argv = /** @type {!Array<string>} */ (argsBundle._argv)
export const _argv = /** @type {!Array<string>} */ (args._argv)
```

@@ -392,3 +393,3 @@

```js
{ input: 'The location of the `arguments.xml` file.\nDefault: types/arguments.xml.',
{ inputs: 'The locations of `arguments.xml` files.\nDefault: types/arguments.xml.',
'--output, -o': 'The destination where to save output.\nIf not passed, prints to stdout.',

@@ -395,0 +396,0 @@ '--help, -h': 'Print the help information and exit.',

@@ -1,7 +0,5 @@

import { readFileSync, writeFileSync } from 'fs'
import rexml from 'rexml'
import usage from './usage'
import { reduceUsage } from './'
import { _input, _output, _help, _version, argsConfig } from './get-args'
import { makeExports } from './lib'
import { _inputs, _output, _help, _version, argsConfig } from './get-args'
import argufy from './commands/argufy'

@@ -13,71 +11,11 @@ if (_help) {

} else if (_version) {
console.log(require('../package.json')['version'])
console.log(require('../package.json').version)
process.exit()
}
/**
* Read the arguments.xml file and return flags.
* @param {string} input
*/
const extract = (input) => {
const xml = `${readFileSync(input)}`
const root = rexml('arguments', xml)
if (!root.length)
throw new Error('XML file should contain root arguments element.')
const [{ content, props: { 'name': argumentsName } }] = root
const allArgs = rexml('arg', content)
/** @type {!Array<!_argufy.Flag>} */
const args = allArgs.map(({ content: description, props: {
'command': command,
'name': name,
'boolean': boolean,
'number': number,
'multiple': multiple,
'short': short,
'default': def,
} }) => {
return {
description: description.trim(),
command, boolean, number, multiple, short, name, default: def,
}
})
return { argumentsName, args }
}
/**
* @param {string} allExports
* @param {string} lastVarArgsName
*/
const template = (allExports, lastVarArgsName) => {
return `import argufy from 'argufy'
${allExports}
/**
* The additional arguments passed to the program.
*/
export const _argv = /** @type {!Array<string>} */ (${lastVarArgsName}._argv)`
}
try {
const inputs = Array.isArray(_input) ? _input : [_input]
let lastVarArgsName = ''
const res = inputs.map((input) => {
const e = extract(input)
return e
}).reduce((acc, { argumentsName = '', args }) => {
const { string, varArgsName } = makeExports(args, lastVarArgsName, argumentsName)
lastVarArgsName = varArgsName
acc.push(string)
return acc
}, [])
const allExports = res.join('')
debugger
const t = template(allExports, lastVarArgsName)
if (_output) {
writeFileSync(_output, t)
console.log('Output saved to %s', _output)
} else console.log(t)
argufy(_inputs, _output)
} catch (err) {
process.env['DEBUG'] ? console.log(err.stack) : console.log(err.message)
process.exit(1)
}

@@ -87,3 +25,3 @@

* @suppress {nonStandardJsDocs}
* @typedef {import('.').Flag} _argufy.Flag
* @typedef {import('./').Flag} _argufy.Flag
*/
import argufy from './'
export const argsConfig = {
'input': {
description: 'The location of the `arguments.xml` file.',
'inputs': {
description: 'The locations of `arguments.xml` files.',
command: true,

@@ -28,5 +28,5 @@ multiple: true,

/**
* The location of the `arguments.xml` file. Default `types/arguments.xml`.
* The locations of `arguments.xml` files. Default `types/arguments.xml`.
*/
export const _input = /** @type {(!Array<string>|string)} */ (args['input'] || 'types/arguments.xml')
export const _inputs = /** @type {!Array<string>} */ (args['inputs'] || ['types/arguments.xml'])

@@ -33,0 +33,0 @@ /**

@@ -42,18 +42,19 @@ /** @param {!Array<_argufy.Flag>} flags */

/**
* @param {!Array<_argufy.Flag>} args
* @param {string} argsName
* @param {!Array<{ argumentsName: (string|undefined), args: !Array<!_argufy.Flag>}>} Args
*/
export const makeExports = (args, lastVarArgsName, argsName = '') => {
const obj = serialiseFlags(args)
const varConfigName = `argsConfig${argsName}`
const varArgsName = `args${argsName}`
const argExports = args.map(arg => makeExport(arg, varArgsName))
return { varArgsName, string: `
export const ${varConfigName} = ${obj}
const ${varArgsName} = argufy(${varConfigName}${
lastVarArgsName ? `, [process.argv[0], process.argv[1], ...${lastVarArgsName}._argv]` : ''
})
export const makeExports = (Args) => {
const exported = Args.map(({ argumentsName = '', args }) => {
const obj = serialiseFlags(args)
const varConfigName = `argsConfig${argumentsName}`
const argExports = args.map(arg => makeExport(arg))
return { varConfigName, constExport: `export const ${varConfigName} = ${obj}`, argExports }
})
const destr = exported.length > 1 ? `{ ${exported.map(({ varConfigName }) => `...${varConfigName}`).join(', ')} }` : exported[0].varConfigName
const a = `const args = argufy(${destr})`
${argExports.join('\n\n')}
` }
let s = exported.map(({ constExport }) => constExport).join('\n\n')
s += `\n\n${a}\n\n`
s += exported.map(({ argExports }) => argExports.join('\n\n')).join('\n\n')
return s
}

@@ -66,6 +67,7 @@

if (boolean) type = 'boolean'
if (multiple) type = '(!Array<string>|string)'
if (multiple) type = '!Array<string>'
const n = name.replace(/-(.)/g, (m, l) => l.toUpperCase())
let ddef = `'${def}'`
if (number || boolean) ddef = def
if (multiple) ddef = `[${ddef}]`
const hasDef = def !== undefined

@@ -72,0 +74,0 @@ // const ddef = number ? def :

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc