Skip to content

Commit

Permalink
chore: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Apr 18, 2024
1 parent b2e8ff8 commit 1b57909
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
67 changes: 32 additions & 35 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export class Config implements IConfig {
private topicPermutations = new Permutations()

constructor(public options: Options) {}

static async load(opts: LoadOptions = module.filename || __dirname): Promise<Config> {
// Handle the case when a file URL string is passed in such as 'import.meta.url'; covert to file path.
if (typeof opts === 'string' && opts.startsWith('file://')) {
Expand Down Expand Up @@ -202,7 +201,6 @@ export class Config implements IConfig {
}

public findCommand(id: string, opts: {must: true}): Command.Loadable

public findCommand(id: string, opts?: {must: boolean}): Command.Loadable | undefined

public findCommand(id: string, opts: {must?: boolean} = {}): Command.Loadable | undefined {
Expand Down Expand Up @@ -336,42 +334,10 @@ export class Config implements IConfig {

this.theme = await this.loadTheme()

const s3 = this.pjson.oclif.update?.s3 ?? {
bucket: '',
host: '',
templates: {
target: {},
vanilla: {},
},
}

s3.bucket = this.scopedEnvVar('S3_BUCKET') || s3.bucket
if (s3.bucket && !s3.host) s3.host = `https://${s3.bucket}.s3.amazonaws.com`
s3.templates = {
...s3.templates,
target: {
baseDir: '<%- bin %>',
manifest: "<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- platform %>-<%- arch %>",
unversioned:
"<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- bin %>-<%- platform %>-<%- arch %><%- ext %>",
versioned:
"<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- bin %>-v<%- version %>/<%- bin %>-v<%- version %>-<%- platform %>-<%- arch %><%- ext %>",
...(s3.templates && s3.templates.target),
},
vanilla: {
baseDir: '<%- bin %>',
manifest: "<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %>version",
unversioned: "<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- bin %><%- ext %>",
versioned:
"<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- bin %>-v<%- version %>/<%- bin %>-v<%- version %><%- ext %>",
...(s3.templates && s3.templates.vanilla),
},
}

this.updateConfig = {
...this.pjson.oclif.update,
node: this.pjson.oclif.update?.node ?? {},
s3,
s3: this.buildS3Config(),
}

this.isSingleCommandCLI = Boolean(
Expand Down Expand Up @@ -740,6 +706,37 @@ export class Config implements IConfig {
return shellPath.at(-1) ?? 'unknown'
}

private buildS3Config() {
const s3 = this.pjson.oclif.update?.s3
const bucket = this.scopedEnvVar('S3_BUCKET') ?? s3?.bucket
const host = s3?.host ?? (bucket && `https://${bucket}.s3.amazonaws.com`)
const templates = {
...s3?.templates,
target: {
baseDir: '<%- bin %>',
manifest: "<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- platform %>-<%- arch %>",
unversioned:
"<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- bin %>-<%- platform %>-<%- arch %><%- ext %>",
versioned:
"<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- bin %>-v<%- version %>/<%- bin %>-v<%- version %>-<%- platform %>-<%- arch %><%- ext %>",
...(s3?.templates && s3?.templates.target),
},
vanilla: {
baseDir: '<%- bin %>',
manifest: "<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %>version",
unversioned: "<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- bin %><%- ext %>",
versioned:
"<%- channel === 'stable' ? '' : 'channels/' + channel + '/' %><%- bin %>-v<%- version %>/<%- bin %>-v<%- version %><%- ext %>",
...(s3?.templates && s3?.templates.vanilla),
},
}
return {
bucket,
host,
templates,
}
}

/**
* This method is responsible for locating the correct plugin to use for a named command id
* It searches the {Config} registered commands to match either the raw command id or the command alias
Expand Down
12 changes: 11 additions & 1 deletion src/errors/warn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import prettyPrint from './errors/pretty-print'

const WARNINGS = new Set<Error | string>()

type Options = {
/**
* If true, will only print the same warning once.
*/
ignoreDuplicates?: boolean
}

/**
* Prints a pretty warning message to stderr.
*
* @param input The error or string to print.
* @param options.ignoreDuplicates If true, will only print the same warning once.
*/
export function warn(input: Error | string, options?: {ignoreDuplicates: boolean}): void {
export function warn(input: Error | string, options?: Options): void {
const ignoreDuplicates = options?.ignoreDuplicates ?? true
if (ignoreDuplicates && WARNINGS.has(input)) return
WARNINGS.add(input)
Expand Down
3 changes: 2 additions & 1 deletion src/ux/colorize-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ function hasRemainingTokens(input: string | undefined, foundToken: boolean) {
* - null
*/
export default function colorizeJson(json: unknown, options?: Options) {
return tokenize(json, options).reduce((acc, token) => acc + colorize(options?.theme?.[token.type], token.value), '')
const opts = {...options, pretty: options?.pretty ?? true}
return tokenize(json, opts).reduce((acc, token) => acc + colorize(options?.theme?.[token.type], token.value), '')
}
5 changes: 1 addition & 4 deletions src/ux/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ export function colorize(color: string | StandardAnsi | undefined, text: string)
export function parseTheme(theme: Record<string, string>): Theme {
return Object.fromEntries(
Object.entries(theme)
.map(([key, value]) => {
if (typeof value === 'string') return [key, isValid(value)]
return [key, parseTheme(value)]
})
.map(([key, value]) => [key, typeof value === 'string' ? isValid(value) : parseTheme(value)])
.filter(([_, value]) => value),
)
}
Expand Down

0 comments on commit 1b57909

Please sign in to comment.