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