Skip to content

Commit

Permalink
chore: revert to single theme.json
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Nov 20, 2023
1 parent 0886fbf commit cdff2ad
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 42 deletions.
8 changes: 3 additions & 5 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, Themes} from '../interfaces/theme'
import {STANDARD_CHALK, StandardChalk, Theme} from '../interfaces/theme'

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

export function parseTheme(theme: Themes): Theme {
const themes = theme.themes ?? {}
const selected = theme.selected ? themes[theme.selected] ?? {} : {}
export function parseTheme(theme: Record<string, string>): Theme {
return Object.fromEntries(
Object.entries(selected)
Object.entries(theme)
.map(([key, value]) => [key, getColor(value)])
.filter(([_, value]) => value),
)
Expand Down
22 changes: 9 additions & 13 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, Themes} from '../interfaces/theme'
import {Theme} from '../interfaces/theme'
import {loadWithData} from '../module-loader'
import {OCLIF_MARKER_OWNER, Performance} from '../performance'
import {settings} from '../settings'
Expand Down Expand Up @@ -330,8 +330,8 @@ export class Config implements IConfig {
this.npmRegistry = this.scopedEnvVar('NPM_REGISTRY') || this.pjson.oclif.npmRegistry

if (!this.scopedEnvVarTrue('DISABLE_THEME')) {
const {activeTheme} = await this.loadThemes()
this.theme = activeTheme
const {theme} = await this.loadThemes()
this.theme = theme
}

this.pjson.oclif.update = this.pjson.oclif.update || {}
Expand Down Expand Up @@ -401,18 +401,14 @@ export class Config implements IConfig {

public async loadThemes(): Promise<{
file: string
activeTheme: Theme | undefined
themes: Themes | undefined
theme: Theme | 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
const file = resolve(this.configDir, 'theme.json')
const themes = await safeReadJson<Record<string, string>>(file)
const theme = themes ? parseTheme(themes) : undefined
return {
activeTheme,
file: themesFile,
themes,
file,
theme,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +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 {Theme} from './theme'
export type {Topic} from './topic'
export type {TSConfig} from './ts-config'
1 change: 0 additions & 1 deletion src/interfaces/pjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export namespace PJSON {
repositoryPrefix?: string
schema?: number
state?: 'beta' | 'deprecated' | string
themesFile?: string
topicSeparator?: ' ' | ':'
topics?: {
[k: string]: {
Expand Down
5 changes: 0 additions & 5 deletions src/interfaces/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,3 @@ export type Theme = {
topic?: string | StandardChalk
version?: string | StandardChalk
}

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

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

expect(theme).to.deep.equal(untypedTheme)
})
Expand All @@ -66,10 +63,7 @@ describe('theme parsing', () => {
alias: 'rgb(255, 255, 255)',
}

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

expect(theme).to.deep.equal({alias: '#FFFFFF'})
})
Expand All @@ -92,10 +86,7 @@ describe('theme parsing', () => {
version: 'cyan',
}

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

const theme = parseTheme({
selected: 'test',
themes: {test: untypedTheme},
})
const theme = parseTheme(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({selected: 'test', themes: {test: theme}}))
if (theme) test = test.stub(fs, 'safeReadJson', (stub) => stub.resolves(theme))
if (pjson) test = test.stub(fs, 'readJson', (stub) => stub.resolves(pjson))

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

0 comments on commit cdff2ad

Please sign in to comment.