@niyamulahsan/create-deskapp 0.0.0-stage → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -2
- package/bin/create.js +136 -0
- package/package.json +14 -4
- package/template/.env +13 -0
- package/template/.env.example +13 -0
- package/template/.vscode/extensions.json +6 -0
- package/template/.vscode/settings.json +37 -0
- package/template/LICENSE +21 -0
- package/template/README.md +29 -0
- package/template/biome.json +99 -0
- package/template/drizzle.config.ts +32 -0
- package/template/gitignore-stub +19 -0
- package/template/package.json +111 -0
- package/template/scripts/build-electron.mjs +55 -0
- package/template/scripts/dev-electron.mjs +53 -0
- package/template/scripts/installer.nsh +14 -0
- package/template/src/config/app.ts +24 -0
- package/template/src/core/api/bindings.generated.ts +23 -0
- package/template/src/core/api/generate.manifest.ts +86 -0
- package/template/src/core/api/registry.ts +48 -0
- package/template/src/core/config.ts +99 -0
- package/template/src/core/database/aggregate.ts +85 -0
- package/template/src/core/database/config.ts +48 -0
- package/template/src/core/database/connection.ts +114 -0
- package/template/src/core/database/migrate-hooks.ts +84 -0
- package/template/src/core/database/paginate.ts +224 -0
- package/template/src/core/database/reset.ts +19 -0
- package/template/src/core/database/seed.ts +169 -0
- package/template/src/core/database/status.ts +32 -0
- package/template/src/core/env.ts +106 -0
- package/template/src/core/facade.ts +59 -0
- package/template/src/core/maker/db/fresh.ts +8 -0
- package/template/src/core/maker/db/generate.ts +11 -0
- package/template/src/core/maker/db/helpers.ts +96 -0
- package/template/src/core/maker/db/migrate.ts +36 -0
- package/template/src/core/maker/db/mod.ts +63 -0
- package/template/src/core/maker/db/module-migrate.ts +53 -0
- package/template/src/core/maker/db/push.ts +21 -0
- package/template/src/core/maker/db/reset.ts +6 -0
- package/template/src/core/maker/db/seed.ts +7 -0
- package/template/src/core/maker/db/status.ts +6 -0
- package/template/src/core/maker/help.ts +66 -0
- package/template/src/core/maker/make/controller.ts +27 -0
- package/template/src/core/maker/make/helpers.ts +93 -0
- package/template/src/core/maker/make/mod.ts +68 -0
- package/template/src/core/maker/make/model.ts +14 -0
- package/template/src/core/maker/make/module.ts +62 -0
- package/template/src/core/maker/make/seeder.ts +21 -0
- package/template/src/core/maker/make/test.ts +30 -0
- package/template/src/core/maker/mod.ts +83 -0
- package/template/src/core/maker/run/mod.ts +112 -0
- package/template/src/core/maker/stub.ts +24 -0
- package/template/src/core/maker/stubs/controller/controller.controller.stub +55 -0
- package/template/src/core/maker/stubs/controller/controller.schema.stub +13 -0
- package/template/src/core/maker/stubs/model/model.stub +8 -0
- package/template/src/core/maker/stubs/seeder/seeder.stub +16 -0
- package/template/src/core/maker/stubs/test/test.stub +7 -0
- package/template/src/core/maker/sync-config.ts +94 -0
- package/template/src/core/utils/password.ts +27 -0
- package/template/src/core/utils/playwright.ts +121 -0
- package/template/src/core/utils/queue.ts +59 -0
- package/template/src/core/utils/scheduler.ts +70 -0
- package/template/src/core/utils/storage.ts +599 -0
- package/template/src/core/utils/validation.ts +35 -0
- package/template/src/database/migrations/sqlite/meta/_journal.json +1 -0
- package/template/src/database/schema.ts +6 -0
- package/template/src/icons/apple-touch-icon.png +0 -0
- package/template/src/icons/deskapp.png +0 -0
- package/template/src/icons/favicon-96x96.png +0 -0
- package/template/src/icons/favicon.ico +0 -0
- package/template/src/icons/favicon.svg +1 -0
- package/template/src/icons/site.webmanifest +21 -0
- package/template/src/icons/tray.png +0 -0
- package/template/src/icons/web-app-manifest-192x192.png +0 -0
- package/template/src/icons/web-app-manifest-512x512.png +0 -0
- package/template/src/main.ts +116 -0
- package/template/src/modules/app/controllers/demo.controller.ts +118 -0
- package/template/src/modules/app/controllers/net.controller.ts +48 -0
- package/template/src/modules/app/controllers/playwright.controller.ts +140 -0
- package/template/src/modules/app/controllers/storage.controller.ts +229 -0
- package/template/src/modules/app/controllers/window.controller.ts +107 -0
- package/template/src/preload.ts +35 -0
- package/template/src/storage/.gitkeep +1 -0
- package/template/src/storage/app/private/.gitkeep +1 -0
- package/template/src/storage/app/private/samples/.gitkeep +1 -0
- package/template/src/storage/app/private/samples/state.json +5 -0
- package/template/src/storage/app/private/uploads/.gitkeep +1 -0
- package/template/src/storage/app/public/.gitkeep +1 -0
- package/template/src/storage/app/tmp/generated/.gitkeep +1 -0
- package/template/src/storage/logs/.gitkeep +0 -0
- package/template/src/storage/logs/app.log +0 -0
- package/template/src/storage/logs/fatal.log +0 -0
- package/template/src/ui/.vscode/extensions.json +3 -0
- package/template/src/ui/index.html +14 -0
- package/template/src/ui/public/icons.svg +24 -0
- package/template/src/ui/src/App.vue +3 -0
- package/template/src/ui/src/app.ts +23 -0
- package/template/src/ui/src/assets/fonts/public-sans/public-sans-latin-ext.woff2 +0 -0
- package/template/src/ui/src/assets/fonts/public-sans/public-sans-latin.woff2 +0 -0
- package/template/src/ui/src/assets/images/deskapp.png +0 -0
- package/template/src/ui/src/assets/images/favicon/favicon.ico +0 -0
- package/template/src/ui/src/assets/scss/custom.scss +186 -0
- package/template/src/ui/src/bindings.d.ts +16 -0
- package/template/src/ui/src/components/Button.vue +32 -0
- package/template/src/ui/src/components/Checkbox.vue +29 -0
- package/template/src/ui/src/components/Input.vue +227 -0
- package/template/src/ui/src/components/Modal.vue +55 -0
- package/template/src/ui/src/components/Spinner.vue +42 -0
- package/template/src/ui/src/components/Switch.vue +65 -0
- package/template/src/ui/src/components/TextArea.vue +121 -0
- package/template/src/ui/src/components/Toast.vue +56 -0
- package/template/src/ui/src/components/datatable/DataTableSkeleton.vue +98 -0
- package/template/src/ui/src/components/datatable/Pagination.vue +125 -0
- package/template/src/ui/src/components/datatable/SelectOpption.vue +35 -0
- package/template/src/ui/src/components/datatable/index.vue +258 -0
- package/template/src/ui/src/env.d.ts +13 -0
- package/template/src/ui/src/global.d.ts +35 -0
- package/template/src/ui/src/helpers/nformatter.ts +10 -0
- package/template/src/ui/src/pages/dashboard/index.vue +687 -0
- package/template/src/ui/src/plugins/gum.ts +549 -0
- package/template/src/ui/src/plugins/gum.ts.bak +683 -0
- package/template/src/ui/src/plugins/pulse.ts +148 -0
- package/template/src/ui/src/router/index.ts +25 -0
- package/template/src/ui/src/stores/example.ts +27 -0
- package/template/src/ui/tsconfig.json +15 -0
- package/template/src/ui/vite.config.ts +38 -0
- package/template/src/window/config.ts +71 -0
- package/template/src/window/dialogs.ts +67 -0
- package/template/src/window/manager.ts +139 -0
- package/template/src/window/native.ts +215 -0
- package/template/tsconfig.json +22 -0
- package/template/vitest.config.ts +34 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const root = path.resolve(__dirname, "..");
|
|
7
|
+
|
|
8
|
+
function run(cmd, args, opts = {}) {
|
|
9
|
+
const child = spawn(cmd, args, {
|
|
10
|
+
cwd: root,
|
|
11
|
+
stdio: "inherit",
|
|
12
|
+
shell: process.platform === "win32",
|
|
13
|
+
...opts
|
|
14
|
+
});
|
|
15
|
+
return child;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// 1) Build main+preload once, then watch
|
|
19
|
+
const build = run("node", [path.resolve(root, "scripts/build-electron.mjs"), "--watch"]);
|
|
20
|
+
|
|
21
|
+
let electron = null;
|
|
22
|
+
let started = false;
|
|
23
|
+
|
|
24
|
+
function startElectron() {
|
|
25
|
+
if (started) return;
|
|
26
|
+
started = true;
|
|
27
|
+
const electronBin = path.resolve(root, "node_modules/electron/dist/electron.exe");
|
|
28
|
+
const electronCmd = process.platform === "win32" && electronBin ? electronBin : "npx";
|
|
29
|
+
const args =
|
|
30
|
+
process.platform === "win32" && electronBin
|
|
31
|
+
? [path.resolve(root, "out/main/index.cjs")]
|
|
32
|
+
: ["electron", path.resolve(root, "out/main/index.cjs")];
|
|
33
|
+
|
|
34
|
+
electron = spawn(process.platform === "win32" && electronBin ? electronBin : "npx", args, {
|
|
35
|
+
cwd: root,
|
|
36
|
+
stdio: "inherit",
|
|
37
|
+
shell: !(process.platform === "win32" && electronBin),
|
|
38
|
+
env: { ...process.env, NODE_ENV: "development" }
|
|
39
|
+
});
|
|
40
|
+
electron.on("close", (code) => {
|
|
41
|
+
build.kill();
|
|
42
|
+
process.exit(code ?? 0);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Wait for Vite (started by concurrently) and initial build - give esbuild a moment
|
|
47
|
+
setTimeout(startElectron, 2500);
|
|
48
|
+
|
|
49
|
+
process.on("SIGINT", () => {
|
|
50
|
+
electron?.kill();
|
|
51
|
+
build.kill();
|
|
52
|
+
process.exit(0);
|
|
53
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
; Custom NSIS include for electron-builder (generated from src/config/app.ts via config:sync)
|
|
2
|
+
; Adds an uninstall choice: full uninstall (remove user data) or just uninstall (keep user data).
|
|
3
|
+
|
|
4
|
+
!macro customUnInstall
|
|
5
|
+
MessageBox MB_YESNO|MB_ICONQUESTION "Do you want to completely remove all Deskapp data and settings as well?$\r$\n$\r$\nYes = Full uninstall (remove app data)$\r$\nNo = Just uninstall (keep your data/settings)" /SD IDNO IDNO keepData
|
|
6
|
+
; Full uninstall: remove Electron user data and common app data locations
|
|
7
|
+
RMDir /r "$APPDATA\Deskapp"
|
|
8
|
+
RMDir /r "$LOCALAPPDATA\Deskapp"
|
|
9
|
+
RMDir /r "$APPDATA\deskapp"
|
|
10
|
+
RMDir /r "$LOCALAPPDATA\deskapp"
|
|
11
|
+
RMDir /r "$APPDATA\com.deskapp.app"
|
|
12
|
+
RMDir /r "$LOCALAPPDATA\com.deskapp.app"
|
|
13
|
+
keepData:
|
|
14
|
+
!macroend
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* App identity config — edit this file to name/brand your app (0 hassle).
|
|
3
|
+
* After changing, run: `npm run config:sync` (or `npm run maker -- config:sync`)
|
|
4
|
+
* to update package.json + NSIS installer fields. Runtime config also reads from here.
|
|
5
|
+
*
|
|
6
|
+
* You can add more config files in this folder later (e.g. database.ts, window.ts)
|
|
7
|
+
* and export them from here or import directly.
|
|
8
|
+
*/
|
|
9
|
+
export const appConfig = {
|
|
10
|
+
/** npm package name (kebab-case) */
|
|
11
|
+
name: "deskapp",
|
|
12
|
+
/** Display / product name (installer name, app title, shortcuts) */
|
|
13
|
+
productName: "Deskapp",
|
|
14
|
+
/** Short description */
|
|
15
|
+
description: "Deskapp - Electron desktop app framework",
|
|
16
|
+
/** Unique app/protocol identifier (reverse-domain style) */
|
|
17
|
+
appId: "com.deskapp.app",
|
|
18
|
+
/** Deep-link/protocol scheme (lowercase, no spaces) */
|
|
19
|
+
protocol: "deskapp",
|
|
20
|
+
/** Folders used by the NSIS "Full uninstall" option (user data). Keep in sync with actual storage/userData names. */
|
|
21
|
+
uninstallDataFolders: ["Deskapp", "deskapp", "com.deskapp.app"],
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
export type AppConfig = typeof appConfig;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// AUTO-GENERATED by `npm run maker -- bindings:gen`. Do not edit.
|
|
2
|
+
// Regenerates the controller list so packaged builds don't rely on scanning
|
|
3
|
+
// the filesystem at runtime.
|
|
4
|
+
|
|
5
|
+
import * as mod0 from "@/modules/app/controllers/demo.controller";
|
|
6
|
+
import * as mod1 from "@/modules/app/controllers/net.controller";
|
|
7
|
+
import * as mod2 from "@/modules/app/controllers/playwright.controller";
|
|
8
|
+
import * as mod3 from "@/modules/app/controllers/storage.controller";
|
|
9
|
+
import * as mod4 from "@/modules/app/controllers/window.controller";
|
|
10
|
+
|
|
11
|
+
export interface GeneratedController {
|
|
12
|
+
module: string;
|
|
13
|
+
controller: string;
|
|
14
|
+
mod: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const GENERATED_CONTROLLERS: GeneratedController[] = [
|
|
18
|
+
{ module: "app", controller: "demo", mod: mod0 },
|
|
19
|
+
{ module: "app", controller: "net", mod: mod1 },
|
|
20
|
+
{ module: "app", controller: "playwright", mod: mod2 },
|
|
21
|
+
{ module: "app", controller: "storage", mod: mod3 },
|
|
22
|
+
{ module: "app", controller: "window", mod: mod4 }
|
|
23
|
+
];
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
|
|
8
|
+
// Project root: src/core/api -> ../../..
|
|
9
|
+
const PROJECT_ROOT = path.resolve(__dirname, "..", "..", "..");
|
|
10
|
+
const MODULES_DIR = path.join(PROJECT_ROOT, "src", "modules");
|
|
11
|
+
const OUT_FILE = path.join(PROJECT_ROOT, "src", "core", "api", "bindings.generated.ts");
|
|
12
|
+
|
|
13
|
+
interface ControllerEntry {
|
|
14
|
+
module: string;
|
|
15
|
+
controller: string;
|
|
16
|
+
importPath: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function collectControllers(): ControllerEntry[] {
|
|
20
|
+
const entries: ControllerEntry[] = [];
|
|
21
|
+
if (!fs.existsSync(MODULES_DIR)) return entries;
|
|
22
|
+
|
|
23
|
+
for (const modDirent of fs.readdirSync(MODULES_DIR, { withFileTypes: true })) {
|
|
24
|
+
if (!modDirent.isDirectory()) continue;
|
|
25
|
+
const moduleName = modDirent.name;
|
|
26
|
+
const controllersDir = path.join(MODULES_DIR, moduleName, "controllers");
|
|
27
|
+
if (!fs.existsSync(controllersDir)) continue;
|
|
28
|
+
|
|
29
|
+
for (const file of fs.readdirSync(controllersDir, { withFileTypes: true })) {
|
|
30
|
+
if (!file.isFile() || !file.name.endsWith(".controller.ts")) continue;
|
|
31
|
+
const controller = file.name.replace(/\.controller\.ts$/, "");
|
|
32
|
+
entries.push({
|
|
33
|
+
module: moduleName,
|
|
34
|
+
controller,
|
|
35
|
+
importPath: `@/modules/${moduleName}/controllers/${controller}.controller`,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return entries.sort((a, b) =>
|
|
41
|
+
`${a.module}.${a.controller}`.localeCompare(`${b.module}.${b.controller}`),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function generate(): void {
|
|
46
|
+
const controllers = collectControllers();
|
|
47
|
+
const imports = controllers
|
|
48
|
+
.map((c, i) => `import * as mod${i} from "${c.importPath}";`)
|
|
49
|
+
.join("\n");
|
|
50
|
+
const items = controllers
|
|
51
|
+
.map((c, i) => ` { module: "${c.module}", controller: "${c.controller}", mod: mod${i} }`)
|
|
52
|
+
.join(",\n");
|
|
53
|
+
|
|
54
|
+
const content = `// AUTO-GENERATED by \`npm run maker -- bindings:gen\`. Do not edit.
|
|
55
|
+
// Regenerates the controller list so packaged builds don't rely on scanning
|
|
56
|
+
// the filesystem at runtime.
|
|
57
|
+
|
|
58
|
+
${imports || "// no controllers found"}
|
|
59
|
+
|
|
60
|
+
export interface GeneratedController {
|
|
61
|
+
module: string;
|
|
62
|
+
controller: string;
|
|
63
|
+
mod: Record<string, unknown>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const GENERATED_CONTROLLERS: GeneratedController[] = [
|
|
67
|
+
${items}
|
|
68
|
+
];
|
|
69
|
+
`;
|
|
70
|
+
|
|
71
|
+
fs.mkdirSync(path.dirname(OUT_FILE), { recursive: true });
|
|
72
|
+
fs.writeFileSync(OUT_FILE, content, "utf8");
|
|
73
|
+
console.log(`[bindings:gen] wrote ${path.relative(PROJECT_ROOT, OUT_FILE)} (${controllers.length} controller(s))`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function isMain(): boolean {
|
|
77
|
+
try {
|
|
78
|
+
return process.argv[1] && path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url));
|
|
79
|
+
} catch {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (isMain()) generate();
|
|
85
|
+
|
|
86
|
+
export { collectControllers, generate };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ipcMain } from "electron";
|
|
2
|
+
import { GENERATED_CONTROLLERS } from "@/core/api/bindings.generated";
|
|
3
|
+
|
|
4
|
+
/** A callable handler registered for the frontend. */
|
|
5
|
+
export type BoundHandler = (...args: unknown[]) => unknown;
|
|
6
|
+
|
|
7
|
+
/** A controller module exposing a `handlers` map for registration. */
|
|
8
|
+
interface ControllerModule {
|
|
9
|
+
handlers?: Record<string, BoundHandler>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Collect every registered handler across modules, keyed by
|
|
14
|
+
* `<module>.<controller>.<handler>` (e.g., `auth.role.index`).
|
|
15
|
+
*/
|
|
16
|
+
export function collectHandlers(): Map<string, BoundHandler> {
|
|
17
|
+
const handlers = new Map<string, BoundHandler>();
|
|
18
|
+
|
|
19
|
+
for (const entry of GENERATED_CONTROLLERS) {
|
|
20
|
+
const mod = entry.mod as ControllerModule;
|
|
21
|
+
if (!mod.handlers) continue;
|
|
22
|
+
for (const key of Object.keys(mod.handlers)) {
|
|
23
|
+
handlers.set(`${entry.module}.${entry.controller}.${key}`, mod.handlers[key]);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return handlers;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Register all handlers with Electron's ipcMain.
|
|
32
|
+
* In Electron, we don't bind to a specific window; we bind globally.
|
|
33
|
+
*/
|
|
34
|
+
export function bindAll(): void {
|
|
35
|
+
const handlers = collectHandlers();
|
|
36
|
+
for (const [name, fn] of handlers) {
|
|
37
|
+
ipcMain.handle(`deskapp:${name}`, async (_event, ...args: unknown[]) => {
|
|
38
|
+
return await fn(...args);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
// Sync channel so preload can expose a concrete, cloneable bindings object.
|
|
42
|
+
ipcMain.removeHandler?.("deskapp:__list__" as never);
|
|
43
|
+
ipcMain.on("deskapp:__list__", (event) => {
|
|
44
|
+
event.returnValue = [...handlers.keys()];
|
|
45
|
+
});
|
|
46
|
+
ipcMain.handle("deskapp:__list__", async () => [...handlers.keys()]);
|
|
47
|
+
console.log(`[bindings] registered ${handlers.size} IPC handler(s)`);
|
|
48
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* config.ts - runtime framework configuration.
|
|
3
|
+
*
|
|
4
|
+
* Reads app identity from src/config/app.ts (edit that file for 0-hassle naming),
|
|
5
|
+
* with safe fallback to package.json.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import { createRequire } from "node:module";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { appConfig } from "@config/app";
|
|
12
|
+
|
|
13
|
+
interface PackageJsonConfig {
|
|
14
|
+
name?: string;
|
|
15
|
+
productName?: string;
|
|
16
|
+
build?: {
|
|
17
|
+
productName?: string;
|
|
18
|
+
mac?: { icon?: string };
|
|
19
|
+
win?: { icon?: string };
|
|
20
|
+
linux?: { icon?: string };
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function getElectronApp(): any {
|
|
25
|
+
try {
|
|
26
|
+
const req = typeof require !== "undefined" ? require : createRequire(process.cwd() + "/");
|
|
27
|
+
const e = req("electron");
|
|
28
|
+
if (e && typeof e === "object" && typeof e.app?.getPath === "function") return e.app;
|
|
29
|
+
} catch {}
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The project's package.json, read once and cached (fallback / extra build metadata). */
|
|
34
|
+
let pkgJsonCache: PackageJsonConfig | null | undefined;
|
|
35
|
+
function readPackageJson(): PackageJsonConfig | undefined {
|
|
36
|
+
if (pkgJsonCache !== undefined) return pkgJsonCache;
|
|
37
|
+
|
|
38
|
+
const candidates: string[] = [];
|
|
39
|
+
const app = getElectronApp();
|
|
40
|
+
try {
|
|
41
|
+
if (app?.getAppPath) candidates.push(join(app.getAppPath(), "package.json"));
|
|
42
|
+
} catch {}
|
|
43
|
+
try {
|
|
44
|
+
if (app?.isPackaged) candidates.push(join(dirname(app.getPath("exe")), "package.json"));
|
|
45
|
+
} catch {}
|
|
46
|
+
candidates.push(join(process.cwd(), "package.json"));
|
|
47
|
+
|
|
48
|
+
// Walk up from cwd to find package.json (CLI/dev safety)
|
|
49
|
+
let dir = process.cwd();
|
|
50
|
+
for (let i = 0; i < 6; i++) {
|
|
51
|
+
candidates.push(join(dir, "package.json"));
|
|
52
|
+
const parent = dirname(dir);
|
|
53
|
+
if (parent === dir) break;
|
|
54
|
+
dir = parent;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const p of candidates) {
|
|
58
|
+
try {
|
|
59
|
+
if (fs.existsSync(p)) {
|
|
60
|
+
pkgJsonCache = JSON.parse(fs.readFileSync(p, "utf-8")) as PackageJsonConfig;
|
|
61
|
+
return pkgJsonCache;
|
|
62
|
+
}
|
|
63
|
+
} catch {}
|
|
64
|
+
}
|
|
65
|
+
pkgJsonCache = null;
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Parse a boolean env var: "1"/"true"/"yes"/"on" are on, anything else off. */
|
|
70
|
+
export function envBool(name: string, fallback: boolean): boolean {
|
|
71
|
+
const value = process.env[name];
|
|
72
|
+
if (value === undefined) return fallback;
|
|
73
|
+
return ["1", "true", "yes", "on"].includes(value.trim().toLowerCase());
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const uiEnabled = () => envBool("DESKAPP_UI", true);
|
|
77
|
+
|
|
78
|
+
const APP_NAME_FALLBACK = "Deskapp";
|
|
79
|
+
const pkg = readPackageJson();
|
|
80
|
+
|
|
81
|
+
// Prefer src/config/app.ts (zero-hassle), fall back to package.json.
|
|
82
|
+
const appName = appConfig?.productName || pkg?.build?.productName || pkg?.productName || pkg?.name || APP_NAME_FALLBACK;
|
|
83
|
+
const appKebab = (appConfig?.name || appName)
|
|
84
|
+
.toLowerCase()
|
|
85
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
86
|
+
.replace(/^-+|-+$/g, "");
|
|
87
|
+
|
|
88
|
+
export const config = {
|
|
89
|
+
/** Display name (src/config/app.ts > package.json, fallback "Deskapp"). */
|
|
90
|
+
appName,
|
|
91
|
+
/** Filesystem-safe kebab form — sqlite filename, linux binary name. */
|
|
92
|
+
appKebab,
|
|
93
|
+
/** Per-OS output paths from package.json build config */
|
|
94
|
+
desktopOutput: pkg?.build ?? null,
|
|
95
|
+
ui: {
|
|
96
|
+
/** True when the frontend should be served and a window opened. */
|
|
97
|
+
enabled: uiEnabled(),
|
|
98
|
+
},
|
|
99
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const SRC_PATH = path.resolve(process.cwd(), "src");
|
|
6
|
+
const SCHEMA_PATH = path.join(SRC_PATH, "database", "schema.ts");
|
|
7
|
+
|
|
8
|
+
export function walkTs(dir: string, out: string[]): void {
|
|
9
|
+
try {
|
|
10
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
11
|
+
const full = path.join(dir, entry.name);
|
|
12
|
+
if (entry.isDirectory()) walkTs(full, out);
|
|
13
|
+
else if (entry.isFile() && (entry.name.endsWith(".ts") || entry.name.endsWith(".tsx"))) out.push(full);
|
|
14
|
+
}
|
|
15
|
+
} catch {
|
|
16
|
+
// path does not exist - skip
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function discoverModelFiles(srcPath = SRC_PATH): string[] {
|
|
21
|
+
const modelFiles: string[] = [];
|
|
22
|
+
const modulesDir = path.join(srcPath, "modules");
|
|
23
|
+
const coreDir = path.join(srcPath, "core");
|
|
24
|
+
|
|
25
|
+
if (fs.existsSync(modulesDir)) {
|
|
26
|
+
for (const moduleDir of fs.readdirSync(modulesDir, { withFileTypes: true })) {
|
|
27
|
+
if (!moduleDir.isDirectory()) continue;
|
|
28
|
+
walkTs(path.join(moduleDir.name ? path.join(modulesDir, moduleDir.name) : "", "database", "models"), modelFiles);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// fix: above had a bug in my quick logic; do it clearly
|
|
32
|
+
modelFiles.length = 0;
|
|
33
|
+
if (fs.existsSync(modulesDir)) {
|
|
34
|
+
for (const moduleDir of fs.readdirSync(modulesDir, { withFileTypes: true })) {
|
|
35
|
+
if (!moduleDir.isDirectory()) continue;
|
|
36
|
+
walkTs(path.join(modulesDir, moduleDir.name, "database", "models"), modelFiles);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (fs.existsSync(coreDir)) {
|
|
40
|
+
for (const areaDir of fs.readdirSync(coreDir, { withFileTypes: true })) {
|
|
41
|
+
if (!areaDir.isDirectory()) continue;
|
|
42
|
+
walkTs(path.join(coreDir, areaDir.name, "database", "models"), modelFiles);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return modelFiles.sort();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function generateSchemaFile(srcPath = SRC_PATH, schemaPath = SCHEMA_PATH): string[] {
|
|
49
|
+
const modelFiles = discoverModelFiles(srcPath);
|
|
50
|
+
const modelExports = modelFiles
|
|
51
|
+
.map((f) => {
|
|
52
|
+
const rel = path.relative(srcPath, f).replace(/\\/g, "/");
|
|
53
|
+
const ext = rel.endsWith(".tsx") ? ".tsx" : ".ts";
|
|
54
|
+
const base = rel.slice(0, -ext.length);
|
|
55
|
+
return `export * from "../${base}${ext}";`;
|
|
56
|
+
})
|
|
57
|
+
.join("\n");
|
|
58
|
+
|
|
59
|
+
const content = `/**
|
|
60
|
+
* schema.ts - GENERATED by \`npm run db:schema\`. Do NOT edit by hand.
|
|
61
|
+
* Aggregates every module + core database/models/*.ts into one Drizzle schema.
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
${modelExports || "export {};"}
|
|
65
|
+
`;
|
|
66
|
+
fs.mkdirSync(path.dirname(schemaPath), { recursive: true });
|
|
67
|
+
fs.mkdirSync(path.join(srcPath, "storage"), { recursive: true });
|
|
68
|
+
fs.writeFileSync(schemaPath, content);
|
|
69
|
+
return modelFiles;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isMain(): boolean {
|
|
73
|
+
try {
|
|
74
|
+
const arg = process.argv[1];
|
|
75
|
+
if (!arg) return false;
|
|
76
|
+
return path.resolve(arg) === path.resolve(fileURLToPath(import.meta.url));
|
|
77
|
+
} catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (isMain()) {
|
|
83
|
+
const found = generateSchemaFile();
|
|
84
|
+
console.log(`[db:schema] wrote ${SCHEMA_PATH} (${found.length} model(s))`);
|
|
85
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { isAbsolute, join } from "node:path";
|
|
2
|
+
import { config } from "@/core/config";
|
|
3
|
+
import { appBaseDir } from "@/core/env";
|
|
4
|
+
|
|
5
|
+
function getElectronApp(): { getPath: (name: string) => string; isPackaged: boolean } | undefined {
|
|
6
|
+
try {
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
8
|
+
const e = require("electron") as any;
|
|
9
|
+
if (e && typeof e.app?.getPath === "function") return e.app;
|
|
10
|
+
} catch {}
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function getAppBaseDir(): string {
|
|
15
|
+
try {
|
|
16
|
+
return appBaseDir();
|
|
17
|
+
} catch {
|
|
18
|
+
return process.cwd();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The URI (scheme + path) for the local SQLite file. */
|
|
23
|
+
export function resolveDatabaseUrl(): string {
|
|
24
|
+
const raw = (process.env.DATABASE_URL ?? "").trim();
|
|
25
|
+
|
|
26
|
+
// Default: Store in the OS-specific user data folder when running in Electron.
|
|
27
|
+
if (!raw) {
|
|
28
|
+
const electronApp = getElectronApp();
|
|
29
|
+
if (electronApp) {
|
|
30
|
+
return `sqlite:${join(electronApp.getPath("userData"), `${config.appKebab}.sqlite`)}`;
|
|
31
|
+
}
|
|
32
|
+
// CLI / non-Electron fallback
|
|
33
|
+
return `sqlite:${join(getAppBaseDir(), "src", "storage", `${config.appKebab}.sqlite`)}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// In-memory databases and remote libsql URLs are used as-is.
|
|
37
|
+
if (raw.includes(":memory:")) return raw;
|
|
38
|
+
if (raw.includes("://")) return raw;
|
|
39
|
+
|
|
40
|
+
const body = raw.replace(/^(sqlite|file):/, "");
|
|
41
|
+
const anchored = body && !isAbsolute(body) ? join(getAppBaseDir(), body) : body;
|
|
42
|
+
return `${raw.startsWith("file:") ? "file" : "sqlite"}:${anchored}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The local SQLite file path (strips the `sqlite:`/`file:` scheme). */
|
|
46
|
+
export function sqliteFile(): string {
|
|
47
|
+
return resolveDatabaseUrl().replace(/^(sqlite|file):/, "");
|
|
48
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { Client as LibSqlClient } from "@libsql/client";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import * as schema from "@/database/schema";
|
|
5
|
+
import { sqliteFile } from "@/core/database/config";
|
|
6
|
+
|
|
7
|
+
let databaseInstance: DrizzleDatabase | null = null;
|
|
8
|
+
let databaseClient: LibSqlClient | null = null;
|
|
9
|
+
|
|
10
|
+
export type DrizzleDatabase = import("drizzle-orm/libsql").LibSQLDatabase<typeof schema>;
|
|
11
|
+
|
|
12
|
+
function resolveMigrationsDir(): string {
|
|
13
|
+
try {
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
15
|
+
const e = require("electron") as any;
|
|
16
|
+
if (typeof e?.app?.getAppPath === "function") {
|
|
17
|
+
const dir = path.join(e.app.getAppPath(), "src", "database", "migrations", "sqlite");
|
|
18
|
+
if (require("node:fs").existsSync(dir)) return dir;
|
|
19
|
+
}
|
|
20
|
+
} catch {}
|
|
21
|
+
// CLI/dev fallback: find project root
|
|
22
|
+
let dir = process.cwd();
|
|
23
|
+
for (let i = 0; i < 6; i++) {
|
|
24
|
+
const candidate = path.join(dir, "src", "database", "migrations", "sqlite");
|
|
25
|
+
if (require("node:fs").existsSync(path.join(dir, "package.json"))) return candidate;
|
|
26
|
+
const parent = path.dirname(dir);
|
|
27
|
+
if (parent === dir) break;
|
|
28
|
+
dir = parent;
|
|
29
|
+
}
|
|
30
|
+
return path.join(process.cwd(), "src", "database", "migrations", "sqlite");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function initDatabase(): Promise<DrizzleDatabase> {
|
|
34
|
+
if (databaseInstance) return databaseInstance;
|
|
35
|
+
|
|
36
|
+
const filePath = sqliteFile();
|
|
37
|
+
await createFolderFor(filePath);
|
|
38
|
+
|
|
39
|
+
databaseInstance = await connectWithLibSql(filePath);
|
|
40
|
+
await runBundledMigrations();
|
|
41
|
+
|
|
42
|
+
return databaseInstance;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function runBundledMigrations(): Promise<void> {
|
|
46
|
+
const folder = resolveMigrationsDir();
|
|
47
|
+
let journal: { entries?: { tag: string }[] };
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const journalRaw = await fs.readFile(path.join(folder, "meta", "_journal.json"), "utf-8");
|
|
51
|
+
journal = JSON.parse(journalRaw);
|
|
52
|
+
} catch {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!journal.entries?.length || !databaseClient) return;
|
|
57
|
+
if ((await countTables()) > 0) return;
|
|
58
|
+
|
|
59
|
+
for (const entry of journal.entries) {
|
|
60
|
+
try {
|
|
61
|
+
const step = await fs.readFile(path.join(folder, `${entry.tag}.sql`), "utf-8");
|
|
62
|
+
await databaseClient.executeMultiple(step);
|
|
63
|
+
} catch (err) {
|
|
64
|
+
console.error(`[db] Failed to run migration ${entry.tag}`, err);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function countTables(): Promise<number> {
|
|
70
|
+
const sql = "select count(*) as n from sqlite_master where type='table' and name not like 'sqlite_%' and name not like '__drizzle_%'";
|
|
71
|
+
const result = await databaseClient!.execute(sql);
|
|
72
|
+
const row = result.rows[0] as { n?: bigint | number };
|
|
73
|
+
return Number(row?.n ?? 0);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function connectWithLibSql(filePath: string): Promise<DrizzleDatabase> {
|
|
77
|
+
const { createClient } = await import("@libsql/client");
|
|
78
|
+
const { drizzle } = await import("drizzle-orm/libsql");
|
|
79
|
+
|
|
80
|
+
// libsql requires forward slashes even on Windows
|
|
81
|
+
const normalizedPath = filePath.replace(/\\/g, "/");
|
|
82
|
+
const client = createClient({ url: `file:${normalizedPath}` });
|
|
83
|
+
databaseClient = client;
|
|
84
|
+
|
|
85
|
+
return drizzle(client, { schema }) as unknown as DrizzleDatabase;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function database(): DrizzleDatabase {
|
|
89
|
+
if (!databaseInstance) throw new Error("Database is not initialized.");
|
|
90
|
+
return databaseInstance;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export const db: DrizzleDatabase & {
|
|
94
|
+
init: typeof initDatabase;
|
|
95
|
+
close: typeof closeDatabase;
|
|
96
|
+
} = new Proxy({} as any, {
|
|
97
|
+
get(_target, property) {
|
|
98
|
+
if (property === "init") return initDatabase;
|
|
99
|
+
if (property === "close") return closeDatabase;
|
|
100
|
+
const value = database()[property as keyof DrizzleDatabase];
|
|
101
|
+
return typeof value === "function" ? value.bind(database()) : value;
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
export function closeDatabase(): void {
|
|
106
|
+
if (databaseClient) databaseClient.close();
|
|
107
|
+
databaseClient = null;
|
|
108
|
+
databaseInstance = null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function createFolderFor(filePath: string): Promise<void> {
|
|
112
|
+
const dir = path.dirname(filePath);
|
|
113
|
+
await fs.mkdir(dir, { recursive: true });
|
|
114
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { sql } from "drizzle-orm";
|
|
5
|
+
import { db } from "@/core/database/connection";
|
|
6
|
+
|
|
7
|
+
interface HookMap {
|
|
8
|
+
__migrationSql: true;
|
|
9
|
+
sqlite?: string[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function isHookMap(value: unknown): value is HookMap {
|
|
13
|
+
if (!value || typeof value !== "object") return false;
|
|
14
|
+
return (value as HookMap).__migrationSql === true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function collectStatements(exportsObj: Record<string, unknown>): string[] {
|
|
18
|
+
const statements: string[] = [];
|
|
19
|
+
for (const value of Object.values(exportsObj)) {
|
|
20
|
+
if (!isHookMap(value)) continue;
|
|
21
|
+
const list = value.sqlite || [];
|
|
22
|
+
for (const statement of list) {
|
|
23
|
+
if (typeof statement === "string" && statement.trim()) statements.push(statement);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return statements;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const SRC_PATH = path.resolve(process.cwd(), "src");
|
|
30
|
+
|
|
31
|
+
function walkTs(dir: string, out: string[]): void {
|
|
32
|
+
try {
|
|
33
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
34
|
+
const full = path.join(dir, entry.name);
|
|
35
|
+
if (entry.isDirectory()) {
|
|
36
|
+
walkTs(full, out);
|
|
37
|
+
} else if (entry.isFile() && entry.name.endsWith(".ts")) {
|
|
38
|
+
out.push(full);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} catch {}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function discoverModelFiles(): string[] {
|
|
45
|
+
const files: string[] = [];
|
|
46
|
+
const modulesDir = path.join(SRC_PATH, "modules");
|
|
47
|
+
const coreDir = path.join(SRC_PATH, "core");
|
|
48
|
+
|
|
49
|
+
if (fs.existsSync(modulesDir)) {
|
|
50
|
+
for (const moduleDir of fs.readdirSync(modulesDir, { withFileTypes: true })) {
|
|
51
|
+
if (!moduleDir.isDirectory()) continue;
|
|
52
|
+
walkTs(path.join(modulesDir, moduleDir.name, "database", "models"), files);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (fs.existsSync(coreDir)) {
|
|
56
|
+
for (const areaDir of fs.readdirSync(coreDir, { withFileTypes: true })) {
|
|
57
|
+
if (!areaDir.isDirectory()) continue;
|
|
58
|
+
walkTs(path.join(coreDir, areaDir.name, "database", "models"), files);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return files.sort();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const modelFiles = discoverModelFiles();
|
|
65
|
+
await db.init();
|
|
66
|
+
|
|
67
|
+
let applied = 0;
|
|
68
|
+
try {
|
|
69
|
+
for (const file of modelFiles) {
|
|
70
|
+
const mod = await import(pathToFileURL(file).href);
|
|
71
|
+
const statements = collectStatements(mod as Record<string, unknown>);
|
|
72
|
+
if (!statements.length) continue;
|
|
73
|
+
for (const statement of statements) {
|
|
74
|
+
await db.run(sql.raw(statement));
|
|
75
|
+
}
|
|
76
|
+
applied += statements.length;
|
|
77
|
+
}
|
|
78
|
+
} finally {
|
|
79
|
+
await db.close();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (applied > 0) {
|
|
83
|
+
console.log(`Applied ${applied} model migration SQL statement(s) for sqlite.`);
|
|
84
|
+
}
|