Skip to content

Commit 689c6d6

Browse files
rexxarsclaude
andcommitted
fix(cli-core): invalidate token cache on auth change, remove redundant mkdirSync
setCliUserConfig now calls clearCliTokenCache() so that subsequent getCliToken() calls in the same process pick up the new value. Also removes the redundant mkdirSync from ConfigStore.delete() - if readConfig() succeeded, the directory already exists. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 531911f commit 689c6d6

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

packages/@sanity/cli-core/src/config/__tests__/cliToken.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,20 @@ describe('getCliToken', () => {
7272
const token = await getCliToken()
7373
expect(token).toBe('trimmed-token')
7474
})
75+
76+
it('should re-read after clearCliTokenCache', async () => {
77+
vi.mocked(getCliUserConfig).mockReturnValueOnce('first-token')
78+
const module = await import('../../services/getCliToken')
79+
80+
const firstCall = await module.getCliToken()
81+
expect(firstCall).toBe('first-token')
82+
83+
// Clear cache and set up a new return value
84+
module.clearCliTokenCache()
85+
vi.mocked(getCliUserConfig).mockReturnValueOnce('second-token')
86+
87+
const secondCall = await module.getCliToken()
88+
expect(secondCall).toBe('second-token')
89+
expect(getCliUserConfig).toHaveBeenCalledTimes(2)
90+
})
7591
})

packages/@sanity/cli-core/src/config/__tests__/cliUserConfig.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import {homedir} from 'node:os'
44
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest'
55

66
import {getCliUserConfig, getUserConfig, setCliUserConfig} from '../../services/cliUserConfig'
7+
import {clearCliTokenCache} from '../../services/getCliToken'
78
import {readJsonFileSync} from '../../util/readJsonFileSync'
89
import {writeJsonFileSync} from '../../util/writeJsonFileSync'
910

1011
vi.mock('node:fs')
1112
vi.mock('node:os')
1213
vi.mock('../../util/readJsonFileSync')
1314
vi.mock('../../util/writeJsonFileSync')
15+
vi.mock('../../services/getCliToken')
1416

1517
const mockHomedir = '/mock/home/dir'
1618

@@ -131,6 +133,16 @@ describe('cliUserConfig', () => {
131133
expect.any(Object),
132134
)
133135
})
136+
137+
test('invalidates token cache after setting authToken', () => {
138+
setCliUserConfig('authToken', 'new-token')
139+
expect(clearCliTokenCache).toHaveBeenCalled()
140+
})
141+
142+
test('invalidates token cache after clearing authToken', () => {
143+
setCliUserConfig('authToken', undefined)
144+
expect(clearCliTokenCache).toHaveBeenCalled()
145+
})
134146
})
135147

136148
describe('getUserConfig', () => {
@@ -217,7 +229,7 @@ describe('cliUserConfig', () => {
217229
const store = getUserConfig()
218230
store.delete('removeMe')
219231

220-
expect(mkdirSync).toHaveBeenCalledWith(expect.any(String), {recursive: true})
232+
expect(mkdirSync).not.toHaveBeenCalled()
221233
expect(writeJsonFileSync).toHaveBeenCalledWith(
222234
expect.any(String),
223235
{keepMe: 'yes'},

packages/@sanity/cli-core/src/services/cliUserConfig.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {z} from 'zod'
77
import {debug} from '../debug.js'
88
import {readJsonFileSync} from '../util/readJsonFileSync.js'
99
import {writeJsonFileSync} from '../util/writeJsonFileSync.js'
10+
import {clearCliTokenCache} from './getCliToken.js'
1011

1112
const cliUserConfigSchema = {
1213
authToken: z.string().optional(),
@@ -42,6 +43,10 @@ export function setCliUserConfig(prop: 'authToken', value: string | undefined):
4243
} else {
4344
writeJsonFileSync(configPath, {...config, [prop]: value}, {pretty: true})
4445
}
46+
47+
// Invalidate the in-process token cache so subsequent getCliToken() calls
48+
// pick up the new value from disk.
49+
clearCliTokenCache()
4550
}
4651

4752
/**
@@ -112,9 +117,7 @@ export function getUserConfig(): ConfigStore {
112117
const config = readConfig()
113118
if (!(key in config)) return
114119
const {[key]: _, ...rest} = config
115-
const configPath = getCliUserConfigPath()
116-
mkdirSync(dirname(configPath), {recursive: true})
117-
writeJsonFileSync(configPath, rest, {pretty: true})
120+
writeJsonFileSync(getCliUserConfigPath(), rest, {pretty: true})
118121
},
119122
}
120123
}

packages/@sanity/cli-core/src/services/getCliToken.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ export async function getCliToken(): Promise<string | undefined> {
2222
cachedToken = getCliUserConfig('authToken')
2323
return cachedToken
2424
}
25+
26+
/**
27+
* Clear the in-process token cache so the next `getCliToken()` call
28+
* re-reads from disk or the environment.
29+
*
30+
* Called automatically by `setCliUserConfig('authToken', ...)`.
31+
*
32+
* @internal
33+
*/
34+
export function clearCliTokenCache(): void {
35+
cachedToken = undefined
36+
}

0 commit comments

Comments
 (0)