This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
029fb6e
commit 28ab5a7
Showing
19 changed files
with
251 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import fs from 'fs/promises' | ||
import {join} from 'path' | ||
import z from 'zod' | ||
import {userDataPath} from './app-paths' | ||
import {t} from './app-trpc' | ||
|
||
const draftsDir = join(userDataPath, 'drafts') | ||
|
||
let draftIdList: string[] | undefined = undefined | ||
|
||
async function initDrafts() { | ||
await fs.mkdir(draftsDir, {recursive: true}) | ||
await fs.readdir(draftsDir) | ||
const allDraftFiles = await fs.readdir(draftsDir) | ||
const allDraftIds = allDraftFiles.map((filename) => { | ||
return filename.replace(/\.json$/, '') | ||
}) | ||
draftIdList = allDraftIds | ||
} | ||
|
||
initDrafts() | ||
.then(() => { | ||
console.log('drafs ready') | ||
}) | ||
.catch((e) => { | ||
console.error('error preparing drafts', e) | ||
}) | ||
|
||
export const draftsApi = t.router({ | ||
list: t.procedure.query(async () => { | ||
return draftIdList | ||
}), | ||
get: t.procedure.input(z.string()).query(async ({input}) => { | ||
const draftPath = join(draftsDir, `${input}.json`) | ||
try { | ||
const draft = JSON.parse(await fs.readFile(draftPath, 'utf-8')) | ||
return draft | ||
} catch (e) { | ||
return null | ||
} | ||
}), | ||
write: t.procedure | ||
.input( | ||
z.object({ | ||
draft: z.any(), | ||
id: z.string(), | ||
}), | ||
) | ||
.mutation(async ({input}) => { | ||
const draftPath = join(draftsDir, `${input.id}.json`) | ||
if (!draftIdList?.includes(input.id)) { | ||
draftIdList?.push(input.id) | ||
} | ||
await fs.writeFile(draftPath, JSON.stringify(input.draft, null, 2)) | ||
}), | ||
delete: t.procedure.input(z.string()).mutation(async ({input}) => { | ||
const draftPath = join(draftsDir, `${input}.json`) | ||
draftIdList = draftIdList?.filter((id) => id !== input) | ||
await fs.unlink(draftPath) | ||
}), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.