@thirty-coder/electron-base-kit 0.1.0 → 0.1.1

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 CHANGED
@@ -65,6 +65,26 @@ bootstrap(config)
65
65
 
66
66
  `updater` 打开后要填 `update`,`license` 打开后要填 `publicKey`。
67
67
 
68
+ ## 配置存储
69
+
70
+ `app.config.json` 是只读的项目配置。用户数据(窗口尺寸、上次打开的目录)用 `settings`:
71
+
72
+ ```ts
73
+ import { settings } from '@thirty-coder/electron-base-kit'
74
+
75
+ settings.get('window.bounds', { width: 1200, height: 800 })
76
+ settings.set('window.bounds', { width: 1440, height: 900, x: 100, y: 80 })
77
+ settings.has('window.bounds')
78
+ settings.remove('window.bounds')
79
+ settings.all()
80
+ settings.onChange('window.bounds', (bounds) => { /* ... */ })
81
+ settings.filePath
82
+ ```
83
+
84
+ 键是点分字符串,平铺存在 `<userData>/settings.json`。读走内存,写防抖 100ms,退出前同步落盘。文件损坏会改名成 `settings.bad.json` 留档,按空数据启动。
85
+
86
+ `bootstrap` 之后才能用,提前访问会抛错。
87
+
68
88
  ## 升级
69
89
 
70
90
  ```bash
package/dist/index.d.ts CHANGED
@@ -42,12 +42,25 @@ interface AppConfig {
42
42
  window?: WindowConfig;
43
43
  }
44
44
 
45
+ type Listener = (value: unknown) => void;
46
+ interface SettingsApi {
47
+ get<T>(key: string, fallback: T): T;
48
+ set(key: string, value: unknown): void;
49
+ has(key: string): boolean;
50
+ remove(key: string): void;
51
+ all(): Record<string, unknown>;
52
+ onChange(key: string, listener: Listener): () => void;
53
+ flush(): Promise<void>;
54
+ readonly filePath: string;
55
+ }
56
+ declare const settings: SettingsApi;
57
+
45
58
  /**
46
59
  * 初始化框架。
47
60
  *
48
- * 骨架阶段只保存配置,各能力后续逐个接入。
61
+ * 常开能力初始化失败直接抛;可选能力失败只记错误,继续启动。
49
62
  */
50
63
  declare function bootstrap(config: AppConfig): void;
51
64
 
52
- export { bootstrap };
53
- export type { AppConfig, FeatureFlags, LicenseConfig, UpdateConfig, UpdateProvider, WindowConfig };
65
+ export { bootstrap, settings };
66
+ export type { AppConfig, FeatureFlags, LicenseConfig, SettingsApi, UpdateConfig, UpdateProvider, WindowConfig };
package/dist/index.js CHANGED
@@ -20,15 +20,198 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- bootstrap: () => bootstrap
23
+ bootstrap: () => bootstrap,
24
+ settings: () => settings
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
26
27
 
28
+ // src/settings.ts
29
+ var import_electron = require("electron");
30
+ var import_node_fs = require("fs");
31
+ var import_node_path = require("path");
32
+ var WRITE_DELAY = 100;
33
+ var store = null;
34
+ var filePath = "";
35
+ var dirty = false;
36
+ var timer = null;
37
+ var listeners = /* @__PURE__ */ new Map();
38
+ function assertInit() {
39
+ if (store === null) {
40
+ throw new Error("settings \u672A\u521D\u59CB\u5316\uFF1A\u8BF7\u5148\u5728 app.whenReady() \u4E2D\u8C03\u7528 bootstrap(config)");
41
+ }
42
+ return store;
43
+ }
44
+ function load() {
45
+ if (!(0, import_node_fs.existsSync)(filePath)) return {};
46
+ let raw;
47
+ try {
48
+ raw = (0, import_node_fs.readFileSync)(filePath, "utf8");
49
+ } catch (err) {
50
+ console.error("[electron-base-kit] \u8BFB\u53D6 settings \u5931\u8D25\uFF1A", err);
51
+ return {};
52
+ }
53
+ try {
54
+ const parsed = JSON.parse(raw);
55
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
56
+ throw new Error("\u6839\u8282\u70B9\u4E0D\u662F\u5BF9\u8C61");
57
+ }
58
+ return parsed;
59
+ } catch (err) {
60
+ console.error("[electron-base-kit] settings \u5185\u5BB9\u65E0\u6548\uFF0C\u5DF2\u6539\u540D\u7559\u6863\uFF1A", err);
61
+ try {
62
+ (0, import_node_fs.renameSync)(filePath, (0, import_node_path.join)((0, import_node_path.dirname)(filePath), "settings.bad.json"));
63
+ } catch (renameErr) {
64
+ console.error("[electron-base-kit] \u7559\u6863\u5931\u8D25\uFF1A", renameErr);
65
+ }
66
+ return {};
67
+ }
68
+ }
69
+ function writeNow() {
70
+ if (!dirty || store === null) return;
71
+ try {
72
+ const tmp = `${filePath}.tmp`;
73
+ (0, import_node_fs.writeFileSync)(tmp, `${JSON.stringify(store, null, 2)}
74
+ `);
75
+ (0, import_node_fs.renameSync)(tmp, filePath);
76
+ dirty = false;
77
+ } catch (err) {
78
+ console.error("[electron-base-kit] \u5199\u5165 settings \u5931\u8D25\uFF1A", err);
79
+ }
80
+ }
81
+ function schedule() {
82
+ dirty = true;
83
+ if (timer) return;
84
+ timer = setTimeout(() => {
85
+ timer = null;
86
+ writeNow();
87
+ }, WRITE_DELAY);
88
+ timer.unref?.();
89
+ }
90
+ function emit(key, value) {
91
+ const set = listeners.get(key);
92
+ if (!set) return;
93
+ for (const fn of [...set]) {
94
+ try {
95
+ fn(value);
96
+ } catch (err) {
97
+ console.error(`[electron-base-kit] settings.onChange(${key}) \u56DE\u8C03\u629B\u9519\uFF1A`, err);
98
+ }
99
+ }
100
+ }
101
+ function initSettings() {
102
+ if (store !== null) return;
103
+ filePath = (0, import_node_path.join)(import_electron.app.getPath("userData"), "settings.json");
104
+ (0, import_node_fs.mkdirSync)((0, import_node_path.dirname)(filePath), { recursive: true });
105
+ store = load();
106
+ import_electron.app.on("before-quit", () => {
107
+ if (timer) {
108
+ clearTimeout(timer);
109
+ timer = null;
110
+ }
111
+ writeNow();
112
+ });
113
+ }
114
+ var settings = {
115
+ get(key, fallback) {
116
+ const data = assertInit();
117
+ return key in data && data[key] !== void 0 ? data[key] : fallback;
118
+ },
119
+ set(key, value) {
120
+ const data = assertInit();
121
+ let serialized;
122
+ try {
123
+ serialized = JSON.stringify(value);
124
+ } catch {
125
+ throw new TypeError(`settings.set('${key}') \u7684\u503C\u65E0\u6CD5\u5E8F\u5217\u5316\u4E3A JSON`);
126
+ }
127
+ if (serialized === void 0) {
128
+ throw new TypeError(`settings.set('${key}') \u7684\u503C\u65E0\u6CD5\u5E8F\u5217\u5316\u4E3A JSON`);
129
+ }
130
+ const previous = key in data ? JSON.stringify(data[key]) : void 0;
131
+ if (previous === serialized) return;
132
+ data[key] = value;
133
+ schedule();
134
+ emit(key, value);
135
+ },
136
+ has(key) {
137
+ const data = assertInit();
138
+ return key in data && data[key] !== void 0;
139
+ },
140
+ remove(key) {
141
+ const data = assertInit();
142
+ if (!(key in data)) return;
143
+ delete data[key];
144
+ schedule();
145
+ emit(key, void 0);
146
+ },
147
+ all() {
148
+ return { ...assertInit() };
149
+ },
150
+ onChange(key, listener) {
151
+ assertInit();
152
+ let set = listeners.get(key);
153
+ if (!set) {
154
+ set = /* @__PURE__ */ new Set();
155
+ listeners.set(key, set);
156
+ }
157
+ set.add(listener);
158
+ return () => {
159
+ const current = listeners.get(key);
160
+ if (!current) return;
161
+ current.delete(listener);
162
+ if (current.size === 0) listeners.delete(key);
163
+ };
164
+ },
165
+ async flush() {
166
+ assertInit();
167
+ if (timer) {
168
+ clearTimeout(timer);
169
+ timer = null;
170
+ }
171
+ writeNow();
172
+ },
173
+ get filePath() {
174
+ assertInit();
175
+ return filePath;
176
+ }
177
+ };
178
+
27
179
  // src/bootstrap.ts
180
+ function assertConfig(config) {
181
+ if (!config || typeof config !== "object") {
182
+ throw new TypeError("bootstrap: \u7F3A\u5C11 config");
183
+ }
184
+ if (!config.appId) {
185
+ throw new Error("bootstrap: config.appId \u4E0D\u80FD\u4E3A\u7A7A");
186
+ }
187
+ if (!config.productName) {
188
+ throw new Error("bootstrap: config.productName \u4E0D\u80FD\u4E3A\u7A7A");
189
+ }
190
+ }
28
191
  function bootstrap(config) {
29
- void config;
192
+ assertConfig(config);
193
+ initSettings();
194
+ if (config.features?.updater) {
195
+ }
196
+ if (config.features?.license) {
197
+ }
198
+ if (config.features?.tray) {
199
+ }
200
+ if (config.features?.notify) {
201
+ }
202
+ if (config.features?.shortcut) {
203
+ }
204
+ if (config.features?.dialog) {
205
+ }
206
+ if (config.features?.clipboard) {
207
+ }
208
+ if (config.features?.autolaunch) {
209
+ }
210
+ if (config.features?.deeplink) {
211
+ }
30
212
  }
31
213
  // Annotate the CommonJS export names for ESM import in node:
32
214
  0 && (module.exports = {
33
- bootstrap
215
+ bootstrap,
216
+ settings
34
217
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thirty-coder/electron-base-kit",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Electron 桌面应用基础能力",
5
5
  "license": "MIT",
6
6
  "repository": {