nexfep 0.2.0 → 0.3.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026 zhuxiaojt
3
+ Copyright (c) 2026 Nexfteam
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README-CN.md CHANGED
@@ -22,6 +22,10 @@ Nexfep 是一个基于 [@webviewjs/webview](https://github.com/webviewjs/webview
22
22
  - **IPC 通信** — 支持主进程与 WebView 之间的双向消息通信,通过注入的函数进行调用
23
23
  - **窗口控制** — 提供最大化、最小化、关闭、标题设置、开发者工具等完整窗口操作 API
24
24
  - **拖拽区域** — 内置 HTML 属性支持,方便定义窗口拖拽区域(`nexfep-area-drag` 等)
25
+ - **系统托盘** — 创建和管理系统托盘图标及右键菜单
26
+ - **桌面通知** — 通过 `app.utils.notify()` 发送桌面通知
27
+ - **日志系统** — 内置 Logger,支持文件日志和彩色控制台输出,自动拦截页面 console 消息
28
+ - **CLI 构建工具** — 通过 `nexfep build` 将应用打包为独立可执行文件
25
29
  - **TypeScript 支持** — 完整的类型定义,开发体验优秀
26
30
 
27
31
  ## 安装
@@ -46,26 +50,64 @@ await window.loadHTML('<h1 nexfep-area-drag>Hello Nexfep!</h1>');
46
50
 
47
51
  ### Application
48
52
 
49
- `Application` 是框架的主入口,负责管理应用生命周期,提供窗口和系统托盘的访问。
53
+ `Application` 是框架的主入口,负责管理应用生命周期,提供窗口、系统托盘和日志的访问。
50
54
 
51
55
  ```typescript
52
56
  import { Application } from 'nexfep';
53
57
 
54
58
  const app = new Application();
55
- // 或指定自定义 WebView2 用户数据目录(仅 Windows)
56
- const app = new Application('C:\\custom\\webview2-data');
59
+ // 或指定自定义 WebView2 用户数据目录(仅 Windows 生效)
60
+ const app = new Application({ WindowsWebview2UserDataFolder: 'C:\\custom\\webview2-data' });
61
+ // 或指定日志文件路径
62
+ const app = new Application({ LogFilePath: './app.log' });
57
63
  ```
58
64
 
65
+ **构造函数参数**
66
+
67
+ | 选项 | 类型 | 默认值 | 说明 |
68
+ |------|------|--------|------|
69
+ | `WindowsWebview2UserDataFolder` | `string`(可选) | `%LOCALAPPDATA%\NexfepDevelopment.webview2-data` | WebView2 用户数据目录(仅 Windows) |
70
+ | `LogFilePath` | `string`(可选) | 无 | 日志文件输出路径 |
71
+
59
72
  **属性**
60
73
 
61
74
  - `windows` — `WindowPool` 实例,用于管理浏览器窗口
62
75
  - `utils` — 工具方法(如桌面通知)
76
+ - `logger` — `Logger` 实例,用于日志记录
63
77
 
64
78
  **方法**
65
79
 
66
80
  - `createTray(options)` — 创建系统托盘图标和右键菜单
67
81
  - `exit()` — 退出应用
68
82
 
83
+ ### Logger
84
+
85
+ Logger 支持文件输出和彩色控制台输出,可通过 `app.logger` 访问。
86
+
87
+ ```typescript
88
+ app.logger.log('Hello World');
89
+ app.logger.error('发生错误');
90
+ app.logger.warn('警告信息');
91
+ app.logger.info('提示信息');
92
+ app.logger.debug('调试信息');
93
+ ```
94
+
95
+ **方法**
96
+
97
+ | 方法 | 说明 |
98
+ |------|------|
99
+ | `log(message)` | 记录日志 |
100
+ | `error(message)` | 记录错误日志(红色) |
101
+ | `warn(message)` | 记录警告日志(黄色) |
102
+ | `info(message)` | 记录提示日志(蓝色) |
103
+ | `debug(message)` | 记录调试日志(灰色) |
104
+
105
+ 每个方法接受字符串或字符串数组作为参数。
106
+
107
+ **页面 Console 拦截**
108
+
109
+ 页面中的 `console.log`、`console.error`、`console.info`、`console.warn`、`console.debug` 调用会被自动拦截并转发到主进程 Logger,输出中包含来源窗口的 ID。
110
+
69
111
  ### 托盘图标
70
112
 
71
113
  通过 `app.createTray()` 创建和管理系统托盘图标及右键菜单。
@@ -141,11 +183,6 @@ const notification = app.utils.notify('标题', '通知内容');
141
183
  const pool = app.windows;
142
184
  ```
143
185
 
144
- **构造函数参数**
145
-
146
- - `app` — `Application` 实例
147
- - `WindowsWebview2UserDataFolder`(可选)— WebView2 用户数据目录,默认为 `%LOCALAPPDATA%\NexfepDevelopment.webview2-data`
148
-
149
186
  ### 窗口创建
150
187
 
151
188
  ```typescript
@@ -166,6 +203,7 @@ window.maximize();
166
203
  window.minimize();
167
204
  window.close();
168
205
  window.setTitle('新标题');
206
+ window.setSize(800, 600);
169
207
  window.openDevTools();
170
208
  ```
171
209
 
@@ -271,9 +309,9 @@ const value = await window.getGlobal('hello');
271
309
 
272
310
  - `name` — 全局变量名称
273
311
 
274
- #### 全局变量Map
312
+ #### 全局变量 Map
275
313
 
276
- 在主进程中通过 `WindowPool.global` 获取一个包含所有全局变量的 `Map<string, any>` 对象,可对其进行设置、获取等操作:
314
+ 在主进程中通过 `pool.global` 获取一个包含所有全局变量的 `Map<string, any>` 对象,可对其进行设置、获取等操作:
277
315
 
278
316
  ```typescript
279
317
  const globals = pool.global;
@@ -424,40 +462,94 @@ if (window.isNexfepLoadDone) {
424
462
  }
425
463
  ```
426
464
 
465
+ ## CLI
466
+
467
+ Nexfep 提供命令行工具,用于将应用打包为独立可执行文件。
468
+
469
+ ### 用法
470
+
471
+ ```bash
472
+ npx nexfep build [options]
473
+ ```
474
+
475
+ ### 选项
476
+
477
+ | 选项 | 说明 |
478
+ |------|------|
479
+ | `-n, --name <name>` | 应用名称(默认:来自 package.json) |
480
+ | `-e, --entry <file>` | 入口文件路径(默认:来自 package.json main) |
481
+ | `-o, --output <dir>` | 输出目录(默认:dist) |
482
+ | `-i, --ignore <pattern>` | 要忽略的文件或目录(可多次使用) |
483
+ | `-c, --console` | 在 Windows 上显示控制台窗口(默认:false) |
484
+ | `-r, --reinstall` | 构建前仅重新安装生产依赖 |
485
+ | `-s, --skip-clean` | 跳过清理旧的构建文件 |
486
+ | `-u, --upx <level>` | 使用 UPX 压缩可执行文件,级别 0-9(默认:0) |
487
+
488
+ ### 示例
489
+
490
+ ```bash
491
+ # 使用 package.json 默认配置构建
492
+ nexfep build
493
+
494
+ # 设置自定义应用名称和入口文件
495
+ nexfep build -n my-app -e ./src/index.js
496
+
497
+ # 设置自定义输出目录
498
+ nexfep build -o ./build
499
+
500
+ # 忽略多个模式
501
+ nexfep build -i node_modules -i test -i temp
502
+
503
+ # 使用 UPX 压缩构建
504
+ nexfep build -u 7
505
+ ```
506
+
507
+ 该命令使用 [nexfpack](https://github.com/nexfteam/Nexfpack) 将应用打包为独立可执行文件。
508
+
427
509
  ## API
428
510
 
511
+ ### Application
512
+
513
+ | 方法/属性 | 参数 | 返回值 | 说明 |
514
+ |-----------|------|--------|------|
515
+ | `constructor(options?)` | `{ WindowsWebview2UserDataFolder?, LogFilePath? }` | Application | 创建应用实例 |
516
+ | `windows` | / | WindowPool | 窗口池实例 |
517
+ | `utils` | / | \_\_Utils | 工具方法(通知) |
518
+ | `logger` | / | Logger | 日志实例 |
519
+ | `createTray(options)` | 见 Tray 章节 | Tray | 创建系统托盘图标 |
520
+ | `exit()` | 无 | void | 退出应用 |
521
+
429
522
  ### WindowPool
430
523
 
431
- | 方法/属性 | 参数 | 返回值 | 说明 |
432
- | ------------------------------------- | ----------------------------------------------------------- | ---------------- | -------------------------- |
433
- | `constructor(userDataFolder?)` | `userDataFolder`: string(可选) | WindowPool | 创建窗口池,可选指定 WebView2 用户数据目录 |
434
- | `createWindow(isShow?, isDecorated?)` | `isShow`: boolean(默认 true), `isDecorated`: boolean(默认 true) | Promise\<Window> | 创建并获取一个窗口 |
435
- | `handle(event, callback)` | `event`: string, `callback`: (data: string) => void | 无 | 监听指定事件,当收到事件时触发回调函数 |
436
- | `unhandle(event, callback)` | `event`: string, `callback`: (data: string) => void | 无 | 取消监听指定事件回调中的指定函数 |
437
- | `global` | / | Map\<string, any> | 全局变量Map,类型为 `Map<string, any>` |
438
- | `closeWindow(window)` | `window`: Window | Promise\<void> | 关闭指定窗口并回收至池中 |
439
- | `onCustomMessage` | `(window: Window, data: string) => void` | 无 | 自定义消息回调函数,当收到页面发来的自定义消息时触发 |
524
+ | 方法/属性 | 参数 | 返回值 | 说明 |
525
+ |-----------|------|--------|------|
526
+ | `createWindow(isShow?, isDecorated?)` | `isShow`: boolean(默认 true), `isDecorated`: boolean(默认 true) | Promise\<Window> | 创建并获取一个窗口 |
527
+ | `handle(event, callback)` | `event`: string, `callback`: (data: any) => any | 无 | 监听指定事件 |
528
+ | `unhandle(event, callback)` | `event`: string, `callback`: (data: any) => any | 无 | 取消监听指定事件 |
529
+ | `global` | / | Map\<string, any> | 全局变量 Map |
530
+ | `closeWindow(window)` | `window`: Window | Promise\<void> | 关闭指定窗口并回收至池中 |
531
+ | `onCustomMessage` | `(window: Window, data: string) => void` | 无 | 自定义消息回调函数 |
440
532
 
441
533
  ### Window
442
534
 
443
- | 方法/属性 | 参数 | 返回值 | 说明 |
444
- | --------------------------- | --------------------------------- | -------------- | ------------- |
445
- | `loadURL(url)` | `url`: string — 要加载的网页地址 | Promise\<void> | 加载指定 URL |
446
- | `loadHTML(html)` | `html`: string — HTML 字符串 | Promise\<void> | 加载指定 HTML 内容 |
447
- | `show()` | 无 | void | 显示窗口 |
448
- | `hide()` | 无 | void | 隐藏窗口 |
449
- | `maximize()` | 无 | void | 最大化窗口 |
450
- | `unMaximize()` | 无 | void | 还原窗口(取消最大化) |
451
- | `minimize()` | 无 | void | 最小化窗口 |
452
- | `unMinimize()` | 无 | void | 还原窗口(取消最小化) |
453
- | `close()` | 无 | void | 关闭窗口并回收至池中 |
454
- | `setTitle(title)` | `title`: string — 窗口标题 | void | 设置窗口标题 |
455
- | `setDecorated(isDecorated)` | `isDecorated`: boolean — 是否使用系统装饰 | void | 设置窗口是否带边框和标题栏 |
456
- | `resizable(resizable)` | `resizable`: boolean — 是否可调整大小 | void | 设置窗口是否可调整大小 |
457
- | `setSize(width, height)` | `width`: number, `height`: number | void | 设置窗口尺寸(像素) |
458
- | `openDevTools()` | 无 | void | 打开开发者工具 |
459
- | `closeDevTools()` | 无 | void | 关闭开发者工具 |
460
- | `id` | 无 | number | 窗口唯一标识,自增编号 |
535
+ | 方法/属性 | 参数 | 返回值 | 说明 |
536
+ |-----------|------|--------|------|
537
+ | `loadURL(url)` | `url`: string — 要加载的网页地址 | Promise\<void> | 加载指定 URL |
538
+ | `loadHTML(html)` | `html`: string — HTML 字符串 | Promise\<void> | 加载指定 HTML 内容 |
539
+ | `show()` | 无 | void | 显示窗口 |
540
+ | `hide()` | 无 | void | 隐藏窗口 |
541
+ | `maximize()` | 无 | void | 最大化窗口 |
542
+ | `unMaximize()` | 无 | void | 还原窗口(取消最大化) |
543
+ | `minimize()` | 无 | void | 最小化窗口 |
544
+ | `unMinimize()` | 无 | void | 还原窗口(取消最小化) |
545
+ | `close()` | 无 | void | 关闭窗口并回收至池中 |
546
+ | `setTitle(title)` | `title`: string | void | 设置窗口标题 |
547
+ | `setDecorated(isDecorated)` | `isDecorated`: boolean | void | 设置窗口是否带边框和标题栏 |
548
+ | `resizable(resizable)` | `resizable`: boolean | void | 设置窗口是否可调整大小 |
549
+ | `setSize(width, height)` | `width`: number, `height`: number | void | 设置窗口尺寸(像素) |
550
+ | `openDevTools()` | 无 | void | 打开开发者工具 |
551
+ | `closeDevTools()` | 无 | void | 关闭开发者工具 |
552
+ | `id` | 无 | number | 窗口唯一标识,自增编号 |
461
553
 
462
554
  ## 开发
463
555
 
package/README.md CHANGED
@@ -22,6 +22,10 @@ The framework uses a window pool management mechanism, supporting multi-window a
22
22
  - **IPC Communication** — Supports bidirectional message communication between the main process and WebView, via injected functions
23
23
  - **Window Control** — Provides complete window operation APIs including maximize, minimize, close, title setting, and developer tools
24
24
  - **Drag Regions** — Built-in HTML attribute support for defining window drag regions (`nexfep-area-drag`, etc.)
25
+ - **System Tray** — Create and manage system tray icons with context menus
26
+ - **Desktop Notifications** — Send desktop notifications via `app.utils.notify()`
27
+ - **Logging System** — Built-in logger with file output and colored console support, with automatic interception of page console messages
28
+ - **CLI Build Tool** — Package your application into a standalone executable via `nexfep build`
25
29
  - **TypeScript Support** — Complete type definitions for excellent development experience
26
30
 
27
31
  ## Installation
@@ -46,26 +50,64 @@ await window.loadHTML('<h1 nexfep-area-drag>Hello Nexfep!</h1>');
46
50
 
47
51
  ### Application
48
52
 
49
- `Application` is the main entry point of the framework, responsible for managing the application lifecycle and providing access to windows and system tray.
53
+ `Application` is the main entry point of the framework, responsible for managing the application lifecycle and providing access to windows, system tray, and logger.
50
54
 
51
55
  ```typescript
52
56
  import { Application } from 'nexfep';
53
57
 
54
58
  const app = new Application();
55
59
  // or with custom WebView2 user data directory (Windows only)
56
- const app = new Application('C:\\custom\\webview2-data');
60
+ const app = new Application({ WindowsWebview2UserDataFolder: 'C:\\custom\\webview2-data' });
61
+ // or with log file path
62
+ const app = new Application({ LogFilePath: './app.log' });
57
63
  ```
58
64
 
65
+ **Constructor Options**
66
+
67
+ | Option | Type | Default | Description |
68
+ |--------|------|---------|-------------|
69
+ | `WindowsWebview2UserDataFolder` | `string` (optional) | `%LOCALAPPDATA%\NexfepDevelopment.webview2-data` | WebView2 user data directory (Windows only) |
70
+ | `LogFilePath` | `string` (optional) | none | File path for log output |
71
+
59
72
  **Properties**
60
73
 
61
74
  - `windows` — The `WindowPool` instance for managing browser windows
62
75
  - `utils` — Utility methods (e.g., desktop notifications)
76
+ - `logger` — The `Logger` instance for logging
63
77
 
64
78
  **Methods**
65
79
 
66
80
  - `createTray(options)` — Create a system tray icon with context menu
67
81
  - `exit()` — Exit the application
68
82
 
83
+ ### Logger
84
+
85
+ The logger supports both file output and colored console output. It can be accessed via `app.logger`.
86
+
87
+ ```typescript
88
+ app.logger.log('Hello World');
89
+ app.logger.error('An error occurred');
90
+ app.logger.warn('Warning message');
91
+ app.logger.info('Info message');
92
+ app.logger.debug('Debug message');
93
+ ```
94
+
95
+ **Methods**
96
+
97
+ | Method | Description |
98
+ |--------|-------------|
99
+ | `log(message)` | Log a message |
100
+ | `error(message)` | Log an error message (red) |
101
+ | `warn(message)` | Log a warning message (yellow) |
102
+ | `info(message)` | Log an info message (blue) |
103
+ | `debug(message)` | Log a debug message (gray) |
104
+
105
+ Each method accepts either a string or an array of strings.
106
+
107
+ **Page Console Interception**
108
+
109
+ Console calls (`console.log`, `console.error`, `console.info`, `console.warn`, `console.debug`) in the page are automatically intercepted and forwarded to the main process logger, with the source window ID included in the output.
110
+
69
111
  ### Tray
70
112
 
71
113
  Create and manage system tray icons with context menus via `app.createTray()`.
@@ -141,11 +183,6 @@ const notification = app.utils.notify('Title', 'Notification body');
141
183
  const pool = app.windows;
142
184
  ```
143
185
 
144
- **Constructor Parameters**
145
-
146
- - `app` — The `Application` instance
147
- - `WindowsWebview2UserDataFolder` (optional) — WebView2 user data directory, defaults to `%LOCALAPPDATA%\NexfepDevelopment.webview2-data`
148
-
149
186
  ### Window Creation
150
187
 
151
188
  ```typescript
@@ -166,6 +203,7 @@ window.maximize();
166
203
  window.minimize();
167
204
  window.close();
168
205
  window.setTitle('New Title');
206
+ window.setSize(800, 600);
169
207
  window.openDevTools();
170
208
  ```
171
209
 
@@ -273,7 +311,7 @@ const value = await window.getGlobal('hello');
273
311
 
274
312
  #### Global Variable Map
275
313
 
276
- Get a `Map<string, any>` containing all global variables via `WindowPool.global` in the main process, which supports operations like set and get:
314
+ Get a `Map<string, any>` containing all global variables via `pool.global` in the main process, which supports operations like set and get:
277
315
 
278
316
  ```typescript
279
317
  const globals = pool.global;
@@ -424,40 +462,94 @@ if (window.isNexfepLoadDone) {
424
462
  }
425
463
  ```
426
464
 
465
+ ## CLI
466
+
467
+ Nexfep provides a command-line tool for building applications into standalone executables.
468
+
469
+ ### Usage
470
+
471
+ ```bash
472
+ npx nexfep build [options]
473
+ ```
474
+
475
+ ### Options
476
+
477
+ | Option | Description |
478
+ |--------|-------------|
479
+ | `-n, --name <name>` | Application name (default: from package.json) |
480
+ | `-e, --entry <file>` | Entry file path (default: from package.json main) |
481
+ | `-o, --output <dir>` | Output directory (default: dist) |
482
+ | `-i, --ignore <pattern>` | Files or directories to ignore (can be used multiple times) |
483
+ | `-c, --console` | Show console window on Windows (default: false) |
484
+ | `-r, --reinstall` | Reinstall production dependencies only before building |
485
+ | `-s, --skip-clean` | Skip cleaning old build files before building |
486
+ | `-u, --upx <level>` | Use UPX to compress the executable, level 0-9 (default: 0) |
487
+
488
+ ### Examples
489
+
490
+ ```bash
491
+ # Build using defaults from package.json
492
+ nexfep build
493
+
494
+ # Set custom application name and entry file
495
+ nexfep build -n my-app -e ./src/index.js
496
+
497
+ # Set custom output directory
498
+ nexfep build -o ./build
499
+
500
+ # Ignore multiple patterns
501
+ nexfep build -i node_modules -i test -i temp
502
+
503
+ # Build with UPX compression
504
+ nexfep build -u 7
505
+ ```
506
+
507
+ This command uses [nexfpack](https://github.com/nexfteam/Nexfpack) to package your application into a standalone executable.
508
+
427
509
  ## API
428
510
 
511
+ ### Application
512
+
513
+ | Method/Property | Parameters | Return Value | Description |
514
+ |----------------|-----------|--------------|-------------|
515
+ | `constructor(options?)` | `{ WindowsWebview2UserDataFolder?, LogFilePath? }` | Application | Creates the application instance |
516
+ | `windows` | / | WindowPool | The window pool instance |
517
+ | `utils` | / | \_\_Utils | Utility methods (notifications) |
518
+ | `logger` | / | Logger | The logger instance |
519
+ | `createTray(options)` | see Tray section | Tray | Creates a system tray icon |
520
+ | `exit()` | None | void | Exits the application |
521
+
429
522
  ### WindowPool
430
523
 
431
- | Method/Property | Parameters | Return Value | Description |
432
- | ------------------------------------- | -------------------------------------------------------------------------- | ---------------- | ------------------------------------------------------------------------ |
433
- | `constructor(userDataFolder?)` | `userDataFolder`: string (optional) | WindowPool | Creates a window pool, optionally specifying the WebView2 user data directory |
434
- | `createWindow(isShow?, isDecorated?)` | `isShow`: boolean (default true), `isDecorated`: boolean (default true) | Promise\<Window> | Creates and returns a window |
435
- | `handle(event, callback)` | `event`: string, `callback`: (data: string) => void | None | Listens for the specified event, triggers callback when event is received |
436
- | `unhandle(event, callback)` | `event`: string, `callback`: (data: string) => void | None | Removes the specified callback from the event listener |
437
- | `global` | / | Map\<string, any> | A global variable map of type: `Map<string, any>` |
438
- | `closeWindow(window)` | `window`: Window | Promise\<void> | Closes the specified window and returns it to the pool |
439
- | `onCustomMessage` | `(window: Window, data: string) => void` | None | Custom message callback, triggered when receiving custom messages from pages |
524
+ | Method/Property | Parameters | Return Value | Description |
525
+ |----------------|-----------|--------------|-------------|
526
+ | `createWindow(isShow?, isDecorated?)` | `isShow`: boolean (default true), `isDecorated`: boolean (default true) | Promise\<Window> | Creates and returns a window |
527
+ | `handle(event, callback)` | `event`: string, `callback`: (data: any) => any | None | Listens for the specified event |
528
+ | `unhandle(event, callback)` | `event`: string, `callback`: (data: any) => any | None | Removes the specified event listener |
529
+ | `global` | / | Map\<string, any> | A global variable map |
530
+ | `closeWindow(window)` | `window`: Window | Promise\<void> | Closes the specified window and returns it to the pool |
531
+ | `onCustomMessage` | `(window: Window, data: string) => void` | None | Custom message callback |
440
532
 
441
533
  ### Window
442
534
 
443
- | Method/Property | Parameters | Return Value | Description |
444
- | ------------------------------------- | ------------------------------------------- | ---------------- | ---------------------------------------- |
445
- | `loadURL(url)` | `url`: string — URL to load | Promise\<void> | Loads the specified URL |
446
- | `loadHTML(html)` | `html`: string — HTML string | Promise\<void> | Loads the specified HTML content |
447
- | `show()` | None | void | Shows the window |
448
- | `hide()` | None | void | Hides the window |
449
- | `maximize()` | None | void | Maximizes the window |
450
- | `unMaximize()` | None | void | Restores the window (cancels maximize) |
451
- | `minimize()` | None | void | Minimizes the window |
452
- | `unMinimize()` | None | void | Restores the window (cancels minimize) |
453
- | `close()` | None | void | Closes the window and returns to pool |
454
- | `setTitle(title)` | `title`: string — Window title | void | Sets the window title |
455
- | `setDecorated(isDecorated)` | `isDecorated`: boolean — Use system decorations | void | Sets whether the window has borders and title bar |
456
- | `resizable(resizable)` | `resizable`: boolean — Resizable | void | Sets whether the window is resizable |
457
- | `setSize(width, height)` | `width`: number, `height`: number | void | Sets the window size in pixels |
458
- | `openDevTools()` | None | void | Opens developer tools |
459
- | `closeDevTools()` | None | void | Closes developer tools |
460
- | `id` | None | number | Unique window identifier, auto-incrementing |
535
+ | Method/Property | Parameters | Return Value | Description |
536
+ |----------------|-----------|--------------|-------------|
537
+ | `loadURL(url)` | `url`: string — URL to load | Promise\<void> | Loads the specified URL |
538
+ | `loadHTML(html)` | `html`: string — HTML string | Promise\<void> | Loads the specified HTML content |
539
+ | `show()` | None | void | Shows the window |
540
+ | `hide()` | None | void | Hides the window |
541
+ | `maximize()` | None | void | Maximizes the window |
542
+ | `unMaximize()` | None | void | Restores the window (cancels maximize) |
543
+ | `minimize()` | None | void | Minimizes the window |
544
+ | `unMinimize()` | None | void | Restores the window (cancels minimize) |
545
+ | `close()` | None | void | Closes the window and returns to pool |
546
+ | `setTitle(title)` | `title`: string | void | Sets the window title |
547
+ | `setDecorated(isDecorated)` | `isDecorated`: boolean | void | Sets whether the window has borders and title bar |
548
+ | `resizable(resizable)` | `resizable`: boolean | void | Sets whether the window is resizable |
549
+ | `setSize(width, height)` | `width`: number, `height`: number | void | Sets the window size in pixels |
550
+ | `openDevTools()` | None | void | Opens developer tools |
551
+ | `closeDevTools()` | None | void | Closes developer tools |
552
+ | `id` | None | number | Unique window identifier, auto-incrementing |
461
553
 
462
554
  ## Development
463
555
 
@@ -468,4 +560,4 @@ pnpm run compile
468
560
 
469
561
  ## License
470
562
 
471
- MIT License
563
+ MIT License
package/cli/index.mjs ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env node
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import build from './build.mjs';
5
+ import help from './help.mjs';
6
+ const args = process.argv.slice(2);
7
+ if(args.length === 0) {
8
+ help();
9
+ process.exit(1);
10
+ }
11
+ if(args[0] === 'help') {
12
+ if(args.length > 1) {
13
+ help(args[1]);
14
+ } else {
15
+ help();
16
+ }
17
+ }
18
+ if(args[0] === 'build') {
19
+ build(args.slice(1));
20
+ }
package/package.json CHANGED
@@ -6,6 +6,9 @@
6
6
  "bugs": {
7
7
  "url": "https://github.com/nexfteam/Nexfep/issues"
8
8
  },
9
+ "bin": {
10
+ "nexfep": "cli/index.mjs"
11
+ },
9
12
  "keywords": [
10
13
  "desktop",
11
14
  "application",
@@ -14,7 +17,7 @@
14
17
  "webview",
15
18
  "typescript"
16
19
  ],
17
- "version": "0.2.0",
20
+ "version": "0.3.1",
18
21
  "type": "module",
19
22
  "main": "./index.js",
20
23
  "scripts": {
@@ -1,6 +1,7 @@
1
1
  import { Application as WebviewApplication, TrayIconImage, Notification } from '@webviewjs/webview';
2
2
  import { WindowPool } from './WindowManager.js';
3
3
  import { Tray } from './Tray.js';
4
+ import { Logger } from './Logger.js';
4
5
  declare class __Utils {
5
6
  app: WebviewApplication;
6
7
  constructor(app: WebviewApplication);
@@ -9,8 +10,12 @@ declare class __Utils {
9
10
  declare class Application {
10
11
  app: WebviewApplication;
11
12
  windows: WindowPool;
13
+ logger: Logger;
12
14
  utils: __Utils;
13
- constructor(WindowsWebview2UserDataFolder?: string);
15
+ constructor(options: {
16
+ WindowsWebview2UserDataFolder?: string;
17
+ LogFilePath?: string;
18
+ });
14
19
  createTray(options: {
15
20
  id: string;
16
21
  tooltip: string;
@@ -1,8 +1,7 @@
1
1
  import { Application as WebviewApplication, Notification } from '@webviewjs/webview';
2
2
  import { WindowPool } from './WindowManager.js';
3
3
  import { Tray } from './Tray.js';
4
- import path from 'path';
5
- import os from 'os';
4
+ import { Logger } from './Logger.js';
6
5
  class __Utils {
7
6
  app;
8
7
  constructor(app) {
@@ -16,11 +15,14 @@ class __Utils {
16
15
  class Application {
17
16
  app;
18
17
  windows;
18
+ logger;
19
19
  utils;
20
- constructor(WindowsWebview2UserDataFolder = path.join(process.env.LOCALAPPDATA || os.homedir(), 'NexfepDevelopment.webview2-data')) {
20
+ constructor(options) {
21
21
  this.app = new WebviewApplication();
22
22
  this.utils = new __Utils(this.app);
23
- this.windows = new WindowPool(this.app, WindowsWebview2UserDataFolder);
23
+ this.logger = new Logger(options.LogFilePath);
24
+ const poolOptions = { WindowsWebview2UserDataFolder: options.WindowsWebview2UserDataFolder, logger: this.logger };
25
+ this.windows = new WindowPool(this.app, poolOptions);
24
26
  this.app.whenReady();
25
27
  }
26
28
  createTray(options) {
@@ -1,4 +1,5 @@
1
1
  import { Application, BrowserWindow, Webview } from "@webviewjs/webview";
2
+ import { Logger } from "./Logger.js";
2
3
  declare class Window {
3
4
  window: BrowserWindow;
4
5
  webview: Webview;
@@ -27,13 +28,17 @@ declare class Window {
27
28
  declare class WindowPool {
28
29
  onCustomMessage: (window: Window, data: string) => void;
29
30
  app: Application;
31
+ logger: Logger;
30
32
  private windows;
31
33
  private handlers;
32
34
  private injectCount;
33
35
  private windowCount;
34
36
  private freeWindowCount;
35
37
  global: Map<string, any>;
36
- constructor(app: Application, WindowsWebview2UserDataFolder?: string);
38
+ constructor(app: Application, options: {
39
+ WindowsWebview2UserDataFolder?: string;
40
+ logger?: Logger;
41
+ });
37
42
  __injectCode(window: Window, code: string): Promise<void>;
38
43
  __injectControlFunctions(windowObj: Window): Promise<void>;
39
44
  __createNewWindowObj(): Promise<Window>;
@@ -1,3 +1,4 @@
1
+ import { Logger } from "./Logger.js";
1
2
  import os from 'os';
2
3
  import path from 'path';
3
4
  import fs from 'fs';
@@ -72,16 +73,17 @@ class Window {
72
73
  class WindowPool {
73
74
  onCustomMessage;
74
75
  app;
76
+ logger;
75
77
  windows;
76
78
  handlers;
77
79
  injectCount;
78
80
  windowCount;
79
81
  freeWindowCount;
80
82
  global;
81
- constructor(app, WindowsWebview2UserDataFolder = path.join(process.env.LOCALAPPDATA || os.homedir(), 'NexfepDevelopment.webview2-data')) {
83
+ constructor(app, options) {
82
84
  if (os.platform() === 'win32') {
83
85
  // 设置 WebView2 用户数据目录(仅 Windows 需要此配置)
84
- const userDataDir = WindowsWebview2UserDataFolder;
86
+ const userDataDir = options.WindowsWebview2UserDataFolder || path.join(process.env.LOCALAPPDATA || os.homedir(), 'NexfepDevelopment.webview2-data');
85
87
  if (!fs.existsSync(userDataDir)) {
86
88
  fs.mkdirSync(userDataDir, { recursive: true });
87
89
  }
@@ -97,6 +99,7 @@ class WindowPool {
97
99
  this.windowCount = 0;
98
100
  this.freeWindowCount = 0;
99
101
  this.global = new Map();
102
+ this.logger = options.logger || new Logger();
100
103
  }
101
104
  async __injectCode(window, code) {
102
105
  await window.webview.evaluateScript(code);
@@ -235,6 +238,37 @@ class WindowPool {
235
238
  const MessageBody = { type: 'NexfepTell', to: to, message: message, data: data }
236
239
  window.ipc.postMessage(JSON.stringify(MessageBody));
237
240
  }
241
+
242
+ window.__originConsoleLog = console.log;
243
+ window.__originConsoleError = console.error;
244
+ window.__originConsoleInfo = console.info;
245
+ window.__originConsoleWarn = console.warn;
246
+ window.__originConsoleDebug = console.debug;
247
+ console.log = (...args) => {
248
+ const MessageBody = { type: 'NexfepConsoleLog', message: args }
249
+ window.ipc.postMessage(JSON.stringify(MessageBody));
250
+ window.__originConsoleLog(...args);
251
+ }
252
+ console.error = (...args) => {
253
+ const MessageBody = { type: 'NexfepConsoleError', message: args }
254
+ window.ipc.postMessage(JSON.stringify(MessageBody));
255
+ window.__originConsoleError(...args);
256
+ }
257
+ console.info = (...args) => {
258
+ const MessageBody = { type: 'NexfepConsoleInfo', message: args }
259
+ window.ipc.postMessage(JSON.stringify(MessageBody));
260
+ window.__originConsoleInfo(...args);
261
+ }
262
+ console.warn = (...args) => {
263
+ const MessageBody = { type: 'NexfepConsoleWarn', message: args }
264
+ window.ipc.postMessage(JSON.stringify(MessageBody));
265
+ window.__originConsoleWarn(...args);
266
+ }
267
+ console.debug = (...args) => {
268
+ const MessageBody = { type: 'NexfepConsoleDebug', message: args }
269
+ window.ipc.postMessage(JSON.stringify(MessageBody));
270
+ window.__originConsoleDebug(...args);
271
+ }
238
272
 
239
273
  // 注入 CSS 样式
240
274
  const style = document.createElement('style');
@@ -250,99 +284,118 @@ class WindowPool {
250
284
  await this.__injectCode(windowObj, INJECT_CODE);
251
285
  }
252
286
  async __createNewWindowObj() {
253
- const window = this.app.createBrowserWindow({ title: "Nexfep" });
254
- window.hide();
255
- this.freeWindowCount++;
256
- const webview = window.createWebview();
257
- const windowObj = new Window(window, webview, ++this.windowCount, this);
258
- this.windows.push(windowObj);
259
- webview.onIpcMessage((data) => {
260
- const dataText = data.body.toString();
261
- const dataObj = JSON.parse(dataText);
262
- if (dataObj.type == 'NexfepBeforeUnload') {
263
- webview.evaluateScript(`if(window?.isNexfepLoadDone){
264
- const MessageBody = { type: 'NexfepBeforeUnload' }
265
- window.ipc.postMessage(JSON.stringify(MessageBody));
266
- }else{
267
- const MessageBody = { type: 'NexfepLoadFalse' }
268
- window.ipc.postMessage(JSON.stringify(MessageBody));
269
- }`);
270
- }
271
- else if (dataObj.type == 'NexfepLoadFalse') {
272
- this.__injectControlFunctions(windowObj);
273
- }
274
- else if (dataObj.type == 'NexfepCloseWindow') {
275
- this.closeWindow(windowObj);
276
- }
277
- else if (dataObj.type == 'NexfepMinimizeWindow') {
278
- window.setMinimized(true);
279
- }
280
- else if (dataObj.type == 'NexfepUnMinimizeWindow') {
281
- window.setMinimized(false);
282
- }
283
- else if (dataObj.type == 'NexfepMaximizeWindow') {
284
- window.setMaximized(true);
285
- }
286
- else if (dataObj.type == 'NexfepUnMaximizeWindow') {
287
- window.setMaximized(false);
288
- }
289
- else if (dataObj.type == 'NexfepSetTitle') {
290
- window.setTitle(dataObj.title);
291
- }
292
- else if (dataObj.type == 'NexfepOpenDevTools') {
293
- webview.openDevtools();
294
- }
295
- else if (dataObj.type == 'NexfepCloseDevTools') {
296
- webview.closeDevtools();
297
- }
298
- else if (dataObj.type == 'CustomMessage') {
299
- this.onCustomMessage(windowObj, dataObj.data);
300
- }
301
- else if (dataObj.type == 'NexfepInvoke') {
302
- const handlers = this.handlers.get(dataObj.event) || [];
303
- handlers.forEach(async (handler) => {
304
- const result = await handler(dataObj.data);
305
- if (result) {
306
- webview.evaluateScript(`
307
- window.dispatchEvent(new CustomEvent('nexfep-invoke-result-${dataObj.eventId[0]}-${dataObj.eventId[1]}', { detail: ${JSON.stringify(result)} }));
308
- `);
287
+ return await new Promise((resolve, reject) => {
288
+ setImmediate(() => {
289
+ const window = this.app.createBrowserWindow({ title: "Nexfep" });
290
+ window.hide();
291
+ this.freeWindowCount++;
292
+ const webview = window.createWebview();
293
+ const windowObj = new Window(window, webview, ++this.windowCount, this);
294
+ this.windows.push(windowObj);
295
+ webview.onIpcMessage((data) => {
296
+ const dataText = data.body.toString();
297
+ const dataObj = JSON.parse(dataText);
298
+ if (dataObj.type == 'NexfepBeforeUnload') {
299
+ webview.evaluateScript(`if(window?.isNexfepLoadDone){
300
+ const MessageBody = { type: 'NexfepBeforeUnload' }
301
+ window.ipc.postMessage(JSON.stringify(MessageBody));
302
+ }else{
303
+ const MessageBody = { type: 'NexfepLoadFalse' }
304
+ window.ipc.postMessage(JSON.stringify(MessageBody));
305
+ }`);
306
+ }
307
+ else if (dataObj.type == 'NexfepLoadFalse') {
308
+ this.__injectControlFunctions(windowObj);
309
+ }
310
+ else if (dataObj.type == 'NexfepCloseWindow') {
311
+ this.closeWindow(windowObj);
309
312
  }
310
- else {
313
+ else if (dataObj.type == 'NexfepMinimizeWindow') {
314
+ window.setMinimized(true);
315
+ }
316
+ else if (dataObj.type == 'NexfepUnMinimizeWindow') {
317
+ window.setMinimized(false);
318
+ }
319
+ else if (dataObj.type == 'NexfepMaximizeWindow') {
320
+ window.setMaximized(true);
321
+ }
322
+ else if (dataObj.type == 'NexfepUnMaximizeWindow') {
323
+ window.setMaximized(false);
324
+ }
325
+ else if (dataObj.type == 'NexfepSetTitle') {
326
+ window.setTitle(dataObj.title);
327
+ }
328
+ else if (dataObj.type == 'NexfepOpenDevTools') {
329
+ webview.openDevtools();
330
+ }
331
+ else if (dataObj.type == 'NexfepCloseDevTools') {
332
+ webview.closeDevtools();
333
+ }
334
+ else if (dataObj.type == 'CustomMessage') {
335
+ this.onCustomMessage(windowObj, dataObj.data);
336
+ }
337
+ else if (dataObj.type == 'NexfepInvoke') {
338
+ const handlers = this.handlers.get(dataObj.event) || [];
339
+ handlers.forEach(async (handler) => {
340
+ const result = await handler(dataObj.data);
341
+ if (result) {
342
+ webview.evaluateScript(`
343
+ window.dispatchEvent(new CustomEvent('nexfep-invoke-result-${dataObj.eventId[0]}-${dataObj.eventId[1]}', { detail: ${JSON.stringify(result)} }));
344
+ `);
345
+ }
346
+ else {
347
+ webview.evaluateScript(`
348
+ window.dispatchEvent(new CustomEvent('nexfep-invoke-result-${dataObj.eventId[0]}-${dataObj.eventId[1]}', { detail: undefined }));
349
+ `);
350
+ }
351
+ });
352
+ }
353
+ else if (dataObj.type == 'NexfepSetGlobal') {
354
+ this.global.set(dataObj.name, dataObj.value);
355
+ }
356
+ else if (dataObj.type == 'NexfepGetGlobal') {
357
+ const value = this.global.get(dataObj.name);
311
358
  webview.evaluateScript(`
312
- window.dispatchEvent(new CustomEvent('nexfep-invoke-result-${dataObj.eventId[0]}-${dataObj.eventId[1]}', { detail: undefined }));
359
+ window.dispatchEvent(new CustomEvent('nexfep-get-global-result-${dataObj.eventId[0]}-${dataObj.eventId[1]}', { detail: ${JSON.stringify(value)} }));
313
360
  `);
314
361
  }
315
- });
316
- }
317
- else if (dataObj.type == 'NexfepSetGlobal') {
318
- this.global.set(dataObj.name, dataObj.value);
319
- }
320
- else if (dataObj.type == 'NexfepGetGlobal') {
321
- const value = this.global.get(dataObj.name);
322
- webview.evaluateScript(`
323
- window.dispatchEvent(new CustomEvent('nexfep-get-global-result-${dataObj.eventId[0]}-${dataObj.eventId[1]}', { detail: ${JSON.stringify(value)} }));
324
- `);
325
- }
326
- else if (dataObj.type == 'NexfepBroadcast') {
327
- this.windows.forEach(async (w) => {
328
- if (w != windowObj && w.isOpen) {
329
- w.webview.evaluateScript(`
330
- window.dispatchEvent(new CustomEvent('${dataObj.name}', { detail: ${JSON.stringify(dataObj.data)} }));
331
- `);
362
+ else if (dataObj.type == 'NexfepBroadcast') {
363
+ this.windows.forEach(async (w) => {
364
+ if (w != windowObj && w.isOpen) {
365
+ w.webview.evaluateScript(`
366
+ window.dispatchEvent(new CustomEvent('${dataObj.name}', { detail: ${JSON.stringify(dataObj.data)} }));
367
+ `);
368
+ }
369
+ });
332
370
  }
333
- });
334
- }
335
- else if (dataObj.type == 'NexfepTell') {
336
- this.windows.forEach(async (w) => {
337
- if (w.id == dataObj.to) {
338
- w.webview.evaluateScript(`
339
- window.dispatchEvent(new CustomEvent('${dataObj.message}', { detail: ${JSON.stringify(dataObj.data)} }));
340
- `);
371
+ else if (dataObj.type == 'NexfepTell') {
372
+ this.windows.forEach(async (w) => {
373
+ if (w.id == dataObj.to) {
374
+ w.webview.evaluateScript(`
375
+ window.dispatchEvent(new CustomEvent('${dataObj.message}', { detail: ${JSON.stringify(dataObj.data)} }));
376
+ `);
377
+ }
378
+ });
379
+ }
380
+ else if (dataObj.type == 'NexfepConsoleLog') {
381
+ this.logger.__printLog(windowObj.id, 'Log', dataObj.message);
382
+ }
383
+ else if (dataObj.type == 'NexfepConsoleError') {
384
+ this.logger.__printLog(windowObj.id, 'Error', dataObj.message, '\x1b[31m', '\x1b[0m');
385
+ }
386
+ else if (dataObj.type == 'NexfepConsoleInfo') {
387
+ this.logger.__printLog(windowObj.id, 'Info', dataObj.message, '\x1b[34m', '\x1b[0m');
388
+ }
389
+ else if (dataObj.type == 'NexfepConsoleWarn') {
390
+ this.logger.__printLog(windowObj.id, 'Warn', dataObj.message, '\x1b[33m', '\x1b[0m');
391
+ }
392
+ else if (dataObj.type == 'NexfepConsoleDebug') {
393
+ this.logger.__printLog(windowObj.id, 'Debug', dataObj.message, '\x1b[90m', '\x1b[0m');
341
394
  }
342
395
  });
343
- }
396
+ resolve(windowObj);
397
+ });
344
398
  });
345
- return windowObj;
346
399
  }
347
400
  async createWindow(isShow = true, isDecorated = true) {
348
401
  const window = this.windows.find(w => w.isOpen === false) || await this.__createNewWindowObj();
@@ -356,7 +409,7 @@ class WindowPool {
356
409
  }
357
410
  }
358
411
  if (this.freeWindowCount == 0) {
359
- await this.__createNewWindowObj();
412
+ this.__createNewWindowObj();
360
413
  }
361
414
  return window;
362
415
  }