clickgo-native 0.0.7-dev7 → 0.0.9

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.
@@ -0,0 +1,21 @@
1
+ import * as libFs from './lib/fs';
2
+ export declare const fs: typeof libFs;
3
+ import * as libTool from './lib/tool';
4
+ export declare const tool: typeof libTool;
5
+ export declare abstract class AbstractBoot {
6
+ get isImmersion(): boolean;
7
+ get hasFrame(): boolean;
8
+ get isNoFormQuit(): boolean;
9
+ get platform(): NodeJS.Platform;
10
+ get token(): string;
11
+ abstract main(): void | Promise<void>;
12
+ run(path: string, opt?: {
13
+ 'frame'?: boolean;
14
+ 'quit'?: boolean;
15
+ }): void;
16
+ on(name: string, handler: (...param: any[]) => any | Promise<any>, once?: boolean): void;
17
+ once(name: string, handler: (...param: any[]) => any | Promise<any>): void;
18
+ off(name: string): void;
19
+ }
20
+ export declare function launcher(boot: AbstractBoot): void;
21
+ export declare function verifyToken(t: string): boolean;
package/dist/index.js CHANGED
@@ -96,7 +96,7 @@ const methods = {
96
96
  'cg-set-state': {
97
97
  'once': false,
98
98
  handler: function (t, state) {
99
- if (!hasFrame || !form || !state) {
99
+ if (!form || !state) {
100
100
  return;
101
101
  }
102
102
  if (!verifyToken(t)) {
@@ -0,0 +1,21 @@
1
+ export declare function refreshDrives(): Promise<void>;
2
+ export declare function getContent(path: string, options: {
3
+ 'encoding'?: BufferEncoding;
4
+ 'start'?: number;
5
+ 'end'?: number;
6
+ }): Promise<string | Buffer | null>;
7
+ export declare function putContent(path: string, data: string | Buffer, options: {
8
+ 'encoding'?: BufferEncoding;
9
+ 'mode'?: number;
10
+ 'flag'?: string;
11
+ }): Promise<boolean>;
12
+ export declare function readLink(path: string, encoding?: BufferEncoding): Promise<string | null>;
13
+ export declare function symlink(filePath: string, linkPath: string, type?: 'dir' | 'file' | 'junction'): Promise<boolean>;
14
+ export declare function unlink(path: string): Promise<boolean>;
15
+ export declare function stats(path: string): Promise<Record<string, any> | null>;
16
+ export declare function mkdir(path: string, mode: number): Promise<boolean>;
17
+ export declare function rmdir(path: string): Promise<boolean>;
18
+ export declare function chmod(path: string, mod: string | number): Promise<boolean>;
19
+ export declare function rename(oldPath: string, newPath: string): Promise<boolean>;
20
+ export declare function readDir(path: string, encoding?: BufferEncoding): Promise<any[]>;
21
+ export declare function copyFile(src: string, dest: string): Promise<boolean>;
@@ -0,0 +1 @@
1
+ export declare function sleep(ms: number): Promise<void>;
package/dist/pre.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clickgo-native",
3
- "version": "0.0.7-dev7",
3
+ "version": "0.0.9",
4
4
  "description": "The software developed with ClickGo will run in Windows, Mac OS, Linux.",
5
5
  "keywords": [
6
6
  "clickgo",
@@ -15,13 +15,11 @@
15
15
  },
16
16
  "devDependencies": {
17
17
  "@litert/eslint-plugin-rules": "^0.3.1",
18
- "@litert/loader": "^3.5.9",
19
- "@types/node": "^24.0.3",
20
- "clickgo": "^3.16.16",
21
- "jszip": "^3.10.1",
18
+ "@types/node": "^24.0.13",
22
19
  "typescript": "^5.8.3"
23
20
  },
24
21
  "dependencies": {
25
- "electron": "^36.4.0"
22
+ "clickgo": "^3.16.17",
23
+ "electron": "^37.2.1"
26
24
  }
27
25
  }
package/dist/index.ts DELETED
@@ -1,537 +0,0 @@
1
- import * as electron from 'electron';
2
- import * as path from 'path';
3
- import * as libFs from './lib/fs';
4
- export const fs = libFs;
5
- import * as libTool from './lib/tool';
6
- export const tool = libTool;
7
-
8
- // npm publish --tag dev --access public
9
- // --- sass --watch dist/:dist/ --style compressed --no-source-map ---
10
-
11
- /** --- 是否是沉浸式的 --- */
12
- let isImmersion: boolean = false;
13
- // --- windows 下是且只会是沉浸式,其他系统以启动的第一个窗体为准绑定大小最小化最大化关闭等功能 ---
14
-
15
- /** --- 窗体是否含有 border 边框,有的话将不是沉浸式,也不会绑定任何窗体的大小和相关事件 --- */
16
- let hasFrame: boolean = false;
17
-
18
- /** --- 是否没有物理窗体时就主动退出软件(进程结束),如果不要窗体只有 node 的话,会用到 false 的情况 --- */
19
- let isNoFormQuit: boolean = true;
20
-
21
- /** --- 主窗体 --- */
22
- let form: electron.BrowserWindow | undefined;
23
-
24
- /** --- 当前设定的通讯 token --- */
25
- let token: string = '';
26
-
27
- /** --- 当前系统平台 --- */
28
- const platform: NodeJS.Platform = process.platform;
29
- // const platform: NodeJS.Platform = 'darwin';
30
-
31
- /** --- 监听前台的执行的方法,内置的方法,用户可调用方法自行添加 --- */
32
- const methods: Record<string, {
33
- 'once': boolean;
34
- 'handler': (...param: any[]) => any | Promise<any>;
35
- }> = {
36
- // --- 成功运行一个 task 后 init ---
37
- 'cg-init': {
38
- 'once': true,
39
- handler: function(t: string) {
40
- // --- t 是网页传来的 token ---
41
- if (!t || !form) {
42
- return;
43
- }
44
- form.resizable = true;
45
- token = t;
46
- }
47
- },
48
- // --- 完全退出软件进程 ---
49
- 'cg-quit': {
50
- 'once': false,
51
- handler: function(t: string): void {
52
- if (!t || !form) {
53
- return;
54
- }
55
- if (!verifyToken(t)) {
56
- return;
57
- }
58
- electron.app.quit();
59
- }
60
- },
61
- // --- 设置实体窗体大小,仅非沉浸式可设置 ---
62
- 'cg-set-size': {
63
- 'once': false,
64
- handler: function(t: string, width: number, height: number): void {
65
- if (isImmersion || !form || !width || !height) {
66
- return;
67
- }
68
- if (!verifyToken(t)) {
69
- return;
70
- }
71
- form.setSize(width, height);
72
- form.center();
73
- }
74
- },
75
- // --- 设置窗体最大化、最小化、还原,仅非 frame 可设置 ---
76
- 'cg-set-state': {
77
- 'once': false,
78
- handler: function(t: string, state: string): void {
79
- if (!hasFrame || !form || !state) {
80
- return;
81
- }
82
- if (!verifyToken(t)) {
83
- return;
84
- }
85
- switch (state) {
86
- case 'max': {
87
- form.maximize();
88
- break;
89
- }
90
- case 'min': {
91
- form.minimize();
92
- break;
93
- }
94
- default: {
95
- form.restore();
96
- }
97
- }
98
- }
99
- },
100
- // --- 激活窗体 ---
101
- 'cg-activate': {
102
- 'once': false,
103
- handler: function(t: string): void {
104
- if (!form) {
105
- return;
106
- }
107
- if (!verifyToken(t)) {
108
- return;
109
- }
110
- if (form.isMinimized()) {
111
- form.restore();
112
- }
113
- form.setAlwaysOnTop(true);
114
- form.show();
115
- form.focus();
116
- form.setAlwaysOnTop(false);
117
- },
118
- },
119
- // --- 关闭窗体(可能软件进程不会被退出) ---
120
- 'cg-close': {
121
- 'once': false,
122
- handler: function(t: string): void {
123
- if (!form) {
124
- return;
125
- }
126
- if (!verifyToken(t)) {
127
- return;
128
- }
129
- form.close();
130
- }
131
- },
132
- // --- 设置 IgnoreMouseEvents,仅限沉浸式 ---
133
- 'cg-mouse-ignore': {
134
- 'once': false,
135
- handler: function(t: string, val: boolean): void {
136
- if (!isImmersion || !form) {
137
- return;
138
- }
139
- if (!verifyToken(t)) {
140
- return;
141
- }
142
- if (val) {
143
- form.setIgnoreMouseEvents(true, { 'forward': true });
144
- }
145
- else {
146
- form.setIgnoreMouseEvents(false);
147
- }
148
- }
149
- },
150
- // --- 是否允许最大化 ---
151
- 'cg-maximizable': {
152
- 'once': false,
153
- handler: function(t: string, val: boolean): void {
154
- if (isImmersion || !form) {
155
- return;
156
- }
157
- if (!verifyToken(t)) {
158
- return;
159
- }
160
- form.setMaximizable(val);
161
- }
162
- },
163
-
164
- // --- fs 相关操作 ---
165
-
166
- 'cg-fs-getContent': {
167
- 'once': false,
168
- handler: async function(
169
- t: string,
170
- path: string,
171
- options: Record<string, any>
172
- ): Promise<string | Buffer | null> {
173
- if (!verifyToken(t)) {
174
- return null;
175
- }
176
- return libFs.getContent(path, options);
177
- }
178
- },
179
- 'cg-fs-putContent': {
180
- 'once': false,
181
- handler: async function(
182
- t: string, path: string, data: string | Buffer, options: Record<string, any>
183
- ): Promise<boolean> {
184
- if (!verifyToken(t)) {
185
- return false;
186
- }
187
- return fs.putContent(path, data, options);
188
- }
189
- },
190
- 'cg-fs-readLink': {
191
- 'once': false,
192
- handler: async function(t: string, path: string, encoding?: BufferEncoding): Promise<string | null> {
193
- if (!verifyToken(t)) {
194
- return null;
195
- }
196
- return fs.readLink(path, encoding);
197
- }
198
- },
199
- 'cg-fs-symlink': {
200
- 'once': false,
201
- handler: async function(t: string, filePath: string, linkPath: string, type?: 'dir' | 'file' | 'junction'): Promise<boolean> {
202
- if (!verifyToken(t)) {
203
- return false;
204
- }
205
- return fs.symlink(filePath, linkPath, type);
206
- }
207
- },
208
- 'cg-fs-unlink': {
209
- 'once': false,
210
- handler: async function(t: string, path: string): Promise<boolean> {
211
- if (!verifyToken(t)) {
212
- return false;
213
- }
214
- return fs.unlink(path);
215
- }
216
- },
217
- 'cg-fs-stats': {
218
- 'once': false,
219
- handler: async function(t: string, path: string): Promise<Record<string, any> | null> {
220
- if (!verifyToken(t)) {
221
- return null;
222
- }
223
- return fs.stats(path);
224
- }
225
- },
226
- 'cg-fs-mkdir': {
227
- 'once': false,
228
- handler: async function(t: string, path: string, mode: number): Promise<boolean> {
229
- if (!verifyToken(t)) {
230
- return false;
231
- }
232
- return fs.mkdir(path, mode);
233
- }
234
- },
235
- 'cg-fs-rmdir': {
236
- 'once': false,
237
- handler: async function(t: string, path: string): Promise<boolean> {
238
- if (!verifyToken(t)) {
239
- return false;
240
- }
241
- return fs.rmdir(path);
242
- }
243
- },
244
- 'cg-fs-chmod': {
245
- 'once': false,
246
- handler: async function(t: string, path: string, mod: string | number): Promise<boolean> {
247
- if (!verifyToken(t)) {
248
- return false;
249
- }
250
- return fs.chmod(path, mod);
251
- }
252
- },
253
- 'cg-fs-rename': {
254
- 'once': false,
255
- handler: async function(t: string, oldPath: string, newPath: string): Promise<boolean> {
256
- if (!verifyToken(t)) {
257
- return false;
258
- }
259
- return fs.rename(oldPath, newPath);
260
- }
261
- },
262
- 'cg-fs-readDir': {
263
- 'once': false,
264
- handler: async function(t: string, path: string, encoding?: BufferEncoding): Promise<any[]> {
265
- if (!verifyToken(t)) {
266
- return [];
267
- }
268
- return fs.readDir(path, encoding);
269
- }
270
- },
271
- 'cg-fs-copyFile': {
272
- 'once': false,
273
- handler: async function(t: string, src: string, dest: string): Promise<boolean> {
274
- if (!verifyToken(t)) {
275
- return false;
276
- }
277
- return fs.copyFile(src, dest);
278
- }
279
- },
280
-
281
- // --- 无需校验码 ---
282
-
283
- // --- 测试与 native 的连通性 ---
284
- 'cg-ping': {
285
- 'once': false,
286
- handler: function(t: string): string {
287
- // --- t 不是 token,传过来什么就会传回去 ---
288
- return 'pong: ' + t;
289
- }
290
- },
291
- // --- 判断窗体是否是最大化状态 ---
292
- 'cg-is-max': {
293
- 'once': false,
294
- handler: function(): boolean {
295
- return form?.isMaximized ? true : false;
296
- }
297
- }
298
- };
299
-
300
- /** --- 全局类 --- */
301
- export abstract class AbstractBoot {
302
-
303
- /**
304
- * --- 是否是沉浸式运行 ---
305
- */
306
- public get isImmersion(): boolean {
307
- return isImmersion;
308
- }
309
-
310
- /**
311
- * --- 是否含有实体窗体边框和标题 ---
312
- */
313
- public get hasFrame(): boolean {
314
- return hasFrame;
315
- }
316
-
317
- /**
318
- * --- 没有实体窗体时整个实体进程是不是会被结束 ---
319
- */
320
- public get isNoFormQuit(): boolean {
321
- return isNoFormQuit;
322
- }
323
-
324
- /**
325
- * --- 当前系统代号 ---
326
- */
327
- public get platform(): NodeJS.Platform {
328
- return platform;
329
- }
330
-
331
- /**
332
- * --- 当前的通讯 token ---
333
- */
334
- public get token(): string {
335
- return token;
336
- }
337
-
338
- /** --- 入口方法 --- */
339
- public abstract main(): void | Promise<void>;
340
-
341
- /**
342
- * --- 开始运行起来一个主实体窗体,整个进程本方法只能执行一次 ---
343
- * @param path 实体窗体网页路径
344
- * @param opt 参数
345
- */
346
- public run(path: string, opt: { 'frame'?: boolean; 'quit'?: boolean; } = {}): void {
347
- if (opt.frame !== undefined) {
348
- // --- 默认 false ---
349
- hasFrame = opt.frame;
350
- }
351
- if (opt.quit !== undefined) {
352
- // --- 默认 true ---
353
- isNoFormQuit = opt.quit;
354
- }
355
- // --- 判断是否是沉浸式 ---
356
- if (platform === 'win32') {
357
- // --- 是 Windows 才有可能是沉浸式 ---
358
- if (!hasFrame) {
359
- // --- 实体窗体没有边框,一定是沉浸式 ---
360
- isImmersion = true;
361
- }
362
- }
363
- // --- 创建实体窗体 ---
364
- createForm(path);
365
- // --- 监听所有实体窗体关闭事件 ---
366
- electron.app.on('window-all-closed', function(): void {
367
- if (isNoFormQuit) {
368
- electron.app.quit();
369
- }
370
- });
371
- // --- 软件被活动性激活的事件 ---
372
- electron.app.on('activate', function(): void {
373
- if (electron.BrowserWindow.getAllWindows().length > 0) {
374
- return;
375
- }
376
- createForm(path);
377
- // --- 成功运行一个 task 后再次添加 init ---
378
- methods['cg-init'] = {
379
- 'once': true,
380
- handler: function(t: string) {
381
- // --- t 是网页传来的 token ---
382
- if (!t || !form) {
383
- return;
384
- }
385
- form.resizable = true;
386
- token = t;
387
- }
388
- };
389
- });
390
- }
391
-
392
- /**
393
- * --- 绑定监听网页调用方法的方法 ---
394
- * @param name 方法名
395
- * @param handler 要执行的函数
396
- * @param once 是否只执行一次
397
- */
398
- public on(
399
- name: string,
400
- handler: (...param: any[]) => any | Promise<any>,
401
- once: boolean = false
402
- ): void {
403
- methods[name] = {
404
- 'once': once,
405
- 'handler': handler
406
- };
407
- }
408
-
409
- /**
410
- * --- 绑定监听网页调用方法的方法但只会执行一次 ---
411
- * @param name 方法名
412
- * @param handler 要执行的函数
413
- */
414
- public once(name: string, handler: (...param: any[]) => any | Promise<any>): void {
415
- this.on(name, handler, true);
416
- }
417
-
418
- /**
419
- * --- 解绑监听的方法 ---
420
- * @param name 方法名
421
- */
422
- public off(name: string): void {
423
- if (!methods[name]) {
424
- return;
425
- }
426
- delete methods[name];
427
- }
428
-
429
- }
430
-
431
- /** --- 用户调用运行 boot 类 --- */
432
- export function launcher(boot: AbstractBoot): void {
433
- (async function() {
434
- // --- 等到 native 环境装载完毕 ---
435
- await electron.app.whenReady();
436
- await fs.refreshDrives();
437
- // --- 执行回调 ---
438
- await boot.main();
439
- })().catch(function() {
440
- return;
441
- });
442
- }
443
-
444
- // --- 系统启动 ---
445
-
446
- electron.Menu.setApplicationMenu(null);
447
-
448
- // --- 实际用来监听网页传输过来的数据 ---
449
- electron.ipcMain.handle('pre', function(e: electron.IpcMainInvokeEvent, name: string, ...param: any[]): any {
450
- if (!methods[name]) {
451
- return;
452
- }
453
- const r = methods[name].handler(...param);
454
- if (methods[name].once) {
455
- delete methods[name];
456
- }
457
- return r;
458
- });
459
-
460
- /**
461
- * --- 验证 token 是否正确 ---
462
- * @param t 要验证的 token
463
- */
464
- export function verifyToken(t: string): boolean {
465
- if (t !== token) {
466
- return false;
467
- }
468
- return true;
469
- }
470
-
471
- /**
472
- * --- 内部调用用来创建实体窗体的函数 ---
473
- * @param p 窗体网页路径
474
- */
475
- function createForm(p: string): void {
476
- const op: Electron.BrowserWindowConstructorOptions = {
477
- 'webPreferences': {
478
- 'nodeIntegration': false,
479
- 'contextIsolation': true,
480
- 'preload': path.join(__dirname, '/pre.js')
481
- },
482
- 'width': hasFrame ? 800 : 500,
483
- 'height': hasFrame ? 700 : 300,
484
- 'frame': hasFrame,
485
- 'resizable': false,
486
- 'show': false,
487
- 'center': true,
488
- 'transparent': isImmersion ? true : false
489
- };
490
- form = new electron.BrowserWindow(op);
491
- form.webContents.userAgent = 'electron/' + electron.app.getVersion() + ' ' + platform + '/' + process.arch + ' immersion/' + (isImmersion ? '1' : '0') + ' frame/' + (hasFrame ? '1' : '0');
492
- // form.webContents.openDevTools();
493
- form.once('ready-to-show', function(): void {
494
- if (!form) {
495
- return;
496
- }
497
- if (isImmersion) {
498
- // --- 沉浸式默认最大化 ---
499
- form.maximize();
500
- form.setIgnoreMouseEvents(true, { 'forward': true });
501
- }
502
- else {
503
- // --- 设置为第一个窗体的大小 ---
504
- }
505
- form.show();
506
- });
507
- if (p.startsWith('https://') || p.startsWith('http://')) {
508
- // --- 加载网页 ---
509
- form.loadURL(p).catch(function(e): void {
510
- throw e;
511
- });
512
- }
513
- else {
514
- // --- 加载本地文件 ---
515
- const lio = p.indexOf('?');
516
- const search = lio === -1 ? '' : p.slice(lio + 1);
517
- if (lio !== -1) {
518
- p = p.slice(0, lio);
519
- }
520
- form.loadFile(p, {
521
- 'search': search
522
- }).catch(function(e): void {
523
- throw e;
524
- });
525
- }
526
- form.on('close', function() {
527
- form = undefined;
528
- });
529
- // --- 最大化事件 ---
530
- form.on('maximize', function() {
531
- form?.webContents.executeJavaScript('if(window.clickgoNativeWeb){clickgoNativeWeb.invoke("maximize")}') as any;
532
- });
533
- // --- 最大化还原 ---
534
- form.on('unmaximize', function() {
535
- form?.webContents.executeJavaScript('if(window.clickgoNativeWeb){clickgoNativeWeb.invoke("unmaximize")}') as any;
536
- });
537
- }
package/dist/lib/fs.ts DELETED
@@ -1,287 +0,0 @@
1
- import * as fs from 'fs';
2
- import * as tool from './tool';
3
-
4
- /** --- 当前系统平台 --- */
5
- const platform: NodeJS.Platform = process.platform;
6
-
7
- /** --- path 要额外处理一下,因为 windows 下可能有点不一样 --- */
8
- function formatPath(path: string): string {
9
- if (platform !== 'win32') {
10
- return path;
11
- }
12
- if (path === '/') {
13
- return path;
14
- }
15
- return path[1] + ':' + path.slice(2);
16
- }
17
-
18
- /** --- windows 下驱动器列表 --- */
19
- const drives: string[] = [];
20
-
21
- /** --- 更新 drives 列表 --- */
22
- export async function refreshDrives(): Promise<void> {
23
- if (platform !== 'win32') {
24
- return;
25
- }
26
- drives.length = 0;
27
- for (let i = 0; i < 26; ++i) {
28
- const char = String.fromCharCode(97 + i);
29
- try {
30
- await fs.promises.stat(char + ':/');
31
- drives.push(char);
32
- }
33
- catch {
34
- // --- 无所谓 ---
35
- }
36
- }
37
- }
38
-
39
- export async function getContent(
40
- path: string,
41
- options: {
42
- 'encoding'?: BufferEncoding;
43
- 'start'?: number;
44
- 'end'?: number;
45
- }
46
- ): Promise<string | Buffer | null> {
47
- path = formatPath(path);
48
- const encoding = options.encoding;
49
- const start = options.start;
50
- const end = options.end;
51
- if (start || end) {
52
- return new Promise(function(resolve) {
53
- const rs = fs.createReadStream(path, {
54
- 'encoding': encoding,
55
- 'start': start,
56
- 'end': end
57
- });
58
- const data: Buffer[] = [];
59
- rs.on('data', function(chunk) {
60
- if (!(chunk instanceof Buffer)) {
61
- return;
62
- }
63
- data.push(chunk);
64
- }).on('end', function() {
65
- const buf = Buffer.concat(data);
66
- if (encoding) {
67
- resolve(buf.toString());
68
- }
69
- else {
70
- resolve(buf);
71
- }
72
- }).on('error', function() {
73
- resolve(null);
74
- });
75
- });
76
- }
77
- else {
78
- try {
79
- if (encoding) {
80
- return await fs.promises.readFile(path, {
81
- 'encoding': encoding
82
- });
83
- }
84
- else {
85
- return await fs.promises.readFile(path);
86
- }
87
- }
88
- catch {
89
- return null;
90
- }
91
- }
92
- }
93
-
94
- export async function putContent(path: string, data: string | Buffer, options: {
95
- 'encoding'?: BufferEncoding;
96
- 'mode'?: number;
97
- 'flag'?: string;
98
- }): Promise<boolean> {
99
- path = formatPath(path);
100
- try {
101
- await fs.promises.writeFile(path, data, options);
102
- return true;
103
- }
104
- catch {
105
- return false;
106
- }
107
- }
108
-
109
- export async function readLink(path: string, encoding?: BufferEncoding): Promise<string | null> {
110
- path = formatPath(path);
111
- try {
112
- return await fs.promises.readlink(path, {
113
- 'encoding': encoding
114
- });
115
- }
116
- catch {
117
- return null;
118
- }
119
- }
120
-
121
- export async function symlink(filePath: string, linkPath: string, type?: 'dir' | 'file' | 'junction'): Promise<boolean> {
122
- filePath = formatPath(filePath);
123
- linkPath = formatPath(linkPath);
124
- try {
125
- await fs.promises.symlink(filePath, linkPath, type);
126
- return true;
127
- }
128
- catch {
129
- return false;
130
- }
131
- }
132
-
133
- export async function unlink(path: string): Promise<boolean> {
134
- path = formatPath(path);
135
- for (let i = 0; i <= 2; ++i) {
136
- try {
137
- await fs.promises.unlink(path);
138
- return true;
139
- }
140
- catch {
141
- await tool.sleep(250);
142
- }
143
- }
144
- try {
145
- await fs.promises.unlink(path);
146
- return true;
147
- }
148
- catch {
149
- return false;
150
- }
151
- }
152
-
153
- export async function stats(path: string): Promise<Record<string, any> | null> {
154
- path = formatPath(path);
155
- try {
156
- const item = await fs.promises.lstat(path);
157
- return {
158
- isFile: item.isFile(),
159
- isDirectory: item.isDirectory(),
160
- isSymbolicLink: item.isSymbolicLink(),
161
- 'size': item.size,
162
- 'blksize': item.blksize,
163
- 'atimeMs': item.atimeMs,
164
- 'mtimeMs': item.mtimeMs,
165
- 'ctimeMs': item.ctimeMs,
166
- 'birthtimeMs': item.birthtimeMs,
167
- 'atime': item.atime,
168
- 'mtime': item.mtime,
169
- 'ctime': item.ctime,
170
- 'birthtime': item.birthtime
171
- };
172
- }
173
- catch {
174
- return null;
175
- }
176
- }
177
-
178
- export async function mkdir(path: string, mode: number): Promise<boolean> {
179
- path = formatPath(path);
180
- const stats = await fs.promises.lstat(path);
181
- if (stats.isDirectory()) {
182
- return true;
183
- }
184
- // --- 深度创建目录 ---
185
- try {
186
- await fs.promises.mkdir(path, {
187
- 'recursive': true,
188
- 'mode': mode
189
- });
190
- return true;
191
- }
192
- catch {
193
- return false;
194
- }
195
- }
196
-
197
- export async function rmdir(path: string): Promise<boolean> {
198
- path = formatPath(path);
199
- const stats = await fs.promises.lstat(path);
200
- if (!stats.isDirectory()) {
201
- return true;
202
- }
203
- try {
204
- await fs.promises.rmdir(path);
205
- return true;
206
- }
207
- catch {
208
- return false;
209
- }
210
- }
211
-
212
- export async function chmod(path: string, mod: string | number): Promise<boolean> {
213
- path = formatPath(path);
214
- try {
215
- await fs.promises.chmod(path, mod);
216
- return true;
217
- }
218
- catch {
219
- return false;
220
- }
221
- }
222
-
223
- export async function rename(oldPath: string, newPath: string): Promise<boolean> {
224
- oldPath = formatPath(oldPath);
225
- newPath = formatPath(newPath);
226
- try {
227
- await fs.promises.rename(oldPath, newPath);
228
- return true;
229
- }
230
- catch {
231
- return false;
232
- }
233
- }
234
-
235
- export async function readDir(path: string, encoding?: BufferEncoding): Promise<any[]> {
236
- try {
237
- const list: any[] = [];
238
- if (platform === 'win32') {
239
- // --- 此处不能用 formatPath,因为还要读 drives ---
240
- if (path === '/') {
241
- for (const item of drives) {
242
- list.push({
243
- isFile: false,
244
- isDirectory: true,
245
- isSymbolicLink: false,
246
- 'name': item
247
- });
248
- }
249
- return list;
250
- }
251
- else {
252
- path = path[1] + ':' + path.slice(2);
253
- }
254
- }
255
- const dlist = await fs.promises.readdir(path, {
256
- 'encoding': encoding,
257
- 'withFileTypes': true
258
- });
259
- for (const item of dlist) {
260
- if (item.name === '.' || item.name === '..') {
261
- continue;
262
- }
263
- list.push({
264
- isFile: item.isFile(),
265
- isDirectory: item.isDirectory(),
266
- isSymbolicLink: item.isSymbolicLink(),
267
- 'name': item.name
268
- });
269
- }
270
- return list;
271
- }
272
- catch {
273
- return [];
274
- }
275
- }
276
-
277
- export async function copyFile(src: string, dest: string): Promise<boolean> {
278
- src = formatPath(src);
279
- dest = formatPath(dest);
280
- try {
281
- await fs.promises.copyFile(src, dest);
282
- return true;
283
- }
284
- catch {
285
- return false;
286
- }
287
- }
package/dist/lib/tool.ts DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * --- 间隔一段时间 ---
3
- * @param ms 间隔毫秒
4
- */
5
- export function sleep(ms: number): Promise<void> {
6
- return new Promise(function(resolve) {
7
- setTimeout(function() {
8
- resolve();
9
- }, ms);
10
- });
11
- }
package/dist/pre.ts DELETED
@@ -1,7 +0,0 @@
1
- import * as electron from 'electron';
2
-
3
- electron.contextBridge.exposeInMainWorld('clickgoNative', {
4
- invoke: function(name: string, ...param: any[]): Promise<void> {
5
- return electron.ipcRenderer.invoke('pre', name, ...param);
6
- }
7
- });