Socket
Socket
Sign inDemoInstall

cac

Package Overview
Dependencies
0
Maintainers
3
Versions
120
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 6.2.6 to 6.3.0

740

dist/index.js

@@ -1,7 +0,739 @@

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var events = _interopDefault(require('events'));
var path = _interopDefault(require('path'));
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
function unwrapExports (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x.default : x;
}
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
const EQQ = /\s|=/;
const FLAG = /^-{1,2}/;
const PREFIX = /^--no-/i;
function isBool(any) {
return typeof any === 'boolean';
}
function toArr(any) {
return Array.isArray(any) ? any : any == null ? [] : [any];
}
function toString(any) {
return any == null || any === true ? '' : String(any);
}
function toBool(any) {
return any === 'false' ? false : Boolean(any);
}
function toNum(any) {
let x = Number(any);
return !isBool(any) && (x * 0 === 0) ? x : any;
}
function getAlibi(names, arr) {
if (arr.length === 0) return arr;
let k, i = 0, len = arr.length, vals = [];
for (; i < len; i++) {
k = arr[i];
vals.push(k);
if (names[k] !== void 0) {
vals = vals.concat(names[k]);
}
}
return vals;
}
function typecast(key, val, strings, booleans) {
if (strings.indexOf(key) !== -1) return toString(val);
if (booleans.indexOf(key) !== -1) return toBool(val);
return toNum(val);
}
var lib = function(args, opts) {
args = args || [];
opts = opts || {};
opts.string = toArr(opts.string);
opts.boolean = toArr(opts.boolean);
const aliases = {};
let k, i, j, x, y, len, type;
if (opts.alias !== void 0) {
for (k in opts.alias) {
aliases[k] = toArr(opts.alias[k]);
len = aliases[k].length; // save length
for (i = 0; i < len; i++) {
x = aliases[k][i]; // alias's key name
aliases[x] = [k]; // set initial array
for (j = 0; j < len; j++) {
if (x !== aliases[k][j]) {
aliases[x].push(aliases[k][j]);
}
}
}
}
}
if (opts.default !== void 0) {
for (k in opts.default) {
type = typeof opts.default[k];
opts[type] = (opts[type] || []).concat(k);
}
}
// apply to all aliases
opts.string = getAlibi(aliases, opts.string);
opts.boolean = getAlibi(aliases, opts.boolean);
let idx = 0;
const out = { _: [] };
while (args[idx] !== void 0) {
let incr = 1;
const val = args[idx];
if (val === '--') {
out._ = out._.concat(args.slice(idx + 1));
break;
} else if (!FLAG.test(val)) {
out._.push(val);
} else if (PREFIX.test(val)) {
out[val.replace(PREFIX, '')] = false;
} else {
let tmp;
const segs = val.split(EQQ);
const isGroup = segs[0].charCodeAt(1) !== 45; // '-'
const flag = segs[0].substr(isGroup ? 1 : 2);
len = flag.length;
const key = isGroup ? flag[len - 1] : flag;
if (opts.unknown !== void 0 && aliases[key] === void 0) {
return opts.unknown(segs[0]);
}
if (segs.length > 1) {
tmp = segs[1];
} else {
tmp = args[idx + 1] || true;
FLAG.test(tmp) ? (tmp = true) : (incr = 2);
}
if (isGroup && len > 1) {
for (i = len - 1; i--; ) {
k = flag[i]; // all but last key
out[k] = typecast(k, true, opts.string, opts.boolean);
}
}
const value = typecast(key, tmp, opts.string, opts.boolean);
out[key] = out[key] !== void 0 ? toArr(out[key]).concat(value) : value;
// handle discarded args when dealing with booleans
if (isBool(value) && !isBool(tmp) && tmp !== 'true' && tmp !== 'false') {
out._.push(tmp);
}
}
idx += incr;
}
if (opts.default !== void 0) {
for (k in opts.default) {
if (out[k] === void 0) {
out[k] = opts.default[k];
}
}
}
for (k in out) {
if (aliases[k] === void 0) continue;
y = out[k];
len = aliases[k].length;
for (i = 0; i < len; i++) {
out[aliases[k][i]] = y; // assign value
}
}
return out;
};
var utils = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeBrackets = (v) => v.replace(/[<[].+/, '').trim();
exports.findAllBrackets = (v) => {
const ANGLED_BRACKET_RE_GLOBAL = /<([^>]+)>/g;
const SQUARE_BRACKET_RE_GLOBAL = /\[([^\]]+)\]/g;
const res = [];
const parse = (match) => {
let variadic = false;
let value = match[1];
if (value.startsWith('...')) {
value = value.slice(3);
variadic = true;
}
return {
required: match[0].startsWith('<'),
value,
variadic
};
};
let angledMatch;
while ((angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v))) {
res.push(parse(angledMatch));
}
let squareMatch;
while ((squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v))) {
res.push(parse(squareMatch));
}
return res;
};
exports.getMriOptions = (globalCommand, subCommand) => {
const options = [
...globalCommand.options,
...(subCommand ? subCommand.options : [])
];
const ignoreDefault = subCommand && subCommand.config.ignoreOptionDefaultValue
? subCommand.config.ignoreOptionDefaultValue
: globalCommand.config.ignoreOptionDefaultValue;
return {
default: ignoreDefault
? {}
: options.reduce((res, option) => {
if (option.config.default !== undefined) {
// Only need to set the default value of the first name
// Since mri will automatically do the rest for alias names
res[option.names[0]] = option.config.default;
}
return res;
}, {}),
boolean: options
.filter(option => option.isBoolean)
.reduce((res, option) => {
return res.concat(option.names);
}, []),
alias: options.reduce((res, option) => {
if (option.names.length > 1) {
res[option.names[0]] = option.names.slice(1);
}
return res;
}, {})
};
};
exports.findLongest = (arr) => {
return arr.sort((a, b) => {
return a.length > b.length ? -1 : 1;
})[0];
};
exports.padRight = (str, length) => {
return str.length >= length ? str : `${str}${' '.repeat(length - str.length)}`;
};
exports.camelcase = (input) => {
return input.replace(/([a-z])-([a-z])/g, (_, p1, p2) => {
return p1 + p2.toUpperCase();
});
};
exports.setDotProp = (obj, keys, val) => {
let i = 0;
let length = keys.length;
let t = obj;
let x;
for (; i < length; ++i) {
x = t[keys[i]];
t = t[keys[i]] =
i === length - 1
? val
: x != null
? x
: !!~keys[i + 1].indexOf('.') || !(+keys[i + 1] > -1)
? {}
: [];
}
};
});
unwrapExports(utils);
var utils_1 = utils.removeBrackets;
var utils_2 = utils.findAllBrackets;
var utils_3 = utils.getMriOptions;
var utils_4 = utils.findLongest;
var utils_5 = utils.padRight;
var utils_6 = utils.camelcase;
var utils_7 = utils.setDotProp;
var Option_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
class Option {
constructor(rawName, description, config) {
this.rawName = rawName;
this.description = description;
this.config = Object.assign({}, config);
let negated = false;
this.names = utils.removeBrackets(rawName)
.split(',')
.map((v) => {
let name = v.trim().replace(/^-{1,2}/, '');
if (name.startsWith('no-')) {
negated = true;
name = name.replace(/^no-/, '');
}
return name;
});
if (negated) {
this.config.default = true;
}
if (rawName.includes('<')) {
this.required = true;
}
else if (rawName.includes('[')) {
this.required = false;
}
else {
// No arg needed, it's boolean flag
this.isBoolean = true;
}
}
}
exports.default = Option;
});
unwrapExports(Option_1);
var Command_1 = createCommonjsModule(function (module, exports) {
var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const CAC_1 = __importDefault(require("./CAC"));
const cac = () => new CAC_1.default();
Object.defineProperty(exports, "__esModule", { value: true });
const Option_1$$1 = __importDefault(Option_1);
class Command {
constructor(rawName, description, config = {}, cli) {
this.rawName = rawName;
this.description = description;
this.config = config;
this.cli = cli;
this.options = [];
this.aliasNames = [];
this.name = utils.removeBrackets(rawName);
this.args = utils.findAllBrackets(rawName);
this.examples = [];
}
usage(text) {
this.usageText = text;
return this;
}
allowUnknownOptions() {
this.config.allowUnknownOptions = true;
return this;
}
ignoreOptionDefaultValue() {
this.config.ignoreOptionDefaultValue = true;
return this;
}
version(version, customFlags = '-v, --version') {
this.versionNumber = version;
this.option(customFlags, 'Display version number');
return this;
}
example(example) {
this.examples.push(example);
return this;
}
/**
* Add a option for this command
* @param rawName Raw option name(s)
* @param description Option description
* @param config Option config
*/
option(rawName, description, config) {
const option = new Option_1$$1.default(rawName, description, config);
this.options.push(option);
return this;
}
alias(name) {
this.aliasNames.push(name);
return this;
}
action(callback) {
this.commandAction = callback;
return this;
}
/**
* Check if a command name is matched by this command
* @param name Command name
*/
isMatched(name) {
return this.name === name || this.aliasNames.includes(name);
}
get isDefaultCommand() {
return this.name === '' || this.aliasNames.includes('!');
}
get isGlobalCommand() {
return this instanceof GlobalCommand;
}
/**
* Check if an option is registered in this command
* @param name Option name
*/
hasOption(name) {
name = name.split('.')[0];
return this.options.find(option => {
return option.names.includes(name);
});
}
outputHelp() {
const { bin, commands } = this.cli;
const { versionNumber, options: globalOptions, helpCallback } = this.cli.globalCommand;
const sections = [
{
body: `${bin}${versionNumber ? ` v${versionNumber}` : ''}`
}
];
sections.push({
title: 'Usage',
body: ` $ ${bin} ${this.usageText || this.rawName}`
});
const showCommands = (this.isGlobalCommand || this.isDefaultCommand) && commands.length > 0;
if (showCommands) {
const longestCommandName = utils.findLongest(commands.map(command => command.rawName));
sections.push({
title: 'Commands',
body: commands
.map(command => {
return ` ${utils.padRight(command.rawName, longestCommandName.length)} ${command.description}`;
})
.join('\n')
});
sections.push({
title: `For more info, run any command with the \`--help\` flag`,
body: commands
.map(command => ` $ ${bin}${command.name === '' ? '' : ` ${command.name}`} --help`)
.join('\n')
});
}
const options = this.isGlobalCommand
? globalOptions
: [...this.options, ...(globalOptions || [])];
if (options.length > 0) {
const longestOptionName = utils.findLongest(options.map(option => option.rawName));
sections.push({
title: 'Options',
body: options
.map(option => {
return ` ${utils.padRight(option.rawName, longestOptionName.length)} ${option.description} ${option.config.default === undefined
? ''
: `(default: ${option.config.default})`}`;
})
.join('\n')
});
}
if (this.examples.length > 0) {
sections.push({
title: 'Examples',
body: this.examples
.map(example => {
if (typeof example === 'function') {
return example(bin);
}
return example;
})
.join('\n')
});
}
if (helpCallback) {
helpCallback(sections);
}
console.log(sections
.map(section => {
return section.title
? `${section.title}:\n${section.body}`
: section.body;
})
.join('\n\n'));
process.exit(0);
}
outputVersion() {
const { bin } = this.cli;
const { versionNumber } = this.cli.globalCommand;
if (versionNumber) {
console.log(`${bin}/${versionNumber} ${process.platform}-${process.arch} node-${process.version}`);
}
process.exit(0);
}
/**
* Check if the parsed options contain any unknown options
*
* Exit and output error when true
*/
checkUnknownOptions() {
const { rawOptions, globalCommand } = this.cli;
if (!this.config.allowUnknownOptions) {
for (const name of Object.keys(rawOptions)) {
if (name !== '--' &&
!this.hasOption(name) &&
!globalCommand.hasOption(name)) {
console.error(`error: Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``);
process.exit(1);
}
}
}
}
/**
* Check if the required string-type options exist
*/
checkRequiredOptions() {
const { rawOptions, globalCommand } = this.cli;
const requiredOptions = [...globalCommand.options, ...this.options].filter(option => option.required);
for (const option of requiredOptions) {
const value = rawOptions[option.names[0].split('.')[0]];
if (typeof value === 'boolean') {
console.error(`error: option \`${option.rawName}\` argument is missing`);
process.exit(1);
}
}
}
}
class GlobalCommand extends Command {
constructor(cli) {
super('@@global@@', '', {}, cli);
}
}
exports.GlobalCommand = GlobalCommand;
exports.default = Command;
});
unwrapExports(Command_1);
var Command_2 = Command_1.GlobalCommand;
var CAC_1 = createCommonjsModule(function (module, exports) {
var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(path);
const mri_1 = __importDefault(lib);
const Command_1$$1 = __importStar(Command_1);
class CAC extends events.EventEmitter {
constructor() {
super();
this.commands = [];
this.globalCommand = new Command_1$$1.GlobalCommand(this);
this.globalCommand.usage('<command> [options]');
}
/**
* Add a global usage text.
*
* This is not used by sub-commands.
*/
usage(text) {
this.globalCommand.usage(text);
return this;
}
/**
* Add a sub-command
*/
command(rawName, description, config) {
const command = new Command_1$$1.default(rawName, description, config, this);
command.globalCommand = this.globalCommand;
this.commands.push(command);
return command;
}
/**
* Add a global CLI option.
*
* Which is also applied to sub-commands.
*/
option(rawName, description, config) {
this.globalCommand.option(rawName, description, config);
return this;
}
/**
* Show help message when `-h, --help` flags appear.
*
*/
help(callback) {
this.globalCommand.option('-h, --help', 'Display this message');
this.globalCommand.helpCallback = callback;
return this;
}
/**
* Show version number when `-v, --version` flags appear.
*
*/
version(version, customFlags = '-v, --version') {
this.globalCommand.version(version, customFlags);
return this;
}
/**
* Add a global example.
*
* This example added here will not be used by sub-commands.
*/
example(example) {
this.globalCommand.example(example);
return this;
}
/**
* Output the global help message
*
* This will also call `process.exit(0)` to quit the process.
*/
outputHelp() {
this.globalCommand.outputHelp();
}
/**
* Output the version number.
*
* This will also call `process.exit(0)` to quit the process.
*/
outputVersion() {
this.globalCommand.outputVersion();
}
setParsedInfo({ args, options, rawOptions }, matchedCommand) {
this.args = args;
this.options = options;
this.rawOptions = rawOptions;
if (matchedCommand) {
this.matchedCommand = matchedCommand;
}
return this;
}
/**
* Parse argv and run command action if found.
*/
parse(argv = process.argv) {
this.rawArgs = argv;
this.bin = argv[1] ? path_1.default.basename(argv[1]) : 'cli';
let shouldParse = true;
// Search sub-commands
for (const command of this.commands) {
const mriOptions = utils.getMriOptions(this.globalCommand, command);
const mriResult = this.mri(argv.slice(2), mriOptions);
const commandName = mriResult.args[0];
if (command.isMatched(commandName)) {
shouldParse = false;
const parsedInfo = Object.assign({}, mriResult, { args: mriResult.args.slice(1) });
this.setParsedInfo(parsedInfo, command);
this.emit(`command:${commandName}`, command);
}
}
if (shouldParse) {
// Search the default command
for (const command of this.commands) {
if (command.name === '') {
shouldParse = false;
const mriOptions = utils.getMriOptions(this.globalCommand, command);
const mriResult = this.mri(argv.slice(2), mriOptions);
this.setParsedInfo(mriResult, command);
this.emit(`command:!`, command);
}
}
}
if (shouldParse) {
const globalMriOptions = utils.getMriOptions(this.globalCommand);
const mriResult = this.mri(argv.slice(2), globalMriOptions);
this.setParsedInfo(mriResult);
}
if (this.options.help && this.globalCommand.hasOption('help')) {
this.outputHelp();
}
if (this.options.version &&
this.globalCommand.hasOption('version') &&
this.globalCommand.versionNumber) {
this.outputVersion();
}
const parsedArgv = { args: this.args, options: this.options };
if (this.matchedCommand) {
this.runCommandAction(this.matchedCommand, parsedArgv);
}
else {
this.emit('command:*');
}
return parsedArgv;
}
mri(argv, mriOptions) {
let argsAfterDoubleDashes = [];
const doubleDashesIndex = argv.indexOf('--');
if (doubleDashesIndex > -1) {
argsAfterDoubleDashes = argv.slice(0, doubleDashesIndex);
argv = argv.slice(doubleDashesIndex + 1);
}
const parsed = mri_1.default(argv, mriOptions);
const args = parsed._;
delete parsed._;
const options = {
'--': argsAfterDoubleDashes
};
for (const key of Object.keys(parsed)) {
const keys = key.split('.').map((v, i) => {
return i === 0 ? utils.camelcase(v) : v;
});
utils.setDotProp(options, keys, parsed[key]);
}
return {
args,
options,
rawOptions: parsed
};
}
runCommandAction(command, { args, options }) {
if (!command.commandAction)
return;
command.checkUnknownOptions();
command.checkRequiredOptions();
const minimalArgsCount = command.args.filter(arg => arg.required).length;
if (args.length < minimalArgsCount) {
console.error(`error: missing required args for command \`${command.rawName}\``);
process.exit(1);
}
const actionArgs = [];
command.args.forEach((arg, index) => {
if (arg.variadic) {
actionArgs.push(args.slice(index));
}
else {
actionArgs.push(args[index]);
}
});
actionArgs.push(options);
return command.commandAction.apply(this, actionArgs);
}
}
exports.default = CAC;
});
unwrapExports(CAC_1);
var lib$1 = createCommonjsModule(function (module) {
var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const CAC_1$$1 = __importDefault(CAC_1);
const cac = () => new CAC_1$$1.default();
module.exports = cac;
});
var index = unwrapExports(lib$1);
module.exports = index;

10

package.json
{
"name": "cac",
"version": "6.2.6",
"version": "6.3.0",
"description": "Simple yet powerful framework for building command-line apps.",

@@ -18,3 +18,3 @@ "repository": {

"test": "jest",
"build": "tsc",
"build": "tsc && bili",
"toc": "markdown-toc -i README.md",

@@ -29,2 +29,3 @@ "prepublishOnly": "npm run build"

"@types/mri": "^1.1.0",
"bili": "^3.4.2",
"cz-conventional-changelog": "^2.1.0",

@@ -37,2 +38,3 @@ "eslint-config-rem": "^3.0.0",

"markdown-toc": "^1.2.0",
"mri": "^1.1.1",
"prettier": "^1.15.2",

@@ -44,5 +46,3 @@ "semantic-release": "^15.12.1",

},
"dependencies": {
"mri": "^1.1.1"
},
"dependencies": {},
"release": {

@@ -49,0 +49,0 @@ "branch": "master"

@@ -230,2 +230,6 @@ <img width="945" alt="2017-07-26 9 27 05" src="https://user-images.githubusercontent.com/8784712/28623641-373450f4-7249-11e7-854d-1b076dab274d.png">

**๐Ÿ’ Check out [the generated docs](https://cac-api-doc.egoist.sh/modules/_index_.html) from source code if you want a more in-depth API references.**
Below is a brief overview.
### CLI Instance

@@ -232,0 +236,0 @@

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with โšก๏ธ by Socket Inc