Skip to content

Commit

Permalink
Merge pull request #932 from oclif/mdonnalley/configurable-theme
Browse files Browse the repository at this point in the history
feat: allow theme file to be configurable
  • Loading branch information
shetzel authored Feb 5, 2024
2 parents ffa2a1c + 03c597a commit 1943ed0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
23 changes: 17 additions & 6 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,26 @@ export class Config implements IConfig {
}

public async loadThemes(): Promise<{
file: string
file: string | undefined
theme: Theme | undefined
}> {
const file = resolve(this.configDir, 'theme.json')
const themes = await safeReadJson<Record<string, string>>(file)
const theme = themes ? parseTheme(themes) : undefined
const defaultThemeFile = this.pjson.oclif.theme
? resolve(this.root, this.pjson.oclif.theme)
: this.pjson.oclif.theme
const userThemeFile = resolve(this.configDir, 'theme.json')

const [defaultTheme, userTheme] = await Promise.all([
defaultThemeFile ? await safeReadJson<Record<string, string>>(defaultThemeFile) : undefined,
await safeReadJson<Record<string, string>>(userThemeFile),
])

// Merge the default theme with the user theme, giving the user theme precedence.
const merged = {...defaultTheme, ...userTheme}
return {
file,
theme,
// Point to the user file if it exists, otherwise use the default file.
// This doesn't really serve a purpose to anyone but removing it would be a breaking change.
file: userTheme ? userThemeFile : defaultThemeFile,
theme: Object.keys(merged).length > 0 ? parseTheme(merged) : undefined,
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/help/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class CommandHelp extends HelpFormatter {
}
}

label = labels.join(colorize(this.config?.theme?.flagSeparator, flag.char ? ', ' : ' '))
label = labels.join(flag.char ? colorize(this.config?.theme?.flagSeparator, ', ') : ' ')
}

if (flag.type === 'option') {
Expand All @@ -175,7 +175,7 @@ export class CommandHelp extends HelpFormatter {
label += `=${value}`
}

return label
return colorize(this.config.theme?.flag, label)
}

protected flags(flags: Array<Command.Flag.Any>): [string, string | undefined][] | undefined {
Expand All @@ -184,7 +184,7 @@ export class CommandHelp extends HelpFormatter {
const noChar = flags.reduce((previous, current) => previous && current.char === undefined, true)

return flags.map((flag) => {
let left = colorize(this.config?.theme?.flag, this.flagHelpLabel(flag))
let left = this.flagHelpLabel(flag)

if (noChar) left = left.replace(' ', '')

Expand Down
11 changes: 6 additions & 5 deletions src/interfaces/pjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ export namespace PJSON {
identifier?: string
sign?: string
}
windows?: {
homepage?: string
keypath?: string
name?: string
}
plugins?: string[]
repositoryPrefix?: string
schema?: number
state?: 'beta' | 'deprecated' | string
theme?: string
topicSeparator?: ' ' | ':'
topics?: {
[k: string]: {
Expand All @@ -72,6 +68,11 @@ export namespace PJSON {
}
s3: S3
}
windows?: {
homepage?: string
keypath?: string
name?: string
}
}
version: string
}
Expand Down

0 comments on commit 1943ed0

Please sign in to comment.