Skip to content

Commit

Permalink
chore: optimize code logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ourongxing committed Sep 6, 2024
1 parent 9751968 commit cdb78b0
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 45 deletions.
3 changes: 1 addition & 2 deletions app/components/ExportAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export function ExportAction() {
})
} else {
setExportState({ ...exportState, range: "all", status: "running", logs: [] })
// 异步的
sendJsonMessage<WSMessgae<ExportFnProps>>({
type: "export",
data: {
Expand Down Expand Up @@ -82,7 +81,7 @@ export function ExportAction() {

else
return "idle"
}, [exportState])
}, [exportState, databaseStatus])
const allStatus = useMemo(() => {
if (!databaseStatus.maimemo_base || (exportState.status === "running" && exportState.range === "selected"))
return "disabled"
Expand Down
37 changes: 6 additions & 31 deletions src/db.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from "node:path"
import fs, { ensureDir } from "fs-extra"
import fs from "fs-extra"
import Database from "libsql"
import { databaseDir, exportedDir } from "./dir"
import { type DatabaseStatus, type Target, targets } from "@/types"
import { databaseDir } from "./dir"
import type { DatabaseStatus } from "@/types"

const dbOpt = {
readonly: true,
Expand Down Expand Up @@ -45,33 +45,8 @@ function r(f: string) {
}

export function checkDatabases() {
if (databases.maimemo_base?.db === undefined)
databases.maimemo_base = c(r("maimemo_base.db"), `SELECT * FROM "BK_VOC_TB" ORDER BY "id" LIMIT 2`)
if (databases.maimemo_cloud?.db === undefined)
databases.maimemo_cloud = c(r("maimemo_cloud.db"), `SELECT * FROM "notepad" ORDER BY "id" LIMIT 2`)
if (databases.ecdict?.db === undefined)
databases.ecdict = c(r("ecdict_ultimate.db"), `SELECT * FROM "stardict" ORDER BY "id" LIMIT 2`)
databases.maimemo_base = c(r("maimemo_base.db"), `SELECT * FROM "BK_VOC_TB" ORDER BY "id" LIMIT 2`)
databases.maimemo_cloud = c(r("maimemo_cloud.db"), `SELECT * FROM "notepad" ORDER BY "id" LIMIT 2`)
databases.ecdict = c(r("ecdict_ultimate.db"), `SELECT * FROM "stardict" ORDER BY "id" LIMIT 2`)
return databases
}

export async function ensureExportedFolder() {
await ensureDir(exportedDir)
return {
path: exportedDir,
status: true,
}
}

export async function ensureTargetFolders(folder: string) {
const folders: Record<Target, string> = {
word: "",
list: "",
translation: "",
}
for (const target of targets) {
folders[target] = path.join(exportedDir, folder, target)
await ensureDir(folders[target])
}

return folders
}
24 changes: 24 additions & 0 deletions src/dir.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
import { fileURLToPath } from "node:url"
import { join } from "node:path"
import { ensureDir } from "fs-extra"
import { type Target, targets } from "@/types"

// 在 esm 中 import.meta.url 应该是模块文件原始的位置,比较方便用于定位。
export const projectDir = fileURLToPath(new URL("..", import.meta.url))
export const databaseDir = join(projectDir, "database")
export const exportedDir = join(projectDir, "exported")

export async function ensureExportedFolder() {
await ensureDir(exportedDir)
return {
path: exportedDir,
status: true,
}
}

export async function ensureTargetFolders(folder: string) {
const folders: Record<Target, string> = {
word: "",
list: "",
translation: "",
}
for (const target of targets) {
folders[target] = join(exportedDir, folder, target)
await ensureDir(folders[target])
}

return folders
}
6 changes: 3 additions & 3 deletions src/export.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { join } from "node:path"
import fs from "fs-extra"
import { getLibWords, getLibs, translateAll } from "./get"
import { checkDatabases, databases, ensureTargetFolders } from "./db"
import { getLibWords, getLibs, translateAll } from "./query"
import { databases } from "./db"
import { transform } from "./transform"
import { ensureTargetFolders } from "./dir"
import type { ExportFnProps, ExportLog, Target, TrafficLights, Word } from "@/types"

export async function exportLib({ selected, range, type, options, fnEvery }: ExportFnProps & { fnEvery: (log: ExportLog) => Promise<boolean> }) {
checkDatabases()
const targetFolders = await ensureTargetFolders(options.folderName)

let libs = selected
Expand Down
5 changes: 1 addition & 4 deletions src/get.ts → src/query.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { sql } from "./sql"
import { translateByDict } from "./translate"
import { transform } from "./transform"
import { checkDatabases, databases } from "./db"
import { databases } from "./db"
import type { LibType, Library, Target, Word } from "@/types"

let memorizedWords: string[] = []
export function getLibs(type: LibType = "base"): Library[] {
checkDatabases()
if (!databases.maimemo_base?.db)
return []
if (type === "base") {
Expand Down Expand Up @@ -41,7 +40,6 @@ export function previewLibWords({
exculedMemorized = false,
target = "word",
}: Props) {
checkDatabases()
const words = getLibWords({ id, type, exculedMemorized })
if (target === "translation") {
if (!databases.maimemo_base?.db)
Expand All @@ -62,7 +60,6 @@ export function getLibWords({
exculedMemorized = false,
}: Omit<Props, "target">): Word[] {
try {
checkDatabases()
if (!id || !databases.maimemo_base?.db)
return []
let words: Word[] = []
Expand Down
3 changes: 1 addition & 2 deletions src/translate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { checkDatabases, databases } from "./db"
import { databases } from "./db"

export function translateByDict(word: string): string {
checkDatabases()
if (databases.ecdict?.db === undefined)
throw new Error("ecdict database not found")
// 缩写
Expand Down
5 changes: 3 additions & 2 deletions trpcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { defineEventHandler, toWebRequest } from "vinxi/http"
import { initTRPC } from "@trpc/server"
import { fetchRequestHandler } from "@trpc/server/adapters/fetch"
import { z } from "zod"
import { checkDatabases, ensureExportedFolder } from "./src/db"
import { getLibs, previewLibWords } from "./src/get"
import { ensureExportedFolder } from "src/dir"
import { checkDatabases } from "./src/db"
import { getLibs, previewLibWords } from "./src/query"

const t = initTRPC.create()

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
"allowSyntheticDefaultImports": true,
"skipLibCheck": false
},
"include": ["./app/**/*.*", "./src/**/*.ts", "./shared/**/*.ts", "./scripts/*.ts", "./*.ts", "eslint.config.js"]
"include": ["./**/*.ts", "./**/*.tsx"],
"exxclude": ["node_modules/**.*"]
}

0 comments on commit cdb78b0

Please sign in to comment.