Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: add an option to throw instead or warn on manifest create
Browse files Browse the repository at this point in the history
  • Loading branch information
amphro committed Mar 15, 2018
1 parent 5cf0ea3 commit 573c26e
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export interface Options {
name?: string
type?: string
tag?: string
ignoreManifest?: boolean
ignoreManifest?: boolean,
errorOnManifestCreate?: boolean
}

export interface IPlugin {
Expand Down Expand Up @@ -108,7 +109,7 @@ export class Plugin implements IPlugin {

this.hooks = mapValues(this.pjson.oclif.hooks || {}, i => Array.isArray(i) ? i : [i])

this.manifest = await this._manifest(!!this.options.ignoreManifest)
this.manifest = await this._manifest(!!this.options.ignoreManifest, !!this.options.errorOnManifestCreate)
this.commands = Object.entries(this.manifest.commands)
.map(([id, c]) => ({...c, load: () => this.findCommand(id, {must: true})}))
}
Expand Down Expand Up @@ -167,7 +168,7 @@ export class Plugin implements IPlugin {
return cmd
}

protected async _manifest(ignoreManifest: boolean): Promise<Manifest> {
protected async _manifest(ignoreManifest: boolean, errorOnManifestCreate: boolean = false): Promise<Manifest> {
const readManifest = async () => {
try {
const p = path.join(this.root, '.oclif.manifest.json')
Expand All @@ -192,7 +193,11 @@ export class Plugin implements IPlugin {
commands: this.commandIDs.map(id => {
try {
return [id, Command.toCached(this.findCommand(id, {must: true}), this)]
} catch (err) { this.warn(err, 'toCached') }
} catch (err) {
const scope = 'toCached'
if (!errorOnManifestCreate) this.warn(err, scope)
else throw this.addErrorScope(err, scope)
}
})
.filter((f): f is [string, Command] => !!f)
.reduce((commands, [id, c]) => {
Expand All @@ -204,9 +209,13 @@ export class Plugin implements IPlugin {

protected warn(err: any, scope?: string) {
if (this.warned) return
process.emitWarning(this.addErrorScope(err, scope))
}

private addErrorScope(err: any, scope?: string) {
err.name = `${err.name} Plugin: ${this.name}`
err.detail = compact([err.detail, `module: ${this._base}`, scope && `task: ${scope}`, `plugin: ${this.name}`, `root: ${this.root}`]).join('\n')
process.emitWarning(err)
return err
}
}

Expand Down

0 comments on commit 573c26e

Please sign in to comment.