Skip to content

Commit

Permalink
fix: prepare for plugin-theme
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Nov 16, 2023
1 parent 0194eaa commit 31604f8
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/cli-ux/theme.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk'
import * as Color from 'color'

import {STANDARD_CHALK, StandardChalk, Theme} from '../interfaces/theme'
import {STANDARD_CHALK, StandardChalk, Theme, Themes} from '../interfaces/theme'

function isStandardChalk(color: any): color is StandardChalk {
return STANDARD_CHALK.includes(color)
Expand All @@ -13,9 +13,11 @@ export function colorize(color: Color | StandardChalk | undefined, text: string)
return color ? chalk.hex(color.hex())(text) : text
}

export function parseTheme(untypedTheme: Record<string, string>): Theme {
export function parseTheme(theme: Themes): Theme {
const themes = theme.themes ?? {}
const selected = theme.selected ? themes[theme.selected] ?? {} : {}
return Object.fromEntries(
Object.entries(untypedTheme)
Object.entries(selected)
.map(([key, value]) => [key, getColor(value)])
.filter(([_, value]) => value),
)
Expand Down
24 changes: 20 additions & 4 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {getHelpFlagAdditions} from '../help/util'
import {Hook, Hooks, PJSON, Topic} from '../interfaces'
import {ArchTypes, Config as IConfig, LoadOptions, PlatformTypes, VersionDetails} from '../interfaces/config'
import {Plugin as IPlugin, Options} from '../interfaces/plugin'
import {Theme} from '../interfaces/theme'
import {Theme, Themes} from '../interfaces/theme'
import {loadWithData} from '../module-loader'
import {OCLIF_MARKER_OWNER, Performance} from '../performance'
import {settings} from '../settings'
Expand Down Expand Up @@ -330,9 +330,8 @@ export class Config implements IConfig {
this.npmRegistry = this.scopedEnvVar('NPM_REGISTRY') || this.pjson.oclif.npmRegistry

if (!this.scopedEnvVarTrue('DISABLE_THEME')) {
const themeFilePath = resolve(this.configDir, 'theme.json')
const theme = await safeReadJson<Record<string, string>>(themeFilePath)
this.theme = theme ? parseTheme(theme) : undefined
const {activeTheme} = await this.loadThemes()
this.theme = activeTheme
}

this.pjson.oclif.update = this.pjson.oclif.update || {}
Expand Down Expand Up @@ -400,6 +399,23 @@ export class Config implements IConfig {
}
}

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

protected macosCacheDir(): string | undefined {
return (this.platform === 'darwin' && join(this.home, 'Library', 'Caches', this.dirname)) || undefined
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export type {Arg, BooleanFlag, CustomOptions, Deprecation, Flag, FlagDefinition,
export type {PJSON} from './pjson'
export type {Options, Plugin, PluginOptions} from './plugin'
export type {S3Manifest} from './s3-manifest'
export type {Theme, Themes} from './theme'
export type {Topic} from './topic'
export type {TSConfig} from './ts-config'
1 change: 1 addition & 0 deletions src/interfaces/pjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export namespace PJSON {
repositoryPrefix?: string
schema?: number
state?: 'beta' | 'deprecated' | string
themesFile?: string
topicSeparator?: ' ' | ':'
topics?: {
[k: string]: {
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ export type Theme = {
topic?: Color | StandardChalk
version?: Color | StandardChalk
}

export type Themes = {
selected?: string
themes?: Record<string, Record<string, string>>
}
20 changes: 16 additions & 4 deletions test/cli-ux/theme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ describe('theme parsing', () => {
version: '#FFFFFF',
}

const theme = parseTheme(untypedTheme)
const theme = parseTheme({
selected: 'test',
themes: {test: untypedTheme},
})

for (const value of Object.values(theme)) {
expect(typeof value).to.equal('object')
Expand All @@ -68,7 +71,10 @@ describe('theme parsing', () => {
alias: 'rgb(255, 255, 255)',
}

const theme = parseTheme(untypedTheme)
const theme = parseTheme({
selected: 'test',
themes: {test: untypedTheme},
})

for (const value of Object.values(theme)) {
expect(typeof value).to.equal('object')
Expand Down Expand Up @@ -96,7 +102,10 @@ describe('theme parsing', () => {
version: 'cyan',
}

const theme = parseTheme(untypedTheme)
const theme = parseTheme({
selected: 'test',
themes: {test: untypedTheme},
})
for (const value of Object.values(theme)) {
expect(value).to.equal('cyan')
}
Expand All @@ -107,7 +116,10 @@ describe('theme parsing', () => {
alias: 'FOO',
}

const theme = parseTheme(untypedTheme)
const theme = parseTheme({
selected: 'test',
themes: {test: untypedTheme},
})
expect(theme).to.deep.equal({})
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Config', () => {
.stub(os, 'getHomeDir', (stub) => stub.returns(join(homedir)))
.stub(os, 'getPlatform', (stub) => stub.returns(platform))

if (theme) test = test.stub(fs, 'safeReadJson', (stub) => stub.resolves(theme))
if (theme) test = test.stub(fs, 'safeReadJson', (stub) => stub.resolves({selected: 'test', themes: {test: theme}}))
if (pjson) test = test.stub(fs, 'readJson', (stub) => stub.resolves(pjson))

test = test.add('config', () => Config.load())
Expand Down

0 comments on commit 31604f8

Please sign in to comment.