Skip to content

feat: improve the scope of configuration file updates #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion extension/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { workspace } from 'vscode'
import type { WorkspaceConfiguration } from 'vscode'
import { ConfigurationTarget, workspace } from 'vscode'

export function getConfig<T = any>(key: string): T | undefined {
return workspace
Expand All @@ -12,3 +13,40 @@ export async function setConfig(key: string, value: any, isGlobal = true) {
.getConfiguration()
.update(key, value, isGlobal)
}

type TargetMethod = keyof Exclude<ReturnType<WorkspaceConfiguration['inspect']>, undefined>

class Config {
#section = 'explorer.fileNesting'
private configTarget: ConfigurationTarget
private targetMethod: TargetMethod

constructor() {
[this.targetMethod, this.configTarget] = this.#detectConfigTarget()
}

get<T = any>(key: string): T | undefined {
return workspace.getConfiguration(this.#section).inspect(key)?.[this.targetMethod] as T
}

set(key: string, value: any) {
return workspace.getConfiguration(this.#section).update(key, value, this.configTarget)
}

#detectConfigTarget(): [TargetMethod, ConfigurationTarget] {
const _map = new Map<TargetMethod, ConfigurationTarget>([
['workspaceFolderValue', ConfigurationTarget.WorkspaceFolder],
['workspaceValue', ConfigurationTarget.Workspace],
['globalValue', ConfigurationTarget.Global],
])

for (const [key, value] of _map) {
if (workspace.getConfiguration(this.#section).inspect('enabled')?.[key] === true)
return [key, value]
}

return ['globalValue', ConfigurationTarget.Global]
}
}

export default Config
38 changes: 23 additions & 15 deletions extension/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { fetch } from 'ofetch'
import type { ExtensionContext } from 'vscode'
import { window, workspace } from 'vscode'
import { window } from 'vscode'
import { FILE, MSG_PREFIX, URL_PREFIX } from './constants'
import { getConfig } from './config'
import Config, { getConfig } from './config'

export async function fetchLatest() {
const repo = getConfig<string>('fileNestingUpdater.upstreamRepo')
Expand All @@ -24,16 +24,28 @@ export async function fetchLatest() {
return config['explorer.fileNesting.patterns']
}

export async function fetchAndUpdate(ctx: ExtensionContext, prompt = true) {
const config = workspace.getConfiguration()
interface Options {
// Whether to prompt the user
prompt?: boolean
/** Everything up-to-date notification */
anyUpdate?: boolean
}

export async function fetchAndUpdate(ctx: ExtensionContext, opt: Options) {
const { prompt = true, anyUpdate } = opt || {}
const fileNestingConfig = new Config()
const patterns = await fetchLatest()
let shouldUpdate = true

const oringalPatterns = { ...(config.get<object>('explorer.fileNesting.patterns') || {}) }
delete oringalPatterns['//']
const originalPatterns = { ...(fileNestingConfig.get<object>('patterns') || {}) }
delete originalPatterns['//']
// no change
if (Object.keys(oringalPatterns).length > 0 && JSON.stringify(patterns) === JSON.stringify(oringalPatterns))
if (Object.keys(originalPatterns).length > 0 && JSON.stringify(patterns) === JSON.stringify(originalPatterns)) {
if (anyUpdate)
window.showInformationMessage(`${MSG_PREFIX} Everything up-to-date`)

return false
}

if (prompt) {
const buttonUpdate = 'Update'
Expand All @@ -47,16 +59,12 @@ export async function fetchAndUpdate(ctx: ExtensionContext, prompt = true) {
}

if (shouldUpdate) {
if (config.inspect('explorer.fileNesting.enabled')?.globalValue == null)
config.update('explorer.fileNesting.enabled', true, true)

if (config.inspect('explorer.fileNesting.expand')?.globalValue == null)
config.update('explorer.fileNesting.expand', false, true)

config.update('explorer.fileNesting.patterns', {
fileNestingConfig.set('enabled', true)
fileNestingConfig.set('expand', false)
fileNestingConfig.set('patterns', {
'//': `Last update at ${new Date().toLocaleString()}`,
...patterns,
}, true)
})

ctx.globalState.update('lastUpdate', Date.now())

Expand Down
14 changes: 10 additions & 4 deletions extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ import { getConfig } from './config'
import { fetchAndUpdate } from './fetch'

export async function activate(ctx: ExtensionContext) {
commands.registerCommand('antfu.file-nesting.manualUpdate', () => fetchAndUpdate(ctx, false))
commands.registerCommand(
'antfu.file-nesting.manualUpdate',
() => fetchAndUpdate(ctx, {
prompt: false,
anyUpdate: true,
}),
)

const lastUpdate = ctx.globalState.get('lastUpdate', 0)
const initialized = ctx.globalState.get('init', false)
const autoUpdateInterval = getConfig<number>('fileNestingUpdater.autoUpdateInterval')!

if (!initialized) {
ctx.globalState.update('init', true)
fetchAndUpdate(ctx, false)
fetchAndUpdate(ctx, { prompt: false })
}

if (getConfig('fileNestingUpdater.autoUpdate')) {
if (Date.now() - lastUpdate >= autoUpdateInterval * 60_000)
fetchAndUpdate(ctx, getConfig('fileNestingUpdater.promptOnAutoUpdate'))
fetchAndUpdate(ctx, { prompt: getConfig('fileNestingUpdater.promptOnAutoUpdate') })
}
}

export function deactivate() {}
export function deactivate() { }