Skip to content

Commit

Permalink
feat: reload Config before running command (#770)
Browse files Browse the repository at this point in the history
* feat: reload Config before running command

* fix: better implementation

* fix: better implementation
  • Loading branch information
mdonnalley authored Aug 28, 2023
1 parent 6e6032e commit efd1f54
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import {Performance} from '../performance'
import {settings} from '../settings'
import {userInfo as osUserInfo} from 'node:os'
import {sep} from 'node:path'
import {lt} from 'semver'

// eslint-disable-next-line new-cap
const debug = Debug()

const _pjson = requireJson<PJSON>(__dirname, '..', '..', 'package.json')
const BASE = `${_pjson.name}@${_pjson.version}`

function channelFromVersion(version: string) {
const m = version.match(/[^-]+(?:-([^.]+))?/)
Expand Down Expand Up @@ -69,7 +71,7 @@ class Permutations extends Map<string, Set<string>> {
}

export class Config implements IConfig {
private _base = `${_pjson.name}@${_pjson.version}`
private _base = BASE

public arch!: ArchTypes
public bin!: string
Expand Down Expand Up @@ -111,7 +113,9 @@ export class Config implements IConfig {

private _commandIDs!: string[]

constructor(public options: Options) {}
constructor(public options: Options) {
if (options.config) Object.assign(this, options.config)
}

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.
Expand All @@ -120,7 +124,26 @@ export class Config implements IConfig {
}

if (typeof opts === 'string') opts = {root: opts}
if (isConfig(opts)) return opts
if (isConfig(opts)) {
const currentConfigBase = BASE.replace('@oclif/core@', '')
const incomingConfigBase = opts._base.replace('@oclif/core@', '')
/**
* Reload the Config based on the version required by the command.
* This is needed because the command is given the Config instantiated
* by the root plugin, which may be a different version than the one
* required by the command.
*
* Doing this ensures that the command can freely use any method on Config that
* exists in the version of Config required by the command but may not exist on the
* root's instance of Config.
*/
if (lt(incomingConfigBase, currentConfigBase)) {
debug(`reloading config from ${opts._base} to ${BASE}`)
return new Config({...opts.options, config: opts})
}

return opts
}

const config = new Config(opts)
await config.load()
Expand All @@ -129,6 +152,8 @@ export class Config implements IConfig {

// eslint-disable-next-line complexity
public async load(): Promise<void> {
if (this.options.config) return

settings.performanceEnabled = (settings.performanceEnabled === undefined ? this.options.enablePerf : settings.performanceEnabled) ?? false
const plugin = new Plugin.Plugin({root: this.options.root})
await plugin.load()
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Config} from './config'
import {Command} from '../command'
import {PJSON} from './pjson'
import {Topic} from './topic'
Expand All @@ -20,6 +21,7 @@ export interface Options extends PluginOptions {
channel?: string;
version?: string;
enablePerf?: boolean;
config?: Config
}

export interface Plugin {
Expand Down

0 comments on commit efd1f54

Please sign in to comment.