@oclif/core
Advanced tools
Comparing version 0.5.12 to 0.5.13
@@ -5,2 +5,4 @@ # Changelog | ||
### [0.5.13](https://github.com/oclif/core/compare/v0.5.12...v0.5.13) (2021-06-09) | ||
### [0.5.12](https://github.com/oclif/core/compare/v0.5.11...v0.5.12) (2021-06-07) | ||
@@ -7,0 +9,0 @@ |
@@ -29,4 +29,2 @@ import * as Interfaces from './interfaces'; | ||
static parse: boolean; | ||
/** A hash of flags for the command */ | ||
static flags?: Interfaces.FlagInput<any>; | ||
/** An order-dependent array of arguments for the command */ | ||
@@ -38,2 +36,3 @@ static args?: Interfaces.ArgInput; | ||
static parserOptions: {}; | ||
static disableJsonFlag: boolean | undefined; | ||
/** | ||
@@ -46,2 +45,7 @@ * instantiate and run the command | ||
static run: Interfaces.Command.Class['run']; | ||
/** A hash of flags for the command */ | ||
private static _flags; | ||
private static globalFlags; | ||
static get flags(): Interfaces.FlagInput<any>; | ||
static set flags(flags: Interfaces.FlagInput<any>); | ||
id: string | undefined; | ||
@@ -63,2 +67,3 @@ protected debug: (...args: any[]) => void; | ||
log(message?: string, ...args: any[]): void; | ||
jsonEnabled(): boolean; | ||
/** | ||
@@ -74,2 +79,4 @@ * actual command run code goes here | ||
protected finally(_: Error | undefined): Promise<any>; | ||
protected toSuccessJson<T>(result: T): T; | ||
protected toErrorJson(err: any): any; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const url_1 = require("url"); | ||
const settings_1 = require("./settings"); | ||
const util_1 = require("util"); | ||
const cli_ux_1 = require("cli-ux"); | ||
const config_1 = require("./config"); | ||
const Errors = require("./errors"); | ||
const Parser = require("./parser"); | ||
const Flags = require("./flags"); | ||
const pjson = require('../package.json'); | ||
@@ -34,2 +37,8 @@ /** | ||
} | ||
static get flags() { | ||
return this._flags; | ||
} | ||
static set flags(flags) { | ||
this._flags = this.disableJsonFlag || settings_1.settings.disableJsonFlag ? flags : Object.assign({}, Command.globalFlags, flags); | ||
} | ||
get ctor() { | ||
@@ -40,2 +49,3 @@ return this.constructor; | ||
let err; | ||
let result; | ||
try { | ||
@@ -45,3 +55,3 @@ // remove redirected env var to allow subsessions to run autoupdated client | ||
await this.init(); | ||
return await this.run(); | ||
result = await this.run(); | ||
} | ||
@@ -55,2 +65,6 @@ catch (error) { | ||
} | ||
if (result && this.jsonEnabled()) { | ||
cli_ux_1.cli.styledJSON(this.toSuccessJson(result)); | ||
} | ||
return result; | ||
} | ||
@@ -67,6 +81,11 @@ exit(code = 0) { | ||
log(message = '', ...args) { | ||
// tslint:disable-next-line strict-type-predicates | ||
message = typeof message === 'string' ? message : util_1.inspect(message); | ||
process.stdout.write(util_1.format(message, ...args) + '\n'); | ||
if (!this.jsonEnabled()) { | ||
// tslint:disable-next-line strict-type-predicates | ||
message = typeof message === 'string' ? message : util_1.inspect(message); | ||
process.stdout.write(util_1.format(message, ...args) + '\n'); | ||
} | ||
} | ||
jsonEnabled() { | ||
return this.argv.includes('--json'); | ||
} | ||
async init() { | ||
@@ -89,14 +108,22 @@ this.debug('init version: %s argv: %o', this.ctor._base, this.argv); | ||
options = this.constructor; | ||
return Parser.parse(argv, Object.assign({ context: this }, options)); | ||
const opts = Object.assign({ context: this }, options); | ||
// the spread operator doesn't work with getters so we have to manually add it here | ||
opts.flags = options === null || options === void 0 ? void 0 : options.flags; | ||
return Parser.parse(argv, opts); | ||
} | ||
async catch(err) { | ||
if (!err.message) | ||
if (this.jsonEnabled()) { | ||
cli_ux_1.cli.styledJSON(this.toErrorJson(err)); | ||
} | ||
else { | ||
if (!err.message) | ||
throw err; | ||
try { | ||
const { cli } = require('cli-ux'); | ||
const chalk = require('chalk'); // eslint-disable-line node/no-extraneous-require | ||
cli.action.stop(chalk.bold.red('!')); | ||
} | ||
catch (_a) { } | ||
throw err; | ||
try { | ||
const { cli } = require('cli-ux'); | ||
const chalk = require('chalk'); // eslint-disable-line node/no-extraneous-require | ||
cli.action.stop(chalk.bold.red('!')); | ||
} | ||
catch (_a) { } | ||
throw err; | ||
} | ||
@@ -114,2 +141,8 @@ async finally(_) { | ||
} | ||
toSuccessJson(result) { | ||
return result; | ||
} | ||
toErrorJson(err) { | ||
return { error: err }; | ||
} | ||
} | ||
@@ -142,1 +175,6 @@ exports.default = Command; | ||
}; | ||
Command.globalFlags = { | ||
json: Flags.boolean({ | ||
description: 'format output as json', | ||
}), | ||
}; |
@@ -45,3 +45,3 @@ import { Options, Plugin as IPlugin } from '../interfaces/plugin'; | ||
runHook<T>(event: string, opts: T): Promise<void>; | ||
runCommand(id: string, argv?: string[], cachedCommand?: Command.Plugin): Promise<void>; | ||
runCommand<T = unknown>(id: string, argv?: string[], cachedCommand?: Command.Plugin): Promise<T>; | ||
scopedEnvVar(k: string): string | undefined; | ||
@@ -48,0 +48,0 @@ scopedEnvVarTrue(k: string): boolean; |
@@ -35,3 +35,3 @@ "use strict"; | ||
} | ||
static async load(opts = (module.parent && module.parent && module.parent.parent && module.parent.parent.filename) || __dirname) { | ||
static async load(opts = (module.parent && module.parent.parent && module.parent.parent.filename) || __dirname) { | ||
// Handle the case when a file URL string is passed in such as 'import.meta.url'; covert to file path. | ||
@@ -186,4 +186,5 @@ if (typeof opts === 'string' && opts.startsWith('file://')) { | ||
await this.runHook('prerun', { Command: command, argv }); | ||
const result = await command.run(argv, this); | ||
const result = (await command.run(argv, this)); | ||
await this.runHook('postrun', { Command: command, result: result, argv }); | ||
return result; | ||
} | ||
@@ -190,0 +191,0 @@ scopedEnvVar(k) { |
@@ -93,3 +93,4 @@ import { PJSON } from './pjson'; | ||
readonly commandIDs: string[]; | ||
runCommand(id: string, argv?: string[]): Promise<void>; | ||
runCommand<T = unknown>(id: string, argv?: string[]): Promise<T>; | ||
runCommand<T = unknown>(id: string, argv?: string[], cachedCommand?: Command.Plugin): Promise<T>; | ||
runHook<T extends Hooks, K extends Extract<keyof T, string>>(event: K, opts: T[K]): Promise<void>; | ||
@@ -96,0 +97,0 @@ findCommand(id: string, opts: { |
@@ -50,3 +50,5 @@ import { AlphabetLowercase, AlphabetUppercase } from './alphabet'; | ||
export declare type ParserOutput<TFlags extends OutputFlags<any>, TArgs extends OutputArgs<any>> = { | ||
flags: TFlags; | ||
flags: TFlags & { | ||
json: boolean | undefined; | ||
}; | ||
args: TArgs; | ||
@@ -53,0 +55,0 @@ argv: string[]; |
import * as Interfaces from './interfaces'; | ||
export declare function run(argv?: string[], options?: Interfaces.LoadOptions): Promise<void>; | ||
export declare function run(argv?: string[], options?: Interfaces.LoadOptions): Promise<unknown>; |
@@ -33,3 +33,7 @@ export declare type Settings = { | ||
tsnodeEnabled?: boolean; | ||
/** | ||
* Disable the --json flag for all commands | ||
*/ | ||
disableJsonFlag?: boolean; | ||
}; | ||
export declare const settings: Settings; |
{ | ||
"name": "@oclif/core", | ||
"description": "base library for oclif CLIs", | ||
"version": "0.5.12", | ||
"version": "0.5.13", | ||
"author": "Jeff Dickey @jdxcode", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/oclif/core/issues", |
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
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
166402
4339