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

Commit

Permalink
fix: build topics dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 7, 2018
1 parent 3dc9fa5 commit 8418ace
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export interface IConfig {
userPJSON?: PJSON.User
plugins: Plugin.IPlugin[]
readonly commands: Command.Plugin[]
readonly topics: Topic[]
readonly commandIDs: string[]

runCommand(id: string, argv?: string[]): Promise<void>
Expand Down Expand Up @@ -132,7 +133,6 @@ export class Config implements IConfig {
pjson: PJSON.CLI
userPJSON?: PJSON.User
plugins: Plugin.IPlugin[] = []
topics: Topic[] = []
protected warned = false

constructor(opts: Options) {
Expand Down Expand Up @@ -180,8 +180,6 @@ export class Config implements IConfig {
}
}

this.addMissingTopics()

debug('config done')
}

Expand Down Expand Up @@ -264,6 +262,30 @@ export class Config implements IConfig {

get commands(): Command.Plugin[] { return flatMap(this.plugins, p => p.commands) }
get commandIDs() { return uniq(this.commands.map(c => c.id)) }
get topics(): Topic[] {
let topics: Topic[] = []
for (let plugin of this.plugins) {
for (let topic of plugin.topics) {
let existing = topics.find(t => t.name === topic.name)
if (existing) {
existing.description = topic.description || existing.description
existing.hidden = existing.hidden || topic.hidden
} else topics.push(topic)
}
}
// add missing topics
for (let c of this.commands.filter(c => !c.hidden)) {
let parts = c.id.split(':')
while (parts.length) {
let name = parts.join(':')
if (name && !topics.find(t => t.name === name)) {
topics.push({name})
}
parts.pop()
}
}
return topics
}

protected dir(category: 'cache' | 'data' | 'config'): string {
const base = process.env[`XDG_${category.toUpperCase()}_HOME`]
Expand Down Expand Up @@ -314,33 +336,13 @@ export class Config implements IConfig {
}
let instance = new Plugin.Plugin(opts)
this.plugins.push(instance)
for (let topic of instance.topics) {
let existing = this.topics.find(t => t.name === topic.name)
if (existing) {
existing.description = topic.description || existing.description
existing.hidden = existing.hidden || topic.hidden
} else this.topics.push(topic)
}
} catch (err) {
if (options.must) throw err
this.warn(err, 'loadPlugins')
}
}
}

protected addMissingTopics() {
for (let c of this.commands.filter(c => !c.hidden)) {
let parts = c.id.split(':')
while (parts.length) {
let name = parts.join(':')
if (name && !this.topics.find(t => t.name === name)) {
this.topics.push({name})
}
parts.pop()
}
}
}

protected warn(err: any, scope?: string) {
if (this.warned) return
err.name = `${err.name} Plugin: ${this.name}`
Expand Down

0 comments on commit 8418ace

Please sign in to comment.