refactor: migrate src/storage/storage.ts from Bun.file()/Bun.write() to Filesystem module (#14122)
This commit is contained in:
@@ -39,7 +39,7 @@ export namespace Storage {
|
|||||||
cwd: path.join(project, projectDir),
|
cwd: path.join(project, projectDir),
|
||||||
absolute: true,
|
absolute: true,
|
||||||
})) {
|
})) {
|
||||||
const json = await Bun.file(msgFile).json()
|
const json = await Filesystem.readJson<any>(msgFile)
|
||||||
worktree = json.path?.root
|
worktree = json.path?.root
|
||||||
if (worktree) break
|
if (worktree) break
|
||||||
}
|
}
|
||||||
@@ -60,18 +60,15 @@ export namespace Storage {
|
|||||||
if (!id) continue
|
if (!id) continue
|
||||||
projectID = id
|
projectID = id
|
||||||
|
|
||||||
await Bun.write(
|
await Filesystem.writeJson(path.join(dir, "project", projectID + ".json"), {
|
||||||
path.join(dir, "project", projectID + ".json"),
|
id,
|
||||||
JSON.stringify({
|
vcs: "git",
|
||||||
id,
|
worktree,
|
||||||
vcs: "git",
|
time: {
|
||||||
worktree,
|
created: Date.now(),
|
||||||
time: {
|
initialized: Date.now(),
|
||||||
created: Date.now(),
|
},
|
||||||
initialized: Date.now(),
|
})
|
||||||
},
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
log.info(`migrating sessions for project ${projectID}`)
|
log.info(`migrating sessions for project ${projectID}`)
|
||||||
for await (const sessionFile of new Bun.Glob("storage/session/info/*.json").scan({
|
for await (const sessionFile of new Bun.Glob("storage/session/info/*.json").scan({
|
||||||
@@ -83,8 +80,8 @@ export namespace Storage {
|
|||||||
sessionFile,
|
sessionFile,
|
||||||
dest,
|
dest,
|
||||||
})
|
})
|
||||||
const session = await Bun.file(sessionFile).json()
|
const session = await Filesystem.readJson<any>(sessionFile)
|
||||||
await Bun.write(dest, JSON.stringify(session))
|
await Filesystem.writeJson(dest, session)
|
||||||
log.info(`migrating messages for session ${session.id}`)
|
log.info(`migrating messages for session ${session.id}`)
|
||||||
for await (const msgFile of new Bun.Glob(`storage/session/message/${session.id}/*.json`).scan({
|
for await (const msgFile of new Bun.Glob(`storage/session/message/${session.id}/*.json`).scan({
|
||||||
cwd: fullProjectDir,
|
cwd: fullProjectDir,
|
||||||
@@ -95,8 +92,8 @@ export namespace Storage {
|
|||||||
msgFile,
|
msgFile,
|
||||||
dest,
|
dest,
|
||||||
})
|
})
|
||||||
const message = await Bun.file(msgFile).json()
|
const message = await Filesystem.readJson<any>(msgFile)
|
||||||
await Bun.write(dest, JSON.stringify(message))
|
await Filesystem.writeJson(dest, message)
|
||||||
|
|
||||||
log.info(`migrating parts for message ${message.id}`)
|
log.info(`migrating parts for message ${message.id}`)
|
||||||
for await (const partFile of new Bun.Glob(`storage/session/part/${session.id}/${message.id}/*.json`).scan(
|
for await (const partFile of new Bun.Glob(`storage/session/part/${session.id}/${message.id}/*.json`).scan(
|
||||||
@@ -123,35 +120,32 @@ export namespace Storage {
|
|||||||
cwd: dir,
|
cwd: dir,
|
||||||
absolute: true,
|
absolute: true,
|
||||||
})) {
|
})) {
|
||||||
const session = await Bun.file(item).json()
|
const session = await Filesystem.readJson<any>(item)
|
||||||
if (!session.projectID) continue
|
if (!session.projectID) continue
|
||||||
if (!session.summary?.diffs) continue
|
if (!session.summary?.diffs) continue
|
||||||
const { diffs } = session.summary
|
const { diffs } = session.summary
|
||||||
await Bun.file(path.join(dir, "session_diff", session.id + ".json")).write(JSON.stringify(diffs))
|
await Filesystem.write(path.join(dir, "session_diff", session.id + ".json"), JSON.stringify(diffs))
|
||||||
await Bun.file(path.join(dir, "session", session.projectID, session.id + ".json")).write(
|
await Filesystem.writeJson(path.join(dir, "session", session.projectID, session.id + ".json"), {
|
||||||
JSON.stringify({
|
...session,
|
||||||
...session,
|
summary: {
|
||||||
summary: {
|
additions: diffs.reduce((sum: any, x: any) => sum + x.additions, 0),
|
||||||
additions: diffs.reduce((sum: any, x: any) => sum + x.additions, 0),
|
deletions: diffs.reduce((sum: any, x: any) => sum + x.deletions, 0),
|
||||||
deletions: diffs.reduce((sum: any, x: any) => sum + x.deletions, 0),
|
},
|
||||||
},
|
})
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
const state = lazy(async () => {
|
const state = lazy(async () => {
|
||||||
const dir = path.join(Global.Path.data, "storage")
|
const dir = path.join(Global.Path.data, "storage")
|
||||||
const migration = await Bun.file(path.join(dir, "migration"))
|
const migration = await Filesystem.readJson<string>(path.join(dir, "migration"))
|
||||||
.json()
|
|
||||||
.then((x) => parseInt(x))
|
.then((x) => parseInt(x))
|
||||||
.catch(() => 0)
|
.catch(() => 0)
|
||||||
for (let index = migration; index < MIGRATIONS.length; index++) {
|
for (let index = migration; index < MIGRATIONS.length; index++) {
|
||||||
log.info("running migration", { index })
|
log.info("running migration", { index })
|
||||||
const migration = MIGRATIONS[index]
|
const migration = MIGRATIONS[index]
|
||||||
await migration(dir).catch(() => log.error("failed to run migration", { index }))
|
await migration(dir).catch(() => log.error("failed to run migration", { index }))
|
||||||
await Bun.write(path.join(dir, "migration"), (index + 1).toString())
|
await Filesystem.write(path.join(dir, "migration"), (index + 1).toString())
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
dir,
|
dir,
|
||||||
@@ -171,7 +165,7 @@ export namespace Storage {
|
|||||||
const target = path.join(dir, ...key) + ".json"
|
const target = path.join(dir, ...key) + ".json"
|
||||||
return withErrorHandling(async () => {
|
return withErrorHandling(async () => {
|
||||||
using _ = await Lock.read(target)
|
using _ = await Lock.read(target)
|
||||||
const result = await Bun.file(target).json()
|
const result = await Filesystem.readJson<T>(target)
|
||||||
return result as T
|
return result as T
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -181,10 +175,10 @@ export namespace Storage {
|
|||||||
const target = path.join(dir, ...key) + ".json"
|
const target = path.join(dir, ...key) + ".json"
|
||||||
return withErrorHandling(async () => {
|
return withErrorHandling(async () => {
|
||||||
using _ = await Lock.write(target)
|
using _ = await Lock.write(target)
|
||||||
const content = await Bun.file(target).json()
|
const content = await Filesystem.readJson<T>(target)
|
||||||
fn(content)
|
fn(content as T)
|
||||||
await Bun.write(target, JSON.stringify(content, null, 2))
|
await Filesystem.writeJson(target, content)
|
||||||
return content as T
|
return content
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +187,7 @@ export namespace Storage {
|
|||||||
const target = path.join(dir, ...key) + ".json"
|
const target = path.join(dir, ...key) + ".json"
|
||||||
return withErrorHandling(async () => {
|
return withErrorHandling(async () => {
|
||||||
using _ = await Lock.write(target)
|
using _ = await Lock.write(target)
|
||||||
await Bun.write(target, JSON.stringify(content, null, 2))
|
await Filesystem.writeJson(target, content)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user