@thirty-coder/electron-base-kit 0.0.0-stage → 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
@@ -1,3 +1,103 @@
1
- # Temporary Holding Version
1
+ # @thirty-coder/electron-base-kit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Electron 桌面应用的基础能力,业务项目以 npm 依赖引入。
4
+
5
+ ## 包含什么
6
+
7
+ 16 项运行时能力,开箱即用。
8
+
9
+ | 能力 | 说明 | 开关 |
10
+ | --- | --- | --- |
11
+ | 窗口管理 | 创建、多屏适配、记忆位置和尺寸 | 常开 |
12
+ | 单实例锁 | 二次启动唤起已有窗口,不开第二个 | 常开 |
13
+ | 安全基线 | contextIsolation、CSP、外链拦截、禁用 webview | 常开 |
14
+ | 日志 | 分级、落盘、按天轮转、一键导出 | 常开 |
15
+ | 崩溃捕获 | 主进程 + 渲染进程,本地留存 | 常开 |
16
+ | IPC 契约 | 类型安全的通道定义,不手写 channel 字符串 | 常开 |
17
+ | 配置存储 | 用户配置持久化,区分用户可改与只读 | 常开 |
18
+ | 自动更新 | Windows 静默更新,macOS 未签名时引导下载 | `updater` |
19
+ | 授权校验 | 非对称签名、离线验签、机器绑定 | `license` |
20
+ | 系统托盘 | 托盘图标、右键菜单、未读角标 | `tray` |
21
+ | 系统通知 | 原生通知 + 点击回调 | `notify` |
22
+ | 全局快捷键 | 注册/注销、冲突检测 | `shortcut` |
23
+ | 文件对话框 | 打开、保存、选目录 | `dialog` |
24
+ | 剪贴板 | 文本、图片读写 | `clipboard` |
25
+ | 开机自启 | 登录项注册 | `autolaunch` |
26
+ | deep link | 协议注册与参数解析 | `deeplink` |
27
+
28
+ 带开关的能力在 `app.config.json` 里控制,关掉就不会初始化。
29
+
30
+ ## 用法
31
+
32
+ 业务项目在 `electron/main.ts` 里调用:
33
+
34
+ ```ts
35
+ import { bootstrap } from '@thirty-coder/electron-base-kit'
36
+ import config from '../app.config.json'
37
+
38
+ bootstrap(config)
39
+ ```
40
+
41
+ 框架不读配置文件,config 由项目传入。
42
+
43
+ ## 配置
44
+
45
+ `app.config.json` 由业务项目维护,结构对应 `AppConfig` 类型:
46
+
47
+ ```json
48
+ {
49
+ "appId": "com.example.my-app",
50
+ "productName": "My App",
51
+ "protocol": "myapp",
52
+ "features": {
53
+ "updater": true,
54
+ "license": false,
55
+ "tray": false
56
+ },
57
+ "update": {
58
+ "provider": "generic",
59
+ "url": "https://dl.example.com/myapp"
60
+ },
61
+ "license": { "publicKey": "" },
62
+ "window": { "width": 1200, "height": 800, "minWidth": 900, "minHeight": 600 }
63
+ }
64
+ ```
65
+
66
+ `updater` 打开后要填 `update`,`license` 打开后要填 `publicKey`。
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
+
88
+ ## 升级
89
+
90
+ ```bash
91
+ pnpm up @thirty-coder/electron-base-kit
92
+ pnpm dev
93
+ ```
94
+
95
+ ## 开发
96
+
97
+ ```bash
98
+ pnpm build # 产出 dist
99
+ pnpm typecheck # 只做类型检查
100
+ cd playground && pnpm start # 在最小环境里验证
101
+ ```
102
+
103
+ 本包怎么改见 [MAINTAINING.md](./MAINTAINING.md)。
@@ -0,0 +1,66 @@
1
+ /** 能力开关。未声明的能力视为关闭。 */
2
+ interface FeatureFlags {
3
+ updater?: boolean;
4
+ license?: boolean;
5
+ tray?: boolean;
6
+ notify?: boolean;
7
+ shortcut?: boolean;
8
+ dialog?: boolean;
9
+ clipboard?: boolean;
10
+ autolaunch?: boolean;
11
+ deeplink?: boolean;
12
+ }
13
+ type UpdateProvider = 'generic' | 'github';
14
+ interface UpdateConfig {
15
+ provider: UpdateProvider;
16
+ /** generic 时必填,产物根地址,例如 https://dl.example.com/myapp */
17
+ url?: string;
18
+ /** github 时必填 */
19
+ owner?: string;
20
+ /** github 时必填 */
21
+ repo?: string;
22
+ }
23
+ interface LicenseConfig {
24
+ /** 验签公钥,签发私钥不在此处 */
25
+ publicKey: string;
26
+ }
27
+ interface WindowConfig {
28
+ width?: number;
29
+ height?: number;
30
+ minWidth?: number;
31
+ minHeight?: number;
32
+ }
33
+ /** 由业务项目传入,框架不读文件。 */
34
+ interface AppConfig {
35
+ appId: string;
36
+ productName: string;
37
+ /** deep link 协议名,例如 myapp */
38
+ protocol?: string;
39
+ features?: FeatureFlags;
40
+ update?: UpdateConfig;
41
+ license?: LicenseConfig;
42
+ window?: WindowConfig;
43
+ }
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
+
58
+ /**
59
+ * 初始化框架。
60
+ *
61
+ * 常开能力初始化失败直接抛;可选能力失败只记错误,继续启动。
62
+ */
63
+ declare function bootstrap(config: AppConfig): void;
64
+
65
+ export { bootstrap, settings };
66
+ export type { AppConfig, FeatureFlags, LicenseConfig, SettingsApi, UpdateConfig, UpdateProvider, WindowConfig };
package/dist/index.js ADDED
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ bootstrap: () => bootstrap,
24
+ settings: () => settings
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
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
+
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
+ }
191
+ function bootstrap(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
+ }
212
+ }
213
+ // Annotate the CommonJS export names for ESM import in node:
214
+ 0 && (module.exports = {
215
+ bootstrap,
216
+ settings
217
+ });
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@thirty-coder/electron-base-kit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Electron 桌面应用基础能力",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/sanshi26910/electron-base-kit.git"
9
+ },
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "require": "./dist/index.js",
16
+ "default": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsup",
24
+ "dev": "tsup --watch",
25
+ "typecheck": "tsc --noEmit",
26
+ "release": "node scripts/release.mjs"
27
+ },
28
+ "peerDependencies": {
29
+ "electron": ">=30"
30
+ },
31
+ "devDependencies": {
32
+ "electron": "^45.6.0",
33
+ "tsup": "^9.0.0",
34
+ "typescript": "^6.0.3"
35
+ }
36
+ }