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

Commit

Permalink
fix: add command/topic getters
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 3, 2018
1 parent 22806f0 commit 92a8316
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ export interface IPlugin {
*/
valid: boolean

allCommands(): Config.Command.Plugin[]
allTopics(): Config.Topic[]
readonly commands: Config.Command.Plugin[]
readonly commandIDs: string[]
readonly topics: Config.Topic[]
findCommand(id: string, opts: {must: true}): Config.Command.Plugin
findCommand(id: string, opts?: {must: boolean}): Config.Command.Plugin | undefined
findTopic(id: string, opts: {must: true}): Config.Topic
Expand All @@ -85,7 +86,7 @@ export class Plugin implements IPlugin {
root!: string
tag?: string
manifest!: Config.Manifest
topics!: Config.Topic[]
_topics!: Config.Topic[]
plugins: IPlugin[] = []
hooks!: {[k: string]: string[]}
valid!: boolean
Expand All @@ -108,7 +109,7 @@ export class Plugin implements IPlugin {
}
this.valid = this.pjson.anycli.schema === 1

this.topics = topicsToArray(this.pjson.anycli.topics || {})
this._topics = topicsToArray(this.pjson.anycli.topics || {})
this.hooks = _.mapValues(this.pjson.anycli.hooks || {}, _.castArray)

this.manifest = this._manifest()
Expand All @@ -119,19 +120,27 @@ export class Plugin implements IPlugin {
return tsPath(this.root, this.pjson.anycli.commands)
}

allTopics() {
let topics = [...this.topics]
get topics() {
let topics = [...this._topics]
for (let plugin of this.plugins) {
topics = [...topics, ...plugin.allTopics()]
topics = [...topics, ...plugin.topics]
}
return topics
}

allCommands() {
get commands() {
let commands = Object.entries(this.manifest.commands)
.map(([id, c]) => ({...c, load: () => this._findCommand(id)}))
for (let plugin of this.plugins) {
commands = [...commands, ...plugin.allCommands()]
commands = [...commands, ...plugin.commands]
}
return commands
}

get commandIDs() {
let commands = Object.keys(this.manifest.commands)
for (let plugin of this.plugins) {
commands = [...commands, ...plugin.commandIDs]
}
return commands
}
Expand Down Expand Up @@ -176,7 +185,9 @@ export class Plugin implements IPlugin {
const promises = (this.hooks[event] || [])
.map(async hook => {
try {
await undefault(require(tsPath(this.root, hook)))(opts)
const p = tsPath(this.root, hook)
debug('hook', event, p)
await undefault(require(p))(opts)
} catch (err) {
if (err.code === 'EEXIT') throw err
cli.warn(err)
Expand Down

0 comments on commit 92a8316

Please sign in to comment.