@niyamulahsan/create-deskapp 1.2.4 → 1.2.6
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/package.json +16 -16
- package/template/.env.example +20 -13
- package/template/AGENTS.md +699 -0
- package/template/package.json +3 -0
- package/template/src/config/app.ts +26 -24
- package/template/src/config/winsetup.ts +93 -91
- package/template/src/core/api/binding-error.ts +57 -0
- package/template/src/core/api/bindings.generated.ts +2 -0
- package/template/src/core/api/generate.manifest.ts +84 -80
- package/template/src/core/api/registry.ts +58 -48
- package/template/src/core/config.ts +101 -99
- package/template/src/core/database/aggregate.ts +89 -85
- package/template/src/core/database/config.ts +50 -48
- package/template/src/core/database/connection.ts +116 -114
- package/template/src/core/database/migrate-hooks.ts +86 -84
- package/template/src/core/database/paginate.ts +226 -224
- package/template/src/core/database/reset.ts +21 -19
- package/template/src/core/database/seed.ts +169 -167
- package/template/src/core/database/status.ts +37 -35
- package/template/src/core/env.ts +108 -106
- package/template/src/core/facade.ts +63 -61
- package/template/src/core/maker/db/fresh.ts +10 -8
- package/template/src/core/maker/db/generate.ts +13 -11
- package/template/src/core/maker/db/helpers.ts +92 -96
- package/template/src/core/maker/db/migrate.ts +37 -35
- package/template/src/core/maker/db/mod.ts +93 -91
- package/template/src/core/maker/db/module-migrate.ts +56 -54
- package/template/src/core/maker/db/push.ts +23 -21
- package/template/src/core/maker/db/reset.ts +8 -6
- package/template/src/core/maker/db/seed.ts +9 -7
- package/template/src/core/maker/db/status.ts +8 -6
- package/template/src/core/maker/help.ts +59 -57
- package/template/src/core/maker/make/controller.ts +25 -22
- package/template/src/core/maker/make/helpers.ts +97 -91
- package/template/src/core/maker/make/mod.ts +69 -67
- package/template/src/core/maker/make/model.ts +16 -14
- package/template/src/core/maker/make/module.ts +53 -39
- package/template/src/core/maker/make/seeder.ts +23 -21
- package/template/src/core/maker/make/test.ts +33 -30
- package/template/src/core/maker/mod.ts +85 -83
- package/template/src/core/maker/run/mod.ts +134 -132
- package/template/src/core/maker/stub.ts +26 -24
- package/template/src/core/maker/stubs/controller/controller.controller.stub +57 -55
- package/template/src/core/maker/stubs/controller/controller.schema.stub +18 -13
- package/template/src/core/maker/stubs/facade/facade.stub +21 -0
- package/template/src/core/maker/stubs/model/model.stub +10 -8
- package/template/src/core/maker/stubs/seeder/seeder.stub +18 -16
- package/template/src/core/maker/stubs/test/test.stub +9 -7
- package/template/src/core/maker/sync-config.ts +93 -94
- package/template/src/core/utils/password.ts +29 -27
- package/template/src/core/utils/playwright.ts +123 -121
- package/template/src/core/utils/queue.ts +68 -66
- package/template/src/core/utils/scheduler.ts +62 -60
- package/template/src/core/utils/storage.ts +3 -1
- package/template/src/core/utils/validation.ts +37 -35
- package/template/src/database/schema.ts +2 -0
- package/template/src/main.ts +120 -118
- package/template/src/modules/app/__tests__/demo.controller.test.ts +75 -0
- package/template/src/modules/app/__tests__/net.controller.test.ts +65 -0
- package/template/src/modules/app/__tests__/playwright.controller.test.ts +50 -0
- package/template/src/modules/app/__tests__/storage.controller.test.ts +114 -0
- package/template/src/modules/app/__tests__/window.controller.test.ts +98 -0
- package/template/src/modules/app/controllers/demo.controller.ts +120 -118
- package/template/src/modules/app/controllers/net.controller.ts +50 -48
- package/template/src/modules/app/controllers/playwright.controller.ts +142 -140
- package/template/src/modules/app/controllers/storage.controller.ts +231 -229
- package/template/src/modules/app/controllers/window.controller.ts +2 -0
- package/template/src/modules/app/facade.ts +21 -0
- package/template/src/preload.ts +25 -2
- package/template/src/ui/src/App.vue +4 -2
- package/template/src/ui/src/app.ts +38 -23
- package/template/src/ui/src/bindings.d.ts +18 -16
- package/template/src/ui/src/components/Button.vue +34 -32
- package/template/src/ui/src/components/Checkbox.vue +26 -29
- package/template/src/ui/src/components/Input.vue +221 -227
- package/template/src/ui/src/components/InputGroup.vue +141 -0
- package/template/src/ui/src/components/InputPasswordToggle.vue +204 -0
- package/template/src/ui/src/components/Modal.vue +65 -55
- package/template/src/ui/src/components/Spinner.vue +44 -42
- package/template/src/ui/src/components/Switch.vue +62 -65
- package/template/src/ui/src/components/TextArea.vue +108 -121
- package/template/src/ui/src/components/Toast.vue +55 -56
- package/template/src/ui/src/components/datatable/DataTableSkeleton.vue +100 -98
- package/template/src/ui/src/components/datatable/Pagination.vue +153 -125
- package/template/src/ui/src/components/datatable/SelectOpption.vue +36 -35
- package/template/src/ui/src/components/datatable/index.vue +336 -258
- package/template/src/ui/src/env.d.ts +16 -13
- package/template/src/ui/src/global.d.ts +21 -2
- package/template/src/ui/src/helpers/nformatter.ts +12 -10
- package/template/src/ui/src/pages/dashboard/index.vue +690 -687
- package/template/src/ui/src/plugins/browserDetect.ts +57 -0
- package/template/src/ui/src/plugins/gum.ts +576 -549
- package/template/src/ui/src/plugins/pulse.ts +181 -147
- package/template/src/ui/src/plugins/routeProgress.ts +89 -0
- package/template/src/ui/src/router/index.ts +44 -25
- package/template/src/ui/src/stores/example.ts +2 -0
- package/template/src/ui/vite.config.ts +40 -38
- package/template/src/window/app-menu.ts +89 -87
- package/template/src/window/config.ts +14 -12
- package/template/src/window/context-menu.ts +23 -21
- package/template/src/window/dialogs.ts +92 -90
- package/template/src/window/manager.ts +2 -0
- package/template/src/window/native.ts +47 -45
- package/template/src/window/tray.ts +132 -130
|
@@ -1,167 +1,169 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
1
|
+
// ./src/core/database/seed.ts
|
|
2
|
+
|
|
3
|
+
// deno-lint-ignore-file no-explicit-any
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { pathToFileURL } from "node:url";
|
|
7
|
+
import { db } from "@/core/database/connection";
|
|
8
|
+
|
|
9
|
+
const NAME_SYMBOL = Symbol.for("drizzle:Name");
|
|
10
|
+
const FK_SYMBOLS = [Symbol.for("drizzle:SQLiteInlineForeignKeys")];
|
|
11
|
+
|
|
12
|
+
interface SeederEntry {
|
|
13
|
+
name: string;
|
|
14
|
+
module: string;
|
|
15
|
+
execute: () => Promise<void>;
|
|
16
|
+
tableName?: string;
|
|
17
|
+
dependsOn: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const SRC_PATH = path.resolve(process.cwd(), "src");
|
|
21
|
+
|
|
22
|
+
function walkTs(dir: string, out: string[]): void {
|
|
23
|
+
try {
|
|
24
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
25
|
+
const full = path.join(dir, entry.name);
|
|
26
|
+
if (entry.isDirectory()) {
|
|
27
|
+
walkTs(full, out);
|
|
28
|
+
} else if (entry.isFile() && entry.name.endsWith(".ts")) {
|
|
29
|
+
out.push(full);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
} catch {}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function getTableDeps(table: any): string[] {
|
|
36
|
+
for (const sym of FK_SYMBOLS) {
|
|
37
|
+
const fks = table?.[sym] as any[] | undefined;
|
|
38
|
+
if (!fks || fks.length === 0) continue;
|
|
39
|
+
const deps: string[] = [];
|
|
40
|
+
for (const fk of fks) {
|
|
41
|
+
const ref = fk.reference();
|
|
42
|
+
const name = ref?.foreignTable?.[NAME_SYMBOL] as string | undefined;
|
|
43
|
+
if (name && !deps.includes(name)) deps.push(name);
|
|
44
|
+
}
|
|
45
|
+
return deps;
|
|
46
|
+
}
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function discoverSeederFiles(): string[] {
|
|
51
|
+
const files: string[] = [];
|
|
52
|
+
const modulesDir = path.join(SRC_PATH, "modules");
|
|
53
|
+
const coreDir = path.join(SRC_PATH, "core");
|
|
54
|
+
|
|
55
|
+
if (fs.existsSync(modulesDir)) {
|
|
56
|
+
for (const moduleDir of fs.readdirSync(modulesDir, { withFileTypes: true })) {
|
|
57
|
+
if (!moduleDir.isDirectory()) continue;
|
|
58
|
+
walkTs(path.join(modulesDir, moduleDir.name, "database", "seeders"), files);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (fs.existsSync(coreDir)) {
|
|
62
|
+
for (const areaDir of fs.readdirSync(coreDir, { withFileTypes: true })) {
|
|
63
|
+
if (!areaDir.isDirectory()) continue;
|
|
64
|
+
walkTs(path.join(coreDir, areaDir.name, "database", "seeders"), files);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return files.sort();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function moduleNameOf(file: string): string {
|
|
71
|
+
const rel = path.relative(SRC_PATH, file).replace(/\\/g, "/");
|
|
72
|
+
const root = rel.startsWith("core/") ? "core" : "modules";
|
|
73
|
+
return rel.slice(root.length + 1).split("/")[0];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Parse `--module=blog` from process.argv
|
|
77
|
+
const moduleArg = process.argv.find((arg) => arg.startsWith("--module="));
|
|
78
|
+
const moduleName = moduleArg?.split("=")[1]?.trim();
|
|
79
|
+
|
|
80
|
+
const seedFiles = moduleName ? discoverSeederFiles().filter((f) => moduleNameOf(f) === moduleName) : discoverSeederFiles();
|
|
81
|
+
|
|
82
|
+
await db.init();
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
const seeders: SeederEntry[] = [];
|
|
86
|
+
for (const file of seedFiles) {
|
|
87
|
+
const mod = await import(pathToFileURL(file).href);
|
|
88
|
+
if (typeof mod.default !== "function") continue;
|
|
89
|
+
|
|
90
|
+
const table: any = mod.table;
|
|
91
|
+
const rawName = path.basename(file).replace(/\.ts$/, "");
|
|
92
|
+
seeders.push({
|
|
93
|
+
name: rawName.replace(/\.seed$/, ""),
|
|
94
|
+
module: moduleNameOf(file),
|
|
95
|
+
execute: () => mod.default(),
|
|
96
|
+
tableName: table?.[NAME_SYMBOL] as string | undefined,
|
|
97
|
+
dependsOn: table ? getTableDeps(table) : []
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (seeders.length === 0) {
|
|
102
|
+
console.log(`No seeders found${moduleName ? ` for module ${moduleName}` : ""}`);
|
|
103
|
+
process.exit(0);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const tableToSeeder = new Map<string, SeederEntry>();
|
|
107
|
+
for (const s of seeders) {
|
|
108
|
+
if (s.tableName) tableToSeeder.set(s.tableName, s);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const resolvedDeps = new Map<SeederEntry, SeederEntry[]>();
|
|
112
|
+
for (const s of seeders) {
|
|
113
|
+
const deps: SeederEntry[] = [];
|
|
114
|
+
for (const dep of s.dependsOn) {
|
|
115
|
+
const depSeeder = tableToSeeder.get(dep);
|
|
116
|
+
if (depSeeder && depSeeder !== s && !deps.includes(depSeeder)) {
|
|
117
|
+
deps.push(depSeeder);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
resolvedDeps.set(s, deps);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const inDegree = new Map<SeederEntry, number>();
|
|
124
|
+
const adj = new Map<SeederEntry, SeederEntry[]>();
|
|
125
|
+
|
|
126
|
+
for (const s of seeders) {
|
|
127
|
+
inDegree.set(s, 0);
|
|
128
|
+
adj.set(s, []);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (const s of seeders) {
|
|
132
|
+
for (const dep of resolvedDeps.get(s)!) {
|
|
133
|
+
if (!adj.has(dep)) continue;
|
|
134
|
+
adj.get(dep)!.push(s);
|
|
135
|
+
inDegree.set(s, inDegree.get(s)! + 1);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const queue: SeederEntry[] = [];
|
|
140
|
+
for (const [s, deg] of inDegree) {
|
|
141
|
+
if (deg === 0) queue.push(s);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const sorted: SeederEntry[] = [];
|
|
145
|
+
while (queue.length > 0) {
|
|
146
|
+
const s = queue.shift()!;
|
|
147
|
+
sorted.push(s);
|
|
148
|
+
for (const next of adj.get(s)!) {
|
|
149
|
+
const newDeg = inDegree.get(next)! - 1;
|
|
150
|
+
inDegree.set(next, newDeg);
|
|
151
|
+
if (newDeg === 0) queue.push(next);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (sorted.length !== seeders.length) {
|
|
156
|
+
const seeded = new Set(sorted.map((s) => s.name));
|
|
157
|
+
const missing = seeders.filter((s) => !seeded.has(s.name)).map((s) => s.name);
|
|
158
|
+
console.error(`Circular dependency detected among seeders: ${missing.join(", ")}`);
|
|
159
|
+
process.exit(1);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (const s of sorted) {
|
|
163
|
+
await s.execute();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
console.log(`Executed ${sorted.length} seeder file(s)${moduleName ? ` for module ${moduleName}` : ""}`);
|
|
167
|
+
} finally {
|
|
168
|
+
await db.close();
|
|
169
|
+
}
|
|
@@ -1,35 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
1
|
+
// ./src/core/database/status.ts
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
const MIGRATION_DIR = path.join(process.cwd(), "src", "database", "migrations", "sqlite");
|
|
8
|
+
|
|
9
|
+
export function listMigrations(dir = MIGRATION_DIR): string[] {
|
|
10
|
+
try {
|
|
11
|
+
return fs
|
|
12
|
+
.readdirSync(dir)
|
|
13
|
+
.filter((name) => name.endsWith(".sql"))
|
|
14
|
+
.sort();
|
|
15
|
+
} catch {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isMain(): boolean {
|
|
21
|
+
try {
|
|
22
|
+
const arg = process.argv[1];
|
|
23
|
+
if (!arg) return false;
|
|
24
|
+
return path.resolve(arg) === path.resolve(fileURLToPath(import.meta.url));
|
|
25
|
+
} catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (isMain()) {
|
|
31
|
+
const found = listMigrations();
|
|
32
|
+
if (found.length === 0) console.log("No generated migration files found.");
|
|
33
|
+
else {
|
|
34
|
+
console.log("Generated migrations:");
|
|
35
|
+
for (const file of found) console.log(` - ${file}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
package/template/src/core/env.ts
CHANGED
|
@@ -1,106 +1,108 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
*
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
electronApp?.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
1
|
+
// ./src/core/env.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* env.ts - runtime environment bootstrapping.
|
|
5
|
+
*
|
|
6
|
+
* Migrated from Deno to Node.js/Electron.
|
|
7
|
+
* Uses `dotenv` to load `.env` files from stable locations.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import { createRequire } from "node:module";
|
|
12
|
+
import { dirname, join, resolve } from "node:path";
|
|
13
|
+
import { config as loadDotenv } from "dotenv";
|
|
14
|
+
|
|
15
|
+
function getElectronApp(): any {
|
|
16
|
+
try {
|
|
17
|
+
const req = typeof require !== "undefined" ? require : createRequire(`${process.cwd()}/`);
|
|
18
|
+
const e = req("electron");
|
|
19
|
+
if (e && typeof e === "object" && typeof e.app?.getPath === "function") return e.app;
|
|
20
|
+
} catch {}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** True when running inside the Electron runtime. */
|
|
25
|
+
export function isDesktopRuntime(): boolean {
|
|
26
|
+
return !!getElectronApp();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** True only for a real packaged app. */
|
|
30
|
+
function bundledDesktopApp(): boolean {
|
|
31
|
+
const app = getElectronApp();
|
|
32
|
+
return !!app?.isPackaged;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function findProjectRoot(start: string): string | undefined {
|
|
36
|
+
let dir = resolve(start);
|
|
37
|
+
for (let i = 0; i < 8; i++) {
|
|
38
|
+
try {
|
|
39
|
+
if (fs.statSync(join(dir, "package.json")).isFile()) return dir;
|
|
40
|
+
} catch {}
|
|
41
|
+
const parent = dirname(dir);
|
|
42
|
+
if (parent === dir) break;
|
|
43
|
+
dir = parent;
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The app/project root in development, executable/resources dir when packaged. */
|
|
49
|
+
export function appBaseDir(): string {
|
|
50
|
+
const override = process.env.APP_BASE_DIR?.trim();
|
|
51
|
+
if (override) return resolve(override);
|
|
52
|
+
|
|
53
|
+
const electronApp = getElectronApp();
|
|
54
|
+
if (electronApp?.isPackaged) {
|
|
55
|
+
return dirname(electronApp.getPath("exe"));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Dev: prefer cwd (npm scripts run from project root), then locate package.json.
|
|
59
|
+
const cwd = resolve(process.cwd());
|
|
60
|
+
if (fs.existsSync(join(cwd, "package.json"))) return cwd;
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
if (electronApp) {
|
|
64
|
+
const found = findProjectRoot(electronApp.getAppPath());
|
|
65
|
+
if (found) return found;
|
|
66
|
+
}
|
|
67
|
+
} catch {}
|
|
68
|
+
|
|
69
|
+
return cwd;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const envState: { path: string | null; tried: boolean } = { path: null, tried: false };
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Load the first `.env` found next to the app (bundled) or at the project
|
|
76
|
+
* root (dev); never overrides variables already present in the environment.
|
|
77
|
+
* Returns the resolved path, or null when no `.env` exists. Idempotent.
|
|
78
|
+
*/
|
|
79
|
+
export function loadEnv(): string | null {
|
|
80
|
+
if (envState.tried) return envState.path;
|
|
81
|
+
envState.tried = true;
|
|
82
|
+
|
|
83
|
+
const electronApp = getElectronApp();
|
|
84
|
+
const base = appBaseDir();
|
|
85
|
+
const candidates = [
|
|
86
|
+
process.env.APP_BASE_DIR?.trim(),
|
|
87
|
+
electronApp?.isPackaged ? dirname(electronApp.getPath("exe")) : undefined,
|
|
88
|
+
base,
|
|
89
|
+
electronApp?.getAppPath ? electronApp.getAppPath() : undefined,
|
|
90
|
+
resolve(process.cwd())
|
|
91
|
+
].filter((dir): dir is string => Boolean(dir) && dir.length > 0);
|
|
92
|
+
|
|
93
|
+
for (const dir of [...new Set(candidates)]) {
|
|
94
|
+
const envPath = join(dir, ".env");
|
|
95
|
+
try {
|
|
96
|
+
if (!fs.statSync(envPath).isFile()) continue;
|
|
97
|
+
loadDotenv({ path: envPath });
|
|
98
|
+
envState.path = envPath;
|
|
99
|
+
return envPath;
|
|
100
|
+
} catch {}
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The runtime `.env` path that was loaded (null until loadEnv runs / none). */
|
|
106
|
+
export function envPath(): string | null {
|
|
107
|
+
return envState.tried ? envState.path : null;
|
|
108
|
+
}
|