Skip to content

Commit 2711cf5

Browse files
committed
refactor: Remove cache implementation and replace with token storage methods
1 parent fcfb719 commit 2711cf5

4 files changed

Lines changed: 17 additions & 62 deletions

File tree

src/lib/cache.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/lib/initialization.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import consola from "consola"
22
import fs from "node:fs"
33
import { FetchError } from "ofetch"
44

5-
import { PATHS } from "~/lib/paths"
65
import { CONFIG } from "~/lib/config"
6+
import { PATHS } from "~/lib/paths"
77
import { getGitHubUser } from "~/services/github/get-user/service"
88

99
import type { parseCli } from "./cli"
1010

1111
import { getModels } from "../services/copilot/get-models/service"
1212
import { getCopilotToken } from "../services/copilot/get-token/copilot-token"
1313
import { getGitHubToken } from "../services/github/get-token/service"
14-
import { CACHE } from "./cache"
1514
import { initializeLogger } from "./logger"
15+
import { getGithubToken, saveGithubToken } from "./token-storage"
1616
import { TOKENS } from "./tokens"
1717

1818
interface InitStep {
@@ -22,26 +22,17 @@ interface InitStep {
2222

2323
const initSteps: Array<InitStep> = [
2424
{
25-
name: "Emulation check",
25+
name: "App directory",
2626
run: () => {
27-
if (CONFIG.EMULATE_STREAMING) {
28-
consola.box("Streaming emulation is enabled.")
29-
}
30-
},
31-
},
32-
{
33-
name: "Cache",
34-
run: async () => {
35-
if (!fs.existsSync(PATHS.CACHE_PATH)) {
27+
if (!fs.existsSync(PATHS.APP_DIR)) {
3628
fs.mkdirSync(PATHS.APP_DIR, { recursive: true })
37-
await CACHE._write({})
3829
}
3930
},
4031
},
4132
{
4233
name: "GitHub authentication",
4334
run: async () => {
44-
TOKENS.GITHUB_TOKEN = await getCachedGithubToken()
35+
TOKENS.GITHUB_TOKEN = await getGithubToken()
4536

4637
try {
4738
await logUser()
@@ -89,15 +80,10 @@ const initSteps: Array<InitStep> = [
8980
},
9081
]
9182

92-
async function getCachedGithubToken() {
93-
const cachedToken = await CACHE.get("github-token")
94-
return cachedToken?.value
95-
}
96-
9783
async function initializeGithubToken() {
9884
consola.start("Getting GitHub device code")
9985
const token = await getGitHubToken()
100-
await CACHE.set("github-token", token.access_token)
86+
await saveGithubToken(token.access_token)
10187
return token.access_token
10288
}
10389

src/lib/paths.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import path from "pathe"
33

44
const APP_DIR = path.join(os.homedir(), ".local", "share", "copilot-api")
55

6-
const CACHE_PATH = path.join(APP_DIR, "cache.json")
6+
const GITHUB_TOKEN_PATH = path.join(APP_DIR, "github_token")
77
const LOG_PATH = path.join(APP_DIR, "logs")
88
const LOG_FILE = path.join(LOG_PATH, "app.log")
99

1010
export const PATHS = {
1111
APP_DIR,
12-
CACHE_PATH,
12+
GITHUB_TOKEN_PATH,
1313
LOG_PATH,
1414
LOG_FILE,
1515
}

src/lib/token-storage.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { PATHS } from "./paths"
2+
3+
export async function saveGithubToken(token: string) {
4+
await Bun.write(PATHS.GITHUB_TOKEN_PATH, token)
5+
}
6+
7+
export async function getGithubToken(): Promise<string> {
8+
return Bun.file(PATHS.GITHUB_TOKEN_PATH).text()
9+
}

0 commit comments

Comments
 (0)