mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import type { OcmState } from '../../src/state.js'
|
|
|
|
export interface CliHomeOptions {
|
|
state?: OcmState | null
|
|
account?: string
|
|
token?: string | null
|
|
documentVersion?: number
|
|
envToken?: string | null
|
|
}
|
|
|
|
export interface CliHome {
|
|
home: string
|
|
configDir: string
|
|
statePath: string
|
|
credentialsPath: string
|
|
account: string
|
|
writeState: (state: OcmState | null) => void
|
|
writeToken: (token: string | null) => void
|
|
restore: () => void
|
|
}
|
|
|
|
const DEFAULT_ACCOUNT = 'https://manager.example.com'
|
|
|
|
export function createCliHome(options: CliHomeOptions = {}): CliHome {
|
|
const savedHome = process.env.HOME
|
|
const savedToken = process.env.OCM_TOKEN
|
|
const savedPlatform = Object.getOwnPropertyDescriptor(process, 'platform')
|
|
const home = mkdtempSync(join(tmpdir(), 'ocm-home-'))
|
|
process.env.HOME = home
|
|
Object.defineProperty(process, 'platform', { value: 'linux', configurable: true })
|
|
if (options.envToken) process.env.OCM_TOKEN = options.envToken
|
|
else delete process.env.OCM_TOKEN
|
|
|
|
const configDir = join(home, '.config', 'opencode-manager')
|
|
mkdirSync(configDir, { recursive: true })
|
|
const statePath = join(configDir, 'state.json')
|
|
const credentialsPath = join(configDir, 'credentials.json')
|
|
const account = options.account ?? DEFAULT_ACCOUNT
|
|
|
|
function writeState(state: OcmState | null): void {
|
|
writeFileSync(statePath, state === null ? '{}' : JSON.stringify(state))
|
|
}
|
|
|
|
function writeToken(token: string | null): void {
|
|
const document = { version: options.documentVersion ?? 1, tokens: token === null ? {} : { [account]: token } }
|
|
writeFileSync(credentialsPath, JSON.stringify(document))
|
|
}
|
|
|
|
writeState(options.state === undefined ? { managerUrl: account } : options.state)
|
|
writeToken(options.token ?? null)
|
|
|
|
return {
|
|
home,
|
|
configDir,
|
|
statePath,
|
|
credentialsPath,
|
|
account,
|
|
writeState,
|
|
writeToken,
|
|
restore: () => {
|
|
if (savedHome === undefined) delete process.env.HOME
|
|
else process.env.HOME = savedHome
|
|
if (savedToken === undefined) delete process.env.OCM_TOKEN
|
|
else process.env.OCM_TOKEN = savedToken
|
|
if (savedPlatform) Object.defineProperty(process, 'platform', savedPlatform)
|
|
rmSync(home, { recursive: true, force: true })
|
|
},
|
|
}
|
|
}
|