@iflyrpa/playwright 1.0.9 → 1.0.10

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/dist/index.cjs CHANGED
@@ -3,7 +3,7 @@
3
3
  const path = require('node:path');
4
4
  const fs = require('node:fs');
5
5
  const https = require('node:https');
6
- const electron = require('electron');
6
+ const log = require('loglevel');
7
7
  const node_child_process = require('node:child_process');
8
8
 
9
9
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
@@ -11,10 +11,11 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
11
11
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
12
12
  const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
13
13
  const https__default = /*#__PURE__*/_interopDefaultCompat(https);
14
+ const log__default = /*#__PURE__*/_interopDefaultCompat(log);
14
15
 
15
16
  const name = "@iflyrpa/playwright";
16
17
  const type = "module";
17
- const version$1 = "1.0.9";
18
+ const version$1 = "1.0.10";
18
19
  const description = "";
19
20
  const main = "./dist/index.cjs";
20
21
  const module$1 = "./dist/index.mjs";
@@ -32,6 +33,7 @@ const files = [
32
33
  "dist"
33
34
  ];
34
35
  const dependencies = {
36
+ loglevel: "^1.9.2",
35
37
  playwright: "^1.46.1"
36
38
  };
37
39
  const peerDependencies = {
@@ -336,6 +338,65 @@ const xiaohongshuPublishAction = async (props) => {
336
338
  return response;
337
339
  };
338
340
 
341
+ var __defProp$2 = Object.defineProperty;
342
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
343
+ var __publicField$2 = (obj, key, value) => {
344
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
345
+ return value;
346
+ };
347
+ const _Logger = class _Logger {
348
+ constructor(cachePath) {
349
+ __publicField$2(this, "stream");
350
+ if (_Logger.instance) {
351
+ return _Logger.instance;
352
+ }
353
+ const logFile = path__default.join(cachePath, "rpa.log");
354
+ this.stream = fs__default.createWriteStream(logFile, { flags: "a" });
355
+ log__default.setLevel("debug");
356
+ log__default.methodFactory = (methodName) => {
357
+ return (message) => {
358
+ this.stream.write(
359
+ `[${( new Date()).toISOString()}] ${methodName.toUpperCase()}: ${message}
360
+ `
361
+ );
362
+ };
363
+ };
364
+ log__default.setLevel(log__default.getLevel());
365
+ _Logger.instance = this;
366
+ }
367
+ static getInstance(cachePath) {
368
+ if (!_Logger.instance) {
369
+ _Logger.instance = new _Logger(cachePath);
370
+ }
371
+ return _Logger.instance;
372
+ }
373
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
374
+ debug(...msg) {
375
+ log__default.debug(...msg);
376
+ }
377
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
378
+ info(...msg) {
379
+ log__default.info(...msg);
380
+ }
381
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
382
+ warn(...msg) {
383
+ log__default.warn(...msg);
384
+ }
385
+ error(prefix, error) {
386
+ let errorMessage = error;
387
+ if (error instanceof Error) {
388
+ errorMessage = `${error.message}
389
+ Error stack: ${error.stack}`;
390
+ }
391
+ log__default.error(prefix, errorMessage);
392
+ }
393
+ close() {
394
+ this.stream.end();
395
+ }
396
+ };
397
+ __publicField$2(_Logger, "instance", null);
398
+ let Logger = _Logger;
399
+
339
400
  const template = `
340
401
  const { app } = require("electron");
341
402
 
@@ -353,14 +414,27 @@ const generateFile = async (dir) => {
353
414
  return filePath;
354
415
  };
355
416
  const createElectronApp = async (cachePath, playwrightPackage) => {
356
- const playwright = await playwrightPackage;
357
- const mainPath = await generateFile(cachePath);
358
- const electronApp = await playwright._electron.launch({
359
- executablePath: electron.app.getPath("exe"),
360
- // 获取 Electron 可执行文件的路径
361
- args: [mainPath]
362
- });
363
- return electronApp;
417
+ const logger = Logger.getInstance(cachePath);
418
+ try {
419
+ const executablePath = path__default.join(
420
+ cachePath,
421
+ "node_modules",
422
+ ".bin",
423
+ "electron"
424
+ );
425
+ const playwright = await playwrightPackage;
426
+ const mainPath = await generateFile(cachePath);
427
+ const electronApp = await playwright._electron.launch({
428
+ executablePath,
429
+ // 获取 Electron 可执行文件的路径
430
+ args: [mainPath],
431
+ cwd: cachePath
432
+ });
433
+ logger.info(`electron \u542F\u52A8\u6210\u529F\uFF1A${executablePath} ${mainPath}`);
434
+ return electronApp;
435
+ } catch (error) {
436
+ logger.error("electron \u542F\u52A8\u5931\u8D25\uFF1A", error);
437
+ }
364
438
  };
365
439
 
366
440
  var __defProp$1 = Object.defineProperty;
@@ -376,8 +450,11 @@ class PackageManager {
376
450
  __publicField$1(this, "forceUpdate");
377
451
  // 是否强制更新
378
452
  __publicField$1(this, "initPromise");
453
+ __publicField$1(this, "logger");
454
+ this.logger = Logger.getInstance(params.cacheDir);
379
455
  this.cacheDir = params.cacheDir;
380
456
  this.forceUpdate = params.forceUpdate || true;
457
+ this.initCacheProject();
381
458
  this.initPromise = this.init(params.packageName, params.packageVersion);
382
459
  }
383
460
  // 在子线程执行 npm 命令
@@ -385,7 +462,8 @@ class PackageManager {
385
462
  return new Promise((resolve) => {
386
463
  const args = [cmd].concat(modules).concat("--color=always").concat("--save");
387
464
  try {
388
- const npm = node_child_process.spawn("npm", args, { cwd: where });
465
+ const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
466
+ const npm = node_child_process.spawn(npmCmd, args, { cwd: where });
389
467
  let output = "";
390
468
  npm.stdout?.on("data", (data) => {
391
469
  output += data;
@@ -405,6 +483,7 @@ class PackageManager {
405
483
  console.error(err);
406
484
  });
407
485
  } catch (error) {
486
+ this.logger.error(`npm ${args.join(" ")}: `, error);
408
487
  console.error(error);
409
488
  }
410
489
  });
@@ -413,10 +492,12 @@ class PackageManager {
413
492
  initCacheProject() {
414
493
  const packagePath = path__default.join(this.cacheDir, "package.json");
415
494
  if (!fs__default.existsSync(packagePath)) {
495
+ this.logger.info("package.json \u4E0D\u5B58\u5728\uFF0C\u6DFB\u52A0\u8BE5\u6587\u4EF6");
416
496
  const pkg = {
417
497
  name: "rpa-plugins",
418
498
  description: "rpa-plugins",
419
- license: "MIT"
499
+ license: "MIT",
500
+ main: "./src/main.js"
420
501
  };
421
502
  ensureFileSync(packagePath);
422
503
  fs__default.writeFileSync(packagePath, JSON.stringify(pkg), "utf8");
@@ -436,6 +517,7 @@ class PackageManager {
436
517
  try {
437
518
  return require(path__default.join(pluginDir, module));
438
519
  } catch (error) {
520
+ this.logger.warn(`${module}\u672C\u5730\u4F9D\u8D56\u4E0D\u5B58\u5728\uFF0C${pluginDir}`);
439
521
  return null;
440
522
  }
441
523
  }
@@ -446,14 +528,13 @@ class PackageManager {
446
528
  // 安装依赖
447
529
  install(module, version) {
448
530
  const moduleName = version ? `${module}@${version}` : module;
449
- return this.execCommand("install", [moduleName]);
531
+ return this.execCommand("install", [moduleName, "--save"]);
450
532
  }
451
533
  // 更新依赖
452
534
  update(module) {
453
535
  return this.execCommand("update", [module]);
454
536
  }
455
537
  async init(name, version) {
456
- this.initCacheProject();
457
538
  const plugin = this.getPlugin(path__default.join(name, "package.json"));
458
539
  if (!plugin) {
459
540
  await this.install(name, version);
@@ -466,7 +547,7 @@ class PackageManager {
466
547
  await this.install(name, pkInfo.version);
467
548
  }
468
549
  }
469
- console.log("Package manager init done!");
550
+ this.logger.info(`${name} package manager init done!`);
470
551
  }
471
552
  }
472
553
 
@@ -478,18 +559,16 @@ var __publicField = (obj, key, value) => {
478
559
  };
479
560
  const PLAYWRIGHT_VERSION = "1.46.1";
480
561
  class LocalAutomateTask {
481
- constructor(params) {
562
+ constructor({ cachePath, debug }) {
482
563
  __publicField(this, "cachePath");
483
564
  __publicField(this, "debug");
484
- __publicField(this, "appWhenReady");
485
565
  __publicField(this, "playwrightPackage");
486
- this.cachePath = params.cachePath;
487
- this.debug = params.debug || false;
566
+ __publicField(this, "logger");
567
+ __publicField(this, "_electronApp", null);
568
+ this.cachePath = cachePath;
569
+ this.debug = debug || false;
570
+ this.logger = Logger.getInstance(cachePath);
488
571
  this.playwrightPackage = this.installPlaywright();
489
- this.appWhenReady = createElectronApp(
490
- params.cachePath,
491
- this.playwrightPackage
492
- );
493
572
  }
494
573
  /**
495
574
  * 安装 playwright
@@ -503,19 +582,33 @@ class LocalAutomateTask {
503
582
  });
504
583
  return playwrightPackageManager.getPluginAfterInit("playwright");
505
584
  }
585
+ /**
586
+ * 启动 Electron
587
+ * @returns
588
+ */
589
+ async getElectronApp() {
590
+ if (!this._electronApp) {
591
+ this._electronApp = await createElectronApp(
592
+ this.cachePath,
593
+ this.playwrightPackage
594
+ );
595
+ }
596
+ return this._electronApp;
597
+ }
506
598
  /**
507
599
  * 关闭 playwright 启动的 electron 客户端
508
600
  * @returns
509
601
  */
510
602
  async close() {
511
- const electronApp = await this.appWhenReady;
603
+ this.logger.close();
604
+ const electronApp = await this.getElectronApp();
512
605
  return electronApp.close();
513
606
  }
514
607
  /**
515
608
  * 小红书自动化发布
516
609
  */
517
610
  async xiaohongshuPublish(params) {
518
- const electronApp = await this.appWhenReady;
611
+ const electronApp = await this.getElectronApp();
519
612
  const commonCookies = {
520
613
  path: "/",
521
614
  sameSite: "lax",
@@ -555,22 +648,29 @@ class LocalAutomateTask {
555
648
  { pageParams }
556
649
  )
557
650
  ]);
558
- const res = await xiaohongshuPublishAction({
559
- page,
560
- params,
561
- cachePath: this.cachePath,
562
- debug: !!this.debug
563
- });
564
- return res;
651
+ try {
652
+ const res = await xiaohongshuPublishAction({
653
+ page,
654
+ params,
655
+ cachePath: this.cachePath,
656
+ debug: !!this.debug
657
+ });
658
+ return res;
659
+ } catch (error) {
660
+ this.logger.error("\u5C0F\u7EA2\u4E66\u53D1\u5E03\u5931\u8D25", error);
661
+ return error;
662
+ }
565
663
  }
566
664
  }
567
665
  const RpaTask = (params) => {
666
+ const logger = Logger.getInstance(params.cachePath);
568
667
  const packageManager = new PackageManager({
569
668
  packageName: packageJson.name,
570
669
  cacheDir: params.cachePath
571
670
  });
572
671
  const localPackge = packageManager.getPlugin(packageJson.name);
573
672
  if (localPackge?.LocalAutomateTask && localPackge?.version && semver.gt(localPackge.version, packageJson.version)) {
673
+ logger.info(`\u4F7F\u7528\u8FDC\u7A0B\u7684\u65B0\u7248\u672C\uFF0C\u7248\u672C\u53F7\u4E3A\uFF1A${localPackge.version}`);
574
674
  return new localPackge.LocalAutomateTask(params);
575
675
  }
576
676
  return new LocalAutomateTask(params);
package/dist/index.d.cts CHANGED
@@ -1,6 +1,17 @@
1
- import { ElectronApplication } from 'playwright';
2
1
  import { CookiesSetDetails } from 'electron';
3
2
 
3
+ declare class Logger {
4
+ static instance: Logger | null;
5
+ private stream;
6
+ constructor(cachePath: string);
7
+ static getInstance(cachePath: string): Logger;
8
+ debug(...msg: any[]): void;
9
+ info(...msg: any[]): void;
10
+ warn(...msg: any[]): void;
11
+ error(prefix: string, error: unknown): void;
12
+ close(): void;
13
+ }
14
+
4
15
  type CookieMap = CookiesSetDetails[];
5
16
 
6
17
  interface FictionalRendition {
@@ -46,14 +57,20 @@ interface TaskParams {
46
57
  declare class LocalAutomateTask implements TaskParams {
47
58
  cachePath: string;
48
59
  debug: boolean;
49
- appWhenReady: Promise<ElectronApplication>;
50
60
  playwrightPackage: Promise<unknown>;
51
- constructor(params: TaskParams);
61
+ logger: Logger;
62
+ private _electronApp;
63
+ constructor({ cachePath, debug }: TaskParams);
52
64
  /**
53
65
  * 安装 playwright
54
66
  * @returns
55
67
  */
56
- installPlaywright(): Promise<any>;
68
+ private installPlaywright;
69
+ /**
70
+ * 启动 Electron
71
+ * @returns
72
+ */
73
+ private getElectronApp;
57
74
  /**
58
75
  * 关闭 playwright 启动的 electron 客户端
59
76
  * @returns
@@ -62,7 +79,7 @@ declare class LocalAutomateTask implements TaskParams {
62
79
  /**
63
80
  * 小红书自动化发布
64
81
  */
65
- xiaohongshuPublish(params: XiaohonshuPublishParams): Promise<string>;
82
+ xiaohongshuPublish(params: XiaohonshuPublishParams): Promise<unknown>;
66
83
  }
67
84
  declare const RpaTask: (params: TaskParams) => LocalAutomateTask;
68
85
 
package/dist/index.d.mts CHANGED
@@ -1,6 +1,17 @@
1
- import { ElectronApplication } from 'playwright';
2
1
  import { CookiesSetDetails } from 'electron';
3
2
 
3
+ declare class Logger {
4
+ static instance: Logger | null;
5
+ private stream;
6
+ constructor(cachePath: string);
7
+ static getInstance(cachePath: string): Logger;
8
+ debug(...msg: any[]): void;
9
+ info(...msg: any[]): void;
10
+ warn(...msg: any[]): void;
11
+ error(prefix: string, error: unknown): void;
12
+ close(): void;
13
+ }
14
+
4
15
  type CookieMap = CookiesSetDetails[];
5
16
 
6
17
  interface FictionalRendition {
@@ -46,14 +57,20 @@ interface TaskParams {
46
57
  declare class LocalAutomateTask implements TaskParams {
47
58
  cachePath: string;
48
59
  debug: boolean;
49
- appWhenReady: Promise<ElectronApplication>;
50
60
  playwrightPackage: Promise<unknown>;
51
- constructor(params: TaskParams);
61
+ logger: Logger;
62
+ private _electronApp;
63
+ constructor({ cachePath, debug }: TaskParams);
52
64
  /**
53
65
  * 安装 playwright
54
66
  * @returns
55
67
  */
56
- installPlaywright(): Promise<any>;
68
+ private installPlaywright;
69
+ /**
70
+ * 启动 Electron
71
+ * @returns
72
+ */
73
+ private getElectronApp;
57
74
  /**
58
75
  * 关闭 playwright 启动的 electron 客户端
59
76
  * @returns
@@ -62,7 +79,7 @@ declare class LocalAutomateTask implements TaskParams {
62
79
  /**
63
80
  * 小红书自动化发布
64
81
  */
65
- xiaohongshuPublish(params: XiaohonshuPublishParams): Promise<string>;
82
+ xiaohongshuPublish(params: XiaohonshuPublishParams): Promise<unknown>;
66
83
  }
67
84
  declare const RpaTask: (params: TaskParams) => LocalAutomateTask;
68
85
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,17 @@
1
- import { ElectronApplication } from 'playwright';
2
1
  import { CookiesSetDetails } from 'electron';
3
2
 
3
+ declare class Logger {
4
+ static instance: Logger | null;
5
+ private stream;
6
+ constructor(cachePath: string);
7
+ static getInstance(cachePath: string): Logger;
8
+ debug(...msg: any[]): void;
9
+ info(...msg: any[]): void;
10
+ warn(...msg: any[]): void;
11
+ error(prefix: string, error: unknown): void;
12
+ close(): void;
13
+ }
14
+
4
15
  type CookieMap = CookiesSetDetails[];
5
16
 
6
17
  interface FictionalRendition {
@@ -46,14 +57,20 @@ interface TaskParams {
46
57
  declare class LocalAutomateTask implements TaskParams {
47
58
  cachePath: string;
48
59
  debug: boolean;
49
- appWhenReady: Promise<ElectronApplication>;
50
60
  playwrightPackage: Promise<unknown>;
51
- constructor(params: TaskParams);
61
+ logger: Logger;
62
+ private _electronApp;
63
+ constructor({ cachePath, debug }: TaskParams);
52
64
  /**
53
65
  * 安装 playwright
54
66
  * @returns
55
67
  */
56
- installPlaywright(): Promise<any>;
68
+ private installPlaywright;
69
+ /**
70
+ * 启动 Electron
71
+ * @returns
72
+ */
73
+ private getElectronApp;
57
74
  /**
58
75
  * 关闭 playwright 启动的 electron 客户端
59
76
  * @returns
@@ -62,7 +79,7 @@ declare class LocalAutomateTask implements TaskParams {
62
79
  /**
63
80
  * 小红书自动化发布
64
81
  */
65
- xiaohongshuPublish(params: XiaohonshuPublishParams): Promise<string>;
82
+ xiaohongshuPublish(params: XiaohonshuPublishParams): Promise<unknown>;
66
83
  }
67
84
  declare const RpaTask: (params: TaskParams) => LocalAutomateTask;
68
85
 
package/dist/index.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  import path from 'node:path';
2
2
  import fs from 'node:fs';
3
3
  import https from 'node:https';
4
- import { app } from 'electron';
4
+ import log from 'loglevel';
5
5
  import { spawn } from 'node:child_process';
6
6
 
7
7
  const name = "@iflyrpa/playwright";
8
8
  const type = "module";
9
- const version$1 = "1.0.9";
9
+ const version$1 = "1.0.10";
10
10
  const description = "";
11
11
  const main = "./dist/index.cjs";
12
12
  const module = "./dist/index.mjs";
@@ -24,6 +24,7 @@ const files = [
24
24
  "dist"
25
25
  ];
26
26
  const dependencies = {
27
+ loglevel: "^1.9.2",
27
28
  playwright: "^1.46.1"
28
29
  };
29
30
  const peerDependencies = {
@@ -328,6 +329,65 @@ const xiaohongshuPublishAction = async (props) => {
328
329
  return response;
329
330
  };
330
331
 
332
+ var __defProp$2 = Object.defineProperty;
333
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
334
+ var __publicField$2 = (obj, key, value) => {
335
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
336
+ return value;
337
+ };
338
+ const _Logger = class _Logger {
339
+ constructor(cachePath) {
340
+ __publicField$2(this, "stream");
341
+ if (_Logger.instance) {
342
+ return _Logger.instance;
343
+ }
344
+ const logFile = path.join(cachePath, "rpa.log");
345
+ this.stream = fs.createWriteStream(logFile, { flags: "a" });
346
+ log.setLevel("debug");
347
+ log.methodFactory = (methodName) => {
348
+ return (message) => {
349
+ this.stream.write(
350
+ `[${( new Date()).toISOString()}] ${methodName.toUpperCase()}: ${message}
351
+ `
352
+ );
353
+ };
354
+ };
355
+ log.setLevel(log.getLevel());
356
+ _Logger.instance = this;
357
+ }
358
+ static getInstance(cachePath) {
359
+ if (!_Logger.instance) {
360
+ _Logger.instance = new _Logger(cachePath);
361
+ }
362
+ return _Logger.instance;
363
+ }
364
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
365
+ debug(...msg) {
366
+ log.debug(...msg);
367
+ }
368
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
369
+ info(...msg) {
370
+ log.info(...msg);
371
+ }
372
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
373
+ warn(...msg) {
374
+ log.warn(...msg);
375
+ }
376
+ error(prefix, error) {
377
+ let errorMessage = error;
378
+ if (error instanceof Error) {
379
+ errorMessage = `${error.message}
380
+ Error stack: ${error.stack}`;
381
+ }
382
+ log.error(prefix, errorMessage);
383
+ }
384
+ close() {
385
+ this.stream.end();
386
+ }
387
+ };
388
+ __publicField$2(_Logger, "instance", null);
389
+ let Logger = _Logger;
390
+
331
391
  const template = `
332
392
  const { app } = require("electron");
333
393
 
@@ -345,14 +405,27 @@ const generateFile = async (dir) => {
345
405
  return filePath;
346
406
  };
347
407
  const createElectronApp = async (cachePath, playwrightPackage) => {
348
- const playwright = await playwrightPackage;
349
- const mainPath = await generateFile(cachePath);
350
- const electronApp = await playwright._electron.launch({
351
- executablePath: app.getPath("exe"),
352
- // 获取 Electron 可执行文件的路径
353
- args: [mainPath]
354
- });
355
- return electronApp;
408
+ const logger = Logger.getInstance(cachePath);
409
+ try {
410
+ const executablePath = path.join(
411
+ cachePath,
412
+ "node_modules",
413
+ ".bin",
414
+ "electron"
415
+ );
416
+ const playwright = await playwrightPackage;
417
+ const mainPath = await generateFile(cachePath);
418
+ const electronApp = await playwright._electron.launch({
419
+ executablePath,
420
+ // 获取 Electron 可执行文件的路径
421
+ args: [mainPath],
422
+ cwd: cachePath
423
+ });
424
+ logger.info(`electron \u542F\u52A8\u6210\u529F\uFF1A${executablePath} ${mainPath}`);
425
+ return electronApp;
426
+ } catch (error) {
427
+ logger.error("electron \u542F\u52A8\u5931\u8D25\uFF1A", error);
428
+ }
356
429
  };
357
430
 
358
431
  var __defProp$1 = Object.defineProperty;
@@ -368,8 +441,11 @@ class PackageManager {
368
441
  __publicField$1(this, "forceUpdate");
369
442
  // 是否强制更新
370
443
  __publicField$1(this, "initPromise");
444
+ __publicField$1(this, "logger");
445
+ this.logger = Logger.getInstance(params.cacheDir);
371
446
  this.cacheDir = params.cacheDir;
372
447
  this.forceUpdate = params.forceUpdate || true;
448
+ this.initCacheProject();
373
449
  this.initPromise = this.init(params.packageName, params.packageVersion);
374
450
  }
375
451
  // 在子线程执行 npm 命令
@@ -377,7 +453,8 @@ class PackageManager {
377
453
  return new Promise((resolve) => {
378
454
  const args = [cmd].concat(modules).concat("--color=always").concat("--save");
379
455
  try {
380
- const npm = spawn("npm", args, { cwd: where });
456
+ const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
457
+ const npm = spawn(npmCmd, args, { cwd: where });
381
458
  let output = "";
382
459
  npm.stdout?.on("data", (data) => {
383
460
  output += data;
@@ -397,6 +474,7 @@ class PackageManager {
397
474
  console.error(err);
398
475
  });
399
476
  } catch (error) {
477
+ this.logger.error(`npm ${args.join(" ")}: `, error);
400
478
  console.error(error);
401
479
  }
402
480
  });
@@ -405,10 +483,12 @@ class PackageManager {
405
483
  initCacheProject() {
406
484
  const packagePath = path.join(this.cacheDir, "package.json");
407
485
  if (!fs.existsSync(packagePath)) {
486
+ this.logger.info("package.json \u4E0D\u5B58\u5728\uFF0C\u6DFB\u52A0\u8BE5\u6587\u4EF6");
408
487
  const pkg = {
409
488
  name: "rpa-plugins",
410
489
  description: "rpa-plugins",
411
- license: "MIT"
490
+ license: "MIT",
491
+ main: "./src/main.js"
412
492
  };
413
493
  ensureFileSync(packagePath);
414
494
  fs.writeFileSync(packagePath, JSON.stringify(pkg), "utf8");
@@ -428,6 +508,7 @@ class PackageManager {
428
508
  try {
429
509
  return require(path.join(pluginDir, module));
430
510
  } catch (error) {
511
+ this.logger.warn(`${module}\u672C\u5730\u4F9D\u8D56\u4E0D\u5B58\u5728\uFF0C${pluginDir}`);
431
512
  return null;
432
513
  }
433
514
  }
@@ -438,14 +519,13 @@ class PackageManager {
438
519
  // 安装依赖
439
520
  install(module, version) {
440
521
  const moduleName = version ? `${module}@${version}` : module;
441
- return this.execCommand("install", [moduleName]);
522
+ return this.execCommand("install", [moduleName, "--save"]);
442
523
  }
443
524
  // 更新依赖
444
525
  update(module) {
445
526
  return this.execCommand("update", [module]);
446
527
  }
447
528
  async init(name, version) {
448
- this.initCacheProject();
449
529
  const plugin = this.getPlugin(path.join(name, "package.json"));
450
530
  if (!plugin) {
451
531
  await this.install(name, version);
@@ -458,7 +538,7 @@ class PackageManager {
458
538
  await this.install(name, pkInfo.version);
459
539
  }
460
540
  }
461
- console.log("Package manager init done!");
541
+ this.logger.info(`${name} package manager init done!`);
462
542
  }
463
543
  }
464
544
 
@@ -470,18 +550,16 @@ var __publicField = (obj, key, value) => {
470
550
  };
471
551
  const PLAYWRIGHT_VERSION = "1.46.1";
472
552
  class LocalAutomateTask {
473
- constructor(params) {
553
+ constructor({ cachePath, debug }) {
474
554
  __publicField(this, "cachePath");
475
555
  __publicField(this, "debug");
476
- __publicField(this, "appWhenReady");
477
556
  __publicField(this, "playwrightPackage");
478
- this.cachePath = params.cachePath;
479
- this.debug = params.debug || false;
557
+ __publicField(this, "logger");
558
+ __publicField(this, "_electronApp", null);
559
+ this.cachePath = cachePath;
560
+ this.debug = debug || false;
561
+ this.logger = Logger.getInstance(cachePath);
480
562
  this.playwrightPackage = this.installPlaywright();
481
- this.appWhenReady = createElectronApp(
482
- params.cachePath,
483
- this.playwrightPackage
484
- );
485
563
  }
486
564
  /**
487
565
  * 安装 playwright
@@ -495,19 +573,33 @@ class LocalAutomateTask {
495
573
  });
496
574
  return playwrightPackageManager.getPluginAfterInit("playwright");
497
575
  }
576
+ /**
577
+ * 启动 Electron
578
+ * @returns
579
+ */
580
+ async getElectronApp() {
581
+ if (!this._electronApp) {
582
+ this._electronApp = await createElectronApp(
583
+ this.cachePath,
584
+ this.playwrightPackage
585
+ );
586
+ }
587
+ return this._electronApp;
588
+ }
498
589
  /**
499
590
  * 关闭 playwright 启动的 electron 客户端
500
591
  * @returns
501
592
  */
502
593
  async close() {
503
- const electronApp = await this.appWhenReady;
594
+ this.logger.close();
595
+ const electronApp = await this.getElectronApp();
504
596
  return electronApp.close();
505
597
  }
506
598
  /**
507
599
  * 小红书自动化发布
508
600
  */
509
601
  async xiaohongshuPublish(params) {
510
- const electronApp = await this.appWhenReady;
602
+ const electronApp = await this.getElectronApp();
511
603
  const commonCookies = {
512
604
  path: "/",
513
605
  sameSite: "lax",
@@ -547,22 +639,29 @@ class LocalAutomateTask {
547
639
  { pageParams }
548
640
  )
549
641
  ]);
550
- const res = await xiaohongshuPublishAction({
551
- page,
552
- params,
553
- cachePath: this.cachePath,
554
- debug: !!this.debug
555
- });
556
- return res;
642
+ try {
643
+ const res = await xiaohongshuPublishAction({
644
+ page,
645
+ params,
646
+ cachePath: this.cachePath,
647
+ debug: !!this.debug
648
+ });
649
+ return res;
650
+ } catch (error) {
651
+ this.logger.error("\u5C0F\u7EA2\u4E66\u53D1\u5E03\u5931\u8D25", error);
652
+ return error;
653
+ }
557
654
  }
558
655
  }
559
656
  const RpaTask = (params) => {
657
+ const logger = Logger.getInstance(params.cachePath);
560
658
  const packageManager = new PackageManager({
561
659
  packageName: packageJson.name,
562
660
  cacheDir: params.cachePath
563
661
  });
564
662
  const localPackge = packageManager.getPlugin(packageJson.name);
565
663
  if (localPackge?.LocalAutomateTask && localPackge?.version && semver.gt(localPackge.version, packageJson.version)) {
664
+ logger.info(`\u4F7F\u7528\u8FDC\u7A0B\u7684\u65B0\u7248\u672C\uFF0C\u7248\u672C\u53F7\u4E3A\uFF1A${localPackge.version}`);
566
665
  return new localPackge.LocalAutomateTask(params);
567
666
  }
568
667
  return new LocalAutomateTask(params);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@iflyrpa/playwright",
3
3
  "type": "module",
4
- "version": "1.0.9",
4
+ "version": "1.0.10",
5
5
  "description": "",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.mjs",
@@ -16,6 +16,7 @@
16
16
  "license": "ISC",
17
17
  "files": ["dist"],
18
18
  "dependencies": {
19
+ "loglevel": "^1.9.2",
19
20
  "playwright": "^1.46.1"
20
21
  },
21
22
  "peerDependencies": {