cli-engine-command
Advanced tools
Comparing version 9.0.0-ts.7 to 9.0.0-ts.8
@@ -1,6 +0,6 @@ | ||
import { InputFlags, InputArgs, OutputArgs, OutputFlags } from 'cli-flags'; | ||
import { InputArgs, InputFlags, OutputArgs, OutputFlags } from 'cli-flags'; | ||
import { Config, ConfigOptions, Plugin, ICommand } from 'cli-engine-config'; | ||
import { HTTP } from 'http-call'; | ||
import { deps } from './deps'; | ||
import { CLI } from 'cli-ux'; | ||
import color = require('./color'); | ||
export interface IMockOutput<T extends Command> { | ||
@@ -12,29 +12,28 @@ cmd: T; | ||
export declare class Command implements ICommand { | ||
topic?: string; | ||
command?: string; | ||
name?: string; | ||
description?: string; | ||
hidden: boolean; | ||
usage?: string; | ||
help?: string; | ||
aliases: string[]; | ||
_version: any; | ||
plugin?: Plugin; | ||
parse: { | ||
__config: { | ||
_version: string; | ||
id?: string; | ||
plugin?: Plugin; | ||
}; | ||
options: { | ||
argv?: string[]; | ||
flags?: InputFlags; | ||
args?: InputArgs; | ||
flags: InputFlags; | ||
strict?: boolean; | ||
description?: string; | ||
hidden?: boolean; | ||
usage?: string; | ||
help?: string; | ||
aliases?: string[]; | ||
}; | ||
readonly id: string; | ||
static mock<T extends Command>(...argv: string[]): Promise<IMockOutput<T>>; | ||
static run(config?: ConfigOptions): Promise<Command>; | ||
config: Config; | ||
http: typeof HTTP; | ||
cli: CLI; | ||
flags: OutputFlags<this['parse']['flags']>; | ||
flags: OutputFlags<this['options']['flags']>; | ||
argv: string[]; | ||
args: OutputArgs; | ||
color: typeof color.color; | ||
color: typeof deps.Color.color; | ||
constructor(config?: ConfigOptions); | ||
_run(): Promise<void>; | ||
_run(argv?: string[]): Promise<void>; | ||
init(): Promise<void>; | ||
@@ -41,0 +40,0 @@ run(): Promise<void>; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const cli_flags_1 = require("cli-flags"); | ||
const cli_engine_config_1 = require("cli-engine-config"); | ||
const http_call_1 = require("http-call"); | ||
const help_1 = require("./help"); | ||
const deps_1 = require("./deps"); | ||
const pjson = require('../package.json'); | ||
@@ -19,89 +8,76 @@ const debug = require('debug')('cli-engine-command'); | ||
constructor(config) { | ||
this.hidden = false; | ||
this.aliases = []; | ||
this._version = pjson.version; | ||
this.parse = { flags: {} }; | ||
this.config = cli_engine_config_1.buildConfig(config); | ||
this.__config = { _version: pjson.version }; | ||
this.options = {}; | ||
this.config = deps_1.deps.Config.buildConfig(config); | ||
const ctor = this.constructor; | ||
const props = [ | ||
['flags', 'flags'], | ||
['args', 'args'], | ||
['variableArgs', 'strict'], | ||
['description', 'description'], | ||
['hidden', 'hidden'], | ||
['usage', 'usage'], | ||
['help', 'help'], | ||
['aliases', 'aliases'], | ||
]; | ||
for (let [from, to] of props) { | ||
if (ctor[from]) { | ||
let cli = new deps_1.deps.CLI({ mock: this.config.mock }); | ||
cli.warn(`${from} is defined as a static property on ${this.__config.id}`); | ||
this.options[to] = ctor[from]; | ||
} | ||
} | ||
} | ||
get id() { | ||
if (this.name) | ||
return this.name; | ||
let cmd = []; | ||
if (this.topic) | ||
cmd.push(this.topic); | ||
if (this.command) | ||
cmd.push(this.command); | ||
let id = cmd.join(':'); | ||
if (id) | ||
return id; | ||
let ctor = this.constructor; | ||
return ctor.name; | ||
static async mock(...argv) { | ||
const cmd = new this({ argv: ['argv0', 'argv1'].concat(argv), mock: true }); | ||
await cmd._run(); | ||
return { | ||
cmd, | ||
stdout: cmd.cli.stdout.output, | ||
stderr: cmd.cli.stderr.output, | ||
}; | ||
} | ||
static mock(...argv) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
argv.unshift('argv0', 'argv1'); | ||
const cmd = (yield this.run({ argv, mock: true })); | ||
return { | ||
cmd, | ||
stdout: cmd.cli.stdout.output, | ||
stderr: cmd.cli.stderr.output, | ||
}; | ||
}); | ||
async _run(argv) { | ||
try { | ||
this.options.argv = argv; | ||
debug('initializing %s version: %s', this.__config.id, this.__config._version); | ||
await this.init(); | ||
debug('run'); | ||
await this.run(); | ||
debug('done'); | ||
await this.cli.done(); | ||
} | ||
catch (err) { | ||
this.cli.error(err); | ||
} | ||
} | ||
static run(config) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmd = new this(config); | ||
yield cmd._run(); | ||
return cmd; | ||
async init() { | ||
this.color = deps_1.deps.Color.color; | ||
const CLI = deps_1.deps.CLI; | ||
this.cli = new CLI({ debug: this.config.debug, mock: this.config.mock, errlog: this.config.errlog }); | ||
this.http = deps_1.deps.HTTP.defaults({ | ||
headers: { | ||
'user-agent': `${this.config.name}/${this.config.version} (${this.config.platform}-${this.config | ||
.arch}) node-${process.version}`, | ||
}, | ||
}); | ||
if (!this.config.mock) | ||
this.cli.handleUnhandleds(); | ||
this.options.argv = this.options.argv || this.config.argv.slice(2); | ||
debug('argv: %o', this.options.argv); | ||
const { argv, flags, args } = await deps_1.deps.CLIFlags.parse(this.options); | ||
this.flags = flags; | ||
this.argv = argv; | ||
this.args = args; | ||
} | ||
_run() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
debug('initializing %s version: %s', this.id, this._version); | ||
debug('argv: %o', this.config.argv); | ||
yield this.init(); | ||
debug('run'); | ||
yield this.run(); | ||
debug('done'); | ||
yield this.cli.done(); | ||
} | ||
catch (err) { | ||
this.cli.error(err); | ||
} | ||
}); | ||
async run() { } | ||
async done() { | ||
this.cli.done(); | ||
} | ||
init() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.color = require('./color').color; | ||
const { CLI } = require('cli-ux'); | ||
this.cli = new CLI({ debug: this.config.debug, mock: this.config.mock, errlog: this.config.errlog }); | ||
this.http = http_call_1.HTTP.defaults({ | ||
headers: { | ||
'user-agent': `${this.config.name}/${this.config.version} (${this.config.platform}-${this.config | ||
.arch}) node-${process.version}`, | ||
}, | ||
}); | ||
if (!this.config.mock) | ||
this.cli.handleUnhandleds(); | ||
const { argv, flags, args } = yield cli_flags_1.parse(Object.assign({}, this.parse, { argv: this.config.argv.slice(2) })); | ||
this.flags = flags; | ||
this.argv = argv; | ||
this.args = args; | ||
}); | ||
} | ||
run() { | ||
return __awaiter(this, void 0, void 0, function* () { }); | ||
} | ||
done() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.cli.done(); | ||
}); | ||
} | ||
buildHelp() { | ||
let help = new help_1.Help(this.config); | ||
let help = new deps_1.deps.Help(this.config); | ||
return help.command(this); | ||
} | ||
buildHelpLine() { | ||
let help = new help_1.Help(this.config); | ||
let help = new deps_1.deps.Help(this.config); | ||
return help.commandLine(this); | ||
@@ -108,0 +84,0 @@ } |
@@ -9,4 +9,4 @@ import { Config, ICommand } from 'cli-engine-config'; | ||
renderAliases(aliases: string[] | undefined): string; | ||
renderArgs(args: IArg[]): string; | ||
renderFlags(flags: [string, IFlag<any>][]): string; | ||
renderArgs(args: IArg<string>[]): string; | ||
renderFlags(flags: IFlag<any>[]): string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const list_1 = require("cli-ux/lib/list"); | ||
const cli_flags_1 = require("cli-flags"); | ||
const chalk = require("chalk"); | ||
function buildUsage(command) { | ||
if (command.usage) | ||
return command.usage.trim(); | ||
let cmd = command.id; | ||
if (!command.parse.args) | ||
return cmd.trim(); | ||
let args = command.parse.args.map(renderArg); | ||
if (command.options.usage) | ||
return command.options.usage.trim(); | ||
let cmd = command.__config.id; | ||
if (!command.options.args) | ||
return (cmd || '').trim(); | ||
let args = command.options.args.map(renderArg); | ||
return `${cmd} ${args.join(' ')}`.trim(); | ||
@@ -16,3 +17,3 @@ } | ||
let name = arg.name.toUpperCase(); | ||
if (arg.required !== false && arg.optional !== true) | ||
if (arg.required) | ||
return `${name}`; | ||
@@ -27,4 +28,9 @@ else | ||
command(cmd) { | ||
let flags = Object.entries(cmd.parse.flags || {}).filter(([, flag]) => !flag.hidden); | ||
let args = (cmd.parse.args || []).filter(a => !a.hidden); | ||
let flags = Object.entries(cmd.options.flags || {}) | ||
.filter(([, f]) => !f.hidden) | ||
.map(([k, f]) => { | ||
f.name = k; | ||
return f; | ||
}); | ||
let args = (cmd.options.args || []).filter(a => !a.hidden); | ||
let hasFlags = flags.length ? ` ${chalk.blue('[flags]')}` : ''; | ||
@@ -34,11 +40,11 @@ let usage = `${chalk.bold('Usage:')} ${this.config.bin} ${buildUsage(cmd)}${hasFlags}\n`; | ||
usage, | ||
cmd.description ? `\n${chalk.bold(cmd.description.trim())}\n` : '', | ||
this.renderAliases(cmd.aliases), | ||
cmd.options.description ? `\n${chalk.bold(cmd.options.description.trim())}\n` : '', | ||
this.renderAliases(cmd.options.aliases), | ||
this.renderArgs(args), | ||
this.renderFlags(flags), | ||
cmd.help ? `\n${cmd.help.trim()}\n` : '', | ||
cmd.options.help ? `\n${cmd.options.help.trim()}\n` : '', | ||
].join(''); | ||
} | ||
commandLine(cmd) { | ||
return [buildUsage(cmd), cmd.description ? chalk.dim(cmd.description) : null]; | ||
return [buildUsage(cmd), cmd.options.description ? chalk.dim(cmd.options.description) : null]; | ||
} | ||
@@ -63,27 +69,5 @@ renderAliases(aliases) { | ||
return ''; | ||
flags.sort((a, b) => { | ||
if (a[1].char && !b[1].char) | ||
return -1; | ||
if (b[1].char && !a[1].char) | ||
return 1; | ||
if (a[0] < b[0]) | ||
return -1; | ||
return b[0] < a[0] ? 1 : 0; | ||
}); | ||
return (`\n${chalk.blue('Flags:')}\n` + | ||
list_1.renderList(flags.map(([name, f]) => { | ||
let label = []; | ||
if (f.char) | ||
label.push(`-${f.char}`); | ||
if (name) | ||
label.push(` --${name}`); | ||
let usage = f.type === 'option' ? ` ${name.toUpperCase()}` : ''; | ||
let description = f.description || ''; | ||
if (f.required) | ||
description = `(required) ${description}`; | ||
return [` ${label.join(',').trim()}` + usage, description ? chalk.dim(description) : null]; | ||
})) + | ||
'\n'); | ||
return `\n${chalk.blue('Flags:')}\n` + list_1.renderList(cli_flags_1.flagUsages(flags)) + '\n'; | ||
} | ||
} | ||
exports.Help = Help; |
{ | ||
"name": "cli-engine-command", | ||
"description": "base CLI command for cli-engine", | ||
"version": "9.0.0-ts.7", | ||
"version": "9.0.0-ts.8", | ||
"author": "Jeff Dickey @dickeyxxx", | ||
@@ -9,7 +9,8 @@ "bugs": "https://github.com/heroku/cli-engine-command/issues", | ||
"chalk": "^2.1.0", | ||
"cli-engine-config": "^4.0.1-ts.11", | ||
"cli-flags": "^1.0.10", | ||
"cli-ux": "^1.1.7", | ||
"cli-engine-config": "^4.0.1-ts.13", | ||
"cli-flags": "^1.0.14", | ||
"cli-ux": "^1.1.8", | ||
"fs-extra": "^4.0.2", | ||
"http-call": "^4.0.0-ts2" | ||
"http-call": "^4.0.0-ts2.0", | ||
"tslib": "^1.7.1" | ||
}, | ||
@@ -24,8 +25,8 @@ "devDependencies": { | ||
"husky": "^0.14.3", | ||
"jest": "^21.0.2", | ||
"lint-staged": "^4.1.3", | ||
"jest": "^21.1.0", | ||
"lint-staged": "^4.2.1", | ||
"nock": "^9.0.14", | ||
"prettier": "^1.6.1", | ||
"prettier": "^1.7.0", | ||
"remap-istanbul": "^0.9.5", | ||
"ts-jest": "^21.0.0", | ||
"ts-jest": "^21.0.1", | ||
"typescript": "^2.5.2" | ||
@@ -32,0 +33,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
12
10863
7
269
2
6
+ Addedtslib@^1.7.1
Updatedcli-flags@^1.0.14
Updatedcli-ux@^1.1.8
Updatedhttp-call@^4.0.0-ts2.0