@iflyrpa/playwright 1.0.14 → 1.0.15
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 +158 -49
- package/dist/index.d.cts +4 -1
- package/dist/index.d.mts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.mjs +155 -48
- package/package.json +10 -5
package/dist/index.cjs
CHANGED
|
@@ -3,20 +3,26 @@
|
|
|
3
3
|
const fs = require('node:fs');
|
|
4
4
|
const path = require('node:path');
|
|
5
5
|
const https = require('node:https');
|
|
6
|
+
const childProcess = require('node:child_process');
|
|
7
|
+
const os = require('node:os');
|
|
8
|
+
const get = require('@electron/get');
|
|
9
|
+
const extract = require('extract-zip');
|
|
6
10
|
const log = require('loglevel');
|
|
7
|
-
const
|
|
11
|
+
const livePluginManager = require('live-plugin-manager');
|
|
8
12
|
|
|
9
13
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
10
14
|
|
|
11
15
|
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
12
16
|
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
13
17
|
const https__default = /*#__PURE__*/_interopDefaultCompat(https);
|
|
18
|
+
const childProcess__default = /*#__PURE__*/_interopDefaultCompat(childProcess);
|
|
19
|
+
const os__default = /*#__PURE__*/_interopDefaultCompat(os);
|
|
20
|
+
const extract__default = /*#__PURE__*/_interopDefaultCompat(extract);
|
|
14
21
|
const log__default = /*#__PURE__*/_interopDefaultCompat(log);
|
|
15
|
-
const npminstall__default = /*#__PURE__*/_interopDefaultCompat(npminstall);
|
|
16
22
|
|
|
17
23
|
const name = "@iflyrpa/playwright";
|
|
18
24
|
const type = "module";
|
|
19
|
-
const version$1 = "1.0.
|
|
25
|
+
const version$1 = "1.0.15";
|
|
20
26
|
const main = "./dist/index.cjs";
|
|
21
27
|
const module$1 = "./dist/index.mjs";
|
|
22
28
|
const types = "./dist/index.d.ts";
|
|
@@ -31,14 +37,19 @@ const files = [
|
|
|
31
37
|
"dist"
|
|
32
38
|
];
|
|
33
39
|
const dependencies = {
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
"@electron/get": "^2.0.0",
|
|
41
|
+
"extract-zip": "^2.0.1",
|
|
42
|
+
"live-plugin-manager": "^1.0.0",
|
|
43
|
+
loglevel: "^1.9.2"
|
|
44
|
+
};
|
|
45
|
+
const peerDependencies = {
|
|
46
|
+
playwright: "^1.46.1"
|
|
36
47
|
};
|
|
37
48
|
const devDependencies = {
|
|
38
49
|
esno: "^4.7.0",
|
|
50
|
+
playwright: "^1.46.1",
|
|
39
51
|
typescript: "^5.5.2",
|
|
40
|
-
unbuild: "^2.0.0"
|
|
41
|
-
playwright: "^1.46.1"
|
|
52
|
+
unbuild: "^2.0.0"
|
|
42
53
|
};
|
|
43
54
|
const packageJson = {
|
|
44
55
|
name: name,
|
|
@@ -52,6 +63,7 @@ const packageJson = {
|
|
|
52
63
|
license: license,
|
|
53
64
|
files: files,
|
|
54
65
|
dependencies: dependencies,
|
|
66
|
+
peerDependencies: peerDependencies,
|
|
55
67
|
devDependencies: devDependencies
|
|
56
68
|
};
|
|
57
69
|
|
|
@@ -336,6 +348,92 @@ const xiaohongshuPublishAction = async (props) => {
|
|
|
336
348
|
return response;
|
|
337
349
|
};
|
|
338
350
|
|
|
351
|
+
var __defProp$3 = Object.defineProperty;
|
|
352
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
353
|
+
var __publicField$3 = (obj, key, value) => {
|
|
354
|
+
__defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
355
|
+
return value;
|
|
356
|
+
};
|
|
357
|
+
class ElectronInstall {
|
|
358
|
+
constructor(rootDir, version, logger) {
|
|
359
|
+
this.rootDir = rootDir;
|
|
360
|
+
this.version = version;
|
|
361
|
+
this.logger = logger;
|
|
362
|
+
__publicField$3(this, "electronPath");
|
|
363
|
+
__publicField$3(this, "platformPath");
|
|
364
|
+
this.platformPath = this.getPlatformPath();
|
|
365
|
+
this.electronPath = path__default.join(rootDir, this.platformPath);
|
|
366
|
+
}
|
|
367
|
+
isInstalled() {
|
|
368
|
+
try {
|
|
369
|
+
if (fs__default.readFileSync(path__default.join(this.rootDir, "version"), "utf-8").replace(/^v/, "") !== this.version) {
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
} catch (ignored) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path__default.join(this.rootDir, this.platformPath);
|
|
376
|
+
return fs__default.existsSync(electronPath);
|
|
377
|
+
}
|
|
378
|
+
getPlatformPath() {
|
|
379
|
+
const platform = process.env.npm_config_platform || os__default.platform();
|
|
380
|
+
switch (platform) {
|
|
381
|
+
case "mas":
|
|
382
|
+
case "darwin":
|
|
383
|
+
return "Electron.app/Contents/MacOS/Electron";
|
|
384
|
+
case "freebsd":
|
|
385
|
+
case "openbsd":
|
|
386
|
+
case "linux":
|
|
387
|
+
return "electron";
|
|
388
|
+
case "win32":
|
|
389
|
+
return "electron.exe";
|
|
390
|
+
default:
|
|
391
|
+
throw new Error(
|
|
392
|
+
`Electron builds are not available on platform: ${platform}`
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
async install() {
|
|
397
|
+
const installed = this.isInstalled();
|
|
398
|
+
this.logger.info(`electron@${this.version} \u662F\u5426\u5DF2\u5B89\u88C5\uFF1A${installed}`);
|
|
399
|
+
if (installed)
|
|
400
|
+
return this.electronPath;
|
|
401
|
+
const platform = process.env.npm_config_platform || process.platform;
|
|
402
|
+
let arch = process.env.npm_config_arch || process.arch;
|
|
403
|
+
if (platform === "darwin" && process.platform === "darwin" && arch === "x64" && process.env.npm_config_arch === void 0) {
|
|
404
|
+
try {
|
|
405
|
+
const output = childProcess__default.execSync(
|
|
406
|
+
"sysctl -in sysctl.proc_translated"
|
|
407
|
+
);
|
|
408
|
+
if (output.toString().trim() === "1") {
|
|
409
|
+
arch = "arm64";
|
|
410
|
+
}
|
|
411
|
+
} catch {
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
try {
|
|
415
|
+
this.logger.info(`electron@${this.version} \u5F00\u59CB\u4E0B\u8F7D\u8D44\u6E90`);
|
|
416
|
+
const zipPath = await get.downloadArtifact({
|
|
417
|
+
version: this.version,
|
|
418
|
+
artifactName: "electron",
|
|
419
|
+
force: process.env.force_no_cache === "true",
|
|
420
|
+
cacheRoot: process.env.electron_config_cache,
|
|
421
|
+
checksums: void 0,
|
|
422
|
+
platform,
|
|
423
|
+
arch
|
|
424
|
+
});
|
|
425
|
+
this.logger.info(`electron@${this.version} \u5F00\u59CB\u89E3\u538B\u8D44\u6E90`);
|
|
426
|
+
process.noAsar = true;
|
|
427
|
+
await extract__default(zipPath, { dir: this.rootDir });
|
|
428
|
+
this.logger.info(`electron@${this.version} \u4E0B\u8F7D\u6210\u529F`);
|
|
429
|
+
return this.electronPath;
|
|
430
|
+
} catch (error) {
|
|
431
|
+
this.logger.error(`electron@${this.version} \u4E0B\u8F7D\u5931\u8D25`, error);
|
|
432
|
+
throw error;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
339
437
|
var __defProp$2 = Object.defineProperty;
|
|
340
438
|
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
341
439
|
var __publicField$2 = (obj, key, value) => {
|
|
@@ -410,23 +508,17 @@ const generateFile = async (dir) => {
|
|
|
410
508
|
}
|
|
411
509
|
return filePath;
|
|
412
510
|
};
|
|
413
|
-
const
|
|
511
|
+
const launchElectronApp = async (cachePath, playwright, electronPath) => {
|
|
414
512
|
const logger = Logger.getInstance(cachePath);
|
|
415
513
|
try {
|
|
416
|
-
const executablePath = path__default.join(
|
|
417
|
-
cachePath,
|
|
418
|
-
"node_modules",
|
|
419
|
-
".bin",
|
|
420
|
-
"electron"
|
|
421
|
-
);
|
|
422
514
|
const mainPath = await generateFile(cachePath);
|
|
423
515
|
const electronApp = await playwright._electron.launch({
|
|
424
|
-
executablePath,
|
|
516
|
+
executablePath: electronPath,
|
|
425
517
|
// 获取 Electron 可执行文件的路径
|
|
426
518
|
args: [mainPath],
|
|
427
519
|
cwd: cachePath
|
|
428
520
|
});
|
|
429
|
-
logger.info(`electron \u542F\u52A8\u6210\u529F\uFF1A${
|
|
521
|
+
logger.info(`electron \u542F\u52A8\u6210\u529F\uFF1A${electronPath} ${mainPath}`);
|
|
430
522
|
return electronApp;
|
|
431
523
|
} catch (error) {
|
|
432
524
|
logger.error("electron \u542F\u52A8\u5931\u8D25\uFF1A", error);
|
|
@@ -441,8 +533,13 @@ var __publicField$1 = (obj, key, value) => {
|
|
|
441
533
|
return value;
|
|
442
534
|
};
|
|
443
535
|
const NPM_REGISTRY = "https://registry.npmmirror.com/";
|
|
444
|
-
class PackageManager {
|
|
536
|
+
class PackageManager extends livePluginManager.PluginManager {
|
|
445
537
|
constructor(params) {
|
|
538
|
+
super({
|
|
539
|
+
cwd: params.cacheDir,
|
|
540
|
+
pluginsPath: path__default.join(params.cacheDir, "node_modules"),
|
|
541
|
+
npmRegistryUrl: NPM_REGISTRY
|
|
542
|
+
});
|
|
446
543
|
__publicField$1(this, "cacheDir");
|
|
447
544
|
// 依赖安装目录
|
|
448
545
|
__publicField$1(this, "forceUpdate");
|
|
@@ -484,7 +581,6 @@ class PackageManager {
|
|
|
484
581
|
try {
|
|
485
582
|
return require(path__default.join(pluginDir, module));
|
|
486
583
|
} catch (error) {
|
|
487
|
-
this.logger.warn(`${module}\u672C\u5730\u4F9D\u8D56\u4E0D\u5B58\u5728\uFF0C${pluginDir}`);
|
|
488
584
|
return null;
|
|
489
585
|
}
|
|
490
586
|
}
|
|
@@ -493,27 +589,29 @@ class PackageManager {
|
|
|
493
589
|
return this.getPlugin(module);
|
|
494
590
|
}
|
|
495
591
|
// 安装依赖
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
})
|
|
592
|
+
async installPackage(name, version) {
|
|
593
|
+
try {
|
|
594
|
+
this.logger.info(`${name} \u5F00\u59CB\u5B89\u88C5`);
|
|
595
|
+
await this.install(name, version);
|
|
596
|
+
this.logger.info(`${name} \u5B89\u88C5\u6210\u529F`);
|
|
597
|
+
} catch (error) {
|
|
598
|
+
this.logger.error(`${name} \u5B89\u88C5\u5931\u8D25`, error);
|
|
599
|
+
}
|
|
502
600
|
}
|
|
503
601
|
async init(name, version) {
|
|
504
602
|
const plugin = this.getPlugin(path__default.join(name, "package.json"));
|
|
505
603
|
this.logger.info(`${name} \u662F\u5426\u5DF2\u5B89\u88C5\uFF1A${!!plugin}`);
|
|
506
604
|
try {
|
|
507
605
|
if (!plugin) {
|
|
508
|
-
await this.
|
|
606
|
+
await this.installPackage(name, version);
|
|
509
607
|
} else if (this.forceUpdate) {
|
|
510
608
|
const pkInfo = await this.getPluginInfo(name);
|
|
511
609
|
if (!pkInfo)
|
|
512
610
|
return;
|
|
513
611
|
const hasNewVersion = semver.gt(pkInfo.version, plugin.version);
|
|
514
612
|
if (hasNewVersion) {
|
|
515
|
-
this.logger.info(`${name} \u68C0\u67E5\u5230\u65B0\u7248\u672C ${pkInfo.version}
|
|
516
|
-
await this.
|
|
613
|
+
this.logger.info(`${name} \u68C0\u67E5\u5230\u65B0\u7248\u672C ${pkInfo.version}`);
|
|
614
|
+
await this.installPackage(name, pkInfo.version);
|
|
517
615
|
}
|
|
518
616
|
}
|
|
519
617
|
this.logger.info(`${name} package manager init done!`);
|
|
@@ -530,6 +628,7 @@ var __publicField = (obj, key, value) => {
|
|
|
530
628
|
return value;
|
|
531
629
|
};
|
|
532
630
|
const PLAYWRIGHT_VERSION = "1.46.1";
|
|
631
|
+
const PLAYWRIGHT_NAME = "playwright-core";
|
|
533
632
|
const ELECTRON_VERSION = "22.3.27";
|
|
534
633
|
class LocalAutomateTask {
|
|
535
634
|
constructor({ cachePath, debug }) {
|
|
@@ -551,37 +650,40 @@ class LocalAutomateTask {
|
|
|
551
650
|
*/
|
|
552
651
|
installPlaywright() {
|
|
553
652
|
const playwrightPackageManager = new PackageManager({
|
|
554
|
-
packageName:
|
|
653
|
+
packageName: PLAYWRIGHT_NAME,
|
|
555
654
|
packageVersion: PLAYWRIGHT_VERSION,
|
|
556
655
|
cacheDir: this.cachePath,
|
|
557
656
|
forceUpdate: false
|
|
558
657
|
});
|
|
559
|
-
return playwrightPackageManager.getPluginAfterInit(
|
|
658
|
+
return playwrightPackageManager.getPluginAfterInit(PLAYWRIGHT_NAME);
|
|
560
659
|
}
|
|
561
660
|
/**
|
|
562
661
|
* 安装 electron
|
|
563
662
|
* @returns
|
|
564
663
|
*/
|
|
565
664
|
installElectron() {
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
return playwrightPackageManager.getPluginAfterInit("electron");
|
|
665
|
+
const electronInstall = new ElectronInstall(
|
|
666
|
+
path__default.join(this.cachePath, "electron"),
|
|
667
|
+
ELECTRON_VERSION,
|
|
668
|
+
this.logger
|
|
669
|
+
);
|
|
670
|
+
return electronInstall.install();
|
|
573
671
|
}
|
|
574
672
|
/**
|
|
575
673
|
* 启动 Electron
|
|
576
674
|
* @returns
|
|
577
675
|
*/
|
|
578
|
-
async
|
|
676
|
+
async launchApp() {
|
|
579
677
|
if (!this._electronApp) {
|
|
580
|
-
const [playwright] = await Promise.all([
|
|
678
|
+
const [playwright, electronPath] = await Promise.all([
|
|
581
679
|
this.playwrightPackage,
|
|
582
680
|
this.electronPackage
|
|
583
681
|
]);
|
|
584
|
-
this._electronApp = await
|
|
682
|
+
this._electronApp = await launchElectronApp(
|
|
683
|
+
this.cachePath,
|
|
684
|
+
playwright,
|
|
685
|
+
electronPath
|
|
686
|
+
);
|
|
585
687
|
}
|
|
586
688
|
return this._electronApp;
|
|
587
689
|
}
|
|
@@ -612,14 +714,16 @@ class LocalAutomateTask {
|
|
|
612
714
|
async close() {
|
|
613
715
|
this.logger.close();
|
|
614
716
|
this.clearTmpPath();
|
|
615
|
-
const electronApp = await this.
|
|
717
|
+
const electronApp = await this.launchApp();
|
|
616
718
|
return electronApp.close();
|
|
617
719
|
}
|
|
618
720
|
/**
|
|
619
721
|
* 小红书自动化发布
|
|
620
722
|
*/
|
|
621
723
|
async xiaohongshuPublish(params) {
|
|
622
|
-
|
|
724
|
+
this.logger.info("\u5F00\u59CB\u5C0F\u7EA2\u4E66\u53D1\u5E03");
|
|
725
|
+
const electronApp = await this.launchApp();
|
|
726
|
+
this.logger.info("\u5C0F\u7EA2\u4E66\u53D1\u5E03\u4E13\u7528\u5BA2\u6237\u7AEF\u5DF2\u542F\u52A8");
|
|
623
727
|
const commonCookies = {
|
|
624
728
|
path: "/",
|
|
625
729
|
sameSite: "lax",
|
|
@@ -659,6 +763,7 @@ class LocalAutomateTask {
|
|
|
659
763
|
{ pageParams }
|
|
660
764
|
)
|
|
661
765
|
]);
|
|
766
|
+
this.logger.info("\u5DF2\u6253\u5F00\u5C0F\u7EA2\u4E66\u53D1\u5E03\u9875\u9762");
|
|
662
767
|
try {
|
|
663
768
|
const res = await xiaohongshuPublishAction({
|
|
664
769
|
page,
|
|
@@ -666,6 +771,7 @@ class LocalAutomateTask {
|
|
|
666
771
|
tmpCachePath: this.getTmpPath(),
|
|
667
772
|
debug: !!this.debug
|
|
668
773
|
});
|
|
774
|
+
this.logger.info(`\u5C0F\u7EA2\u4E66\u53D1\u5E03\u6210\u529F\uFF1A${res}`);
|
|
669
775
|
return res;
|
|
670
776
|
} catch (error) {
|
|
671
777
|
this.logger.error("\u5C0F\u7EA2\u4E66\u53D1\u5E03\u5931\u8D25", error);
|
|
@@ -675,14 +781,17 @@ class LocalAutomateTask {
|
|
|
675
781
|
}
|
|
676
782
|
const RpaTask = (params) => {
|
|
677
783
|
const logger = Logger.getInstance(params.cachePath);
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
784
|
+
if (params.forceUpdate) {
|
|
785
|
+
logger.info("\u5C1D\u8BD5\u4F7F\u7528\u8FDC\u7A0B\u6700\u65B0\u7248\u672C");
|
|
786
|
+
const packageManager = new PackageManager({
|
|
787
|
+
packageName: packageJson.name,
|
|
788
|
+
cacheDir: params.cachePath
|
|
789
|
+
});
|
|
790
|
+
const localPackge = packageManager.getPlugin(packageJson.name);
|
|
791
|
+
if (localPackge?.LocalAutomateTask && localPackge?.version && semver.gt(localPackge.version, packageJson.version)) {
|
|
792
|
+
logger.info(`\u4F7F\u7528\u8FDC\u7A0B\u7684\u65B0\u7248\u672C\uFF0C\u7248\u672C\u53F7\u4E3A\uFF1A${localPackge.version}`);
|
|
793
|
+
return new localPackge.LocalAutomateTask(params);
|
|
794
|
+
}
|
|
686
795
|
}
|
|
687
796
|
return new LocalAutomateTask(params);
|
|
688
797
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ElectronApplication } from 'playwright';
|
|
2
|
+
|
|
1
3
|
interface CookiesSetDetails {
|
|
2
4
|
/**
|
|
3
5
|
* The URL to associate the cookie with. The promise will be rejected if the URL is
|
|
@@ -84,6 +86,7 @@ interface XiaohonshuPublishParams
|
|
|
84
86
|
interface TaskParams {
|
|
85
87
|
debug?: boolean;
|
|
86
88
|
cachePath: string;
|
|
89
|
+
forceUpdate?: boolean;
|
|
87
90
|
}
|
|
88
91
|
declare class LocalAutomateTask implements TaskParams {
|
|
89
92
|
private logger;
|
|
@@ -107,7 +110,7 @@ declare class LocalAutomateTask implements TaskParams {
|
|
|
107
110
|
* 启动 Electron
|
|
108
111
|
* @returns
|
|
109
112
|
*/
|
|
110
|
-
|
|
113
|
+
launchApp(): Promise<ElectronApplication>;
|
|
111
114
|
/**
|
|
112
115
|
* 临时文件目录
|
|
113
116
|
* @returns
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ElectronApplication } from 'playwright';
|
|
2
|
+
|
|
1
3
|
interface CookiesSetDetails {
|
|
2
4
|
/**
|
|
3
5
|
* The URL to associate the cookie with. The promise will be rejected if the URL is
|
|
@@ -84,6 +86,7 @@ interface XiaohonshuPublishParams
|
|
|
84
86
|
interface TaskParams {
|
|
85
87
|
debug?: boolean;
|
|
86
88
|
cachePath: string;
|
|
89
|
+
forceUpdate?: boolean;
|
|
87
90
|
}
|
|
88
91
|
declare class LocalAutomateTask implements TaskParams {
|
|
89
92
|
private logger;
|
|
@@ -107,7 +110,7 @@ declare class LocalAutomateTask implements TaskParams {
|
|
|
107
110
|
* 启动 Electron
|
|
108
111
|
* @returns
|
|
109
112
|
*/
|
|
110
|
-
|
|
113
|
+
launchApp(): Promise<ElectronApplication>;
|
|
111
114
|
/**
|
|
112
115
|
* 临时文件目录
|
|
113
116
|
* @returns
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ElectronApplication } from 'playwright';
|
|
2
|
+
|
|
1
3
|
interface CookiesSetDetails {
|
|
2
4
|
/**
|
|
3
5
|
* The URL to associate the cookie with. The promise will be rejected if the URL is
|
|
@@ -84,6 +86,7 @@ interface XiaohonshuPublishParams
|
|
|
84
86
|
interface TaskParams {
|
|
85
87
|
debug?: boolean;
|
|
86
88
|
cachePath: string;
|
|
89
|
+
forceUpdate?: boolean;
|
|
87
90
|
}
|
|
88
91
|
declare class LocalAutomateTask implements TaskParams {
|
|
89
92
|
private logger;
|
|
@@ -107,7 +110,7 @@ declare class LocalAutomateTask implements TaskParams {
|
|
|
107
110
|
* 启动 Electron
|
|
108
111
|
* @returns
|
|
109
112
|
*/
|
|
110
|
-
|
|
113
|
+
launchApp(): Promise<ElectronApplication>;
|
|
111
114
|
/**
|
|
112
115
|
* 临时文件目录
|
|
113
116
|
* @returns
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import https from 'node:https';
|
|
4
|
+
import childProcess from 'node:child_process';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import { downloadArtifact } from '@electron/get';
|
|
7
|
+
import extract from 'extract-zip';
|
|
4
8
|
import log from 'loglevel';
|
|
5
|
-
import
|
|
9
|
+
import { PluginManager } from 'live-plugin-manager';
|
|
6
10
|
|
|
7
11
|
const name = "@iflyrpa/playwright";
|
|
8
12
|
const type = "module";
|
|
9
|
-
const version$1 = "1.0.
|
|
13
|
+
const version$1 = "1.0.15";
|
|
10
14
|
const main = "./dist/index.cjs";
|
|
11
15
|
const module = "./dist/index.mjs";
|
|
12
16
|
const types = "./dist/index.d.ts";
|
|
@@ -21,14 +25,19 @@ const files = [
|
|
|
21
25
|
"dist"
|
|
22
26
|
];
|
|
23
27
|
const dependencies = {
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
"@electron/get": "^2.0.0",
|
|
29
|
+
"extract-zip": "^2.0.1",
|
|
30
|
+
"live-plugin-manager": "^1.0.0",
|
|
31
|
+
loglevel: "^1.9.2"
|
|
32
|
+
};
|
|
33
|
+
const peerDependencies = {
|
|
34
|
+
playwright: "^1.46.1"
|
|
26
35
|
};
|
|
27
36
|
const devDependencies = {
|
|
28
37
|
esno: "^4.7.0",
|
|
38
|
+
playwright: "^1.46.1",
|
|
29
39
|
typescript: "^5.5.2",
|
|
30
|
-
unbuild: "^2.0.0"
|
|
31
|
-
playwright: "^1.46.1"
|
|
40
|
+
unbuild: "^2.0.0"
|
|
32
41
|
};
|
|
33
42
|
const packageJson = {
|
|
34
43
|
name: name,
|
|
@@ -42,6 +51,7 @@ const packageJson = {
|
|
|
42
51
|
license: license,
|
|
43
52
|
files: files,
|
|
44
53
|
dependencies: dependencies,
|
|
54
|
+
peerDependencies: peerDependencies,
|
|
45
55
|
devDependencies: devDependencies
|
|
46
56
|
};
|
|
47
57
|
|
|
@@ -326,6 +336,92 @@ const xiaohongshuPublishAction = async (props) => {
|
|
|
326
336
|
return response;
|
|
327
337
|
};
|
|
328
338
|
|
|
339
|
+
var __defProp$3 = Object.defineProperty;
|
|
340
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
341
|
+
var __publicField$3 = (obj, key, value) => {
|
|
342
|
+
__defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
343
|
+
return value;
|
|
344
|
+
};
|
|
345
|
+
class ElectronInstall {
|
|
346
|
+
constructor(rootDir, version, logger) {
|
|
347
|
+
this.rootDir = rootDir;
|
|
348
|
+
this.version = version;
|
|
349
|
+
this.logger = logger;
|
|
350
|
+
__publicField$3(this, "electronPath");
|
|
351
|
+
__publicField$3(this, "platformPath");
|
|
352
|
+
this.platformPath = this.getPlatformPath();
|
|
353
|
+
this.electronPath = path.join(rootDir, this.platformPath);
|
|
354
|
+
}
|
|
355
|
+
isInstalled() {
|
|
356
|
+
try {
|
|
357
|
+
if (fs.readFileSync(path.join(this.rootDir, "version"), "utf-8").replace(/^v/, "") !== this.version) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
} catch (ignored) {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
const electronPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || path.join(this.rootDir, this.platformPath);
|
|
364
|
+
return fs.existsSync(electronPath);
|
|
365
|
+
}
|
|
366
|
+
getPlatformPath() {
|
|
367
|
+
const platform = process.env.npm_config_platform || os.platform();
|
|
368
|
+
switch (platform) {
|
|
369
|
+
case "mas":
|
|
370
|
+
case "darwin":
|
|
371
|
+
return "Electron.app/Contents/MacOS/Electron";
|
|
372
|
+
case "freebsd":
|
|
373
|
+
case "openbsd":
|
|
374
|
+
case "linux":
|
|
375
|
+
return "electron";
|
|
376
|
+
case "win32":
|
|
377
|
+
return "electron.exe";
|
|
378
|
+
default:
|
|
379
|
+
throw new Error(
|
|
380
|
+
`Electron builds are not available on platform: ${platform}`
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
async install() {
|
|
385
|
+
const installed = this.isInstalled();
|
|
386
|
+
this.logger.info(`electron@${this.version} \u662F\u5426\u5DF2\u5B89\u88C5\uFF1A${installed}`);
|
|
387
|
+
if (installed)
|
|
388
|
+
return this.electronPath;
|
|
389
|
+
const platform = process.env.npm_config_platform || process.platform;
|
|
390
|
+
let arch = process.env.npm_config_arch || process.arch;
|
|
391
|
+
if (platform === "darwin" && process.platform === "darwin" && arch === "x64" && process.env.npm_config_arch === void 0) {
|
|
392
|
+
try {
|
|
393
|
+
const output = childProcess.execSync(
|
|
394
|
+
"sysctl -in sysctl.proc_translated"
|
|
395
|
+
);
|
|
396
|
+
if (output.toString().trim() === "1") {
|
|
397
|
+
arch = "arm64";
|
|
398
|
+
}
|
|
399
|
+
} catch {
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
try {
|
|
403
|
+
this.logger.info(`electron@${this.version} \u5F00\u59CB\u4E0B\u8F7D\u8D44\u6E90`);
|
|
404
|
+
const zipPath = await downloadArtifact({
|
|
405
|
+
version: this.version,
|
|
406
|
+
artifactName: "electron",
|
|
407
|
+
force: process.env.force_no_cache === "true",
|
|
408
|
+
cacheRoot: process.env.electron_config_cache,
|
|
409
|
+
checksums: void 0,
|
|
410
|
+
platform,
|
|
411
|
+
arch
|
|
412
|
+
});
|
|
413
|
+
this.logger.info(`electron@${this.version} \u5F00\u59CB\u89E3\u538B\u8D44\u6E90`);
|
|
414
|
+
process.noAsar = true;
|
|
415
|
+
await extract(zipPath, { dir: this.rootDir });
|
|
416
|
+
this.logger.info(`electron@${this.version} \u4E0B\u8F7D\u6210\u529F`);
|
|
417
|
+
return this.electronPath;
|
|
418
|
+
} catch (error) {
|
|
419
|
+
this.logger.error(`electron@${this.version} \u4E0B\u8F7D\u5931\u8D25`, error);
|
|
420
|
+
throw error;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
329
425
|
var __defProp$2 = Object.defineProperty;
|
|
330
426
|
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
331
427
|
var __publicField$2 = (obj, key, value) => {
|
|
@@ -400,23 +496,17 @@ const generateFile = async (dir) => {
|
|
|
400
496
|
}
|
|
401
497
|
return filePath;
|
|
402
498
|
};
|
|
403
|
-
const
|
|
499
|
+
const launchElectronApp = async (cachePath, playwright, electronPath) => {
|
|
404
500
|
const logger = Logger.getInstance(cachePath);
|
|
405
501
|
try {
|
|
406
|
-
const executablePath = path.join(
|
|
407
|
-
cachePath,
|
|
408
|
-
"node_modules",
|
|
409
|
-
".bin",
|
|
410
|
-
"electron"
|
|
411
|
-
);
|
|
412
502
|
const mainPath = await generateFile(cachePath);
|
|
413
503
|
const electronApp = await playwright._electron.launch({
|
|
414
|
-
executablePath,
|
|
504
|
+
executablePath: electronPath,
|
|
415
505
|
// 获取 Electron 可执行文件的路径
|
|
416
506
|
args: [mainPath],
|
|
417
507
|
cwd: cachePath
|
|
418
508
|
});
|
|
419
|
-
logger.info(`electron \u542F\u52A8\u6210\u529F\uFF1A${
|
|
509
|
+
logger.info(`electron \u542F\u52A8\u6210\u529F\uFF1A${electronPath} ${mainPath}`);
|
|
420
510
|
return electronApp;
|
|
421
511
|
} catch (error) {
|
|
422
512
|
logger.error("electron \u542F\u52A8\u5931\u8D25\uFF1A", error);
|
|
@@ -431,8 +521,13 @@ var __publicField$1 = (obj, key, value) => {
|
|
|
431
521
|
return value;
|
|
432
522
|
};
|
|
433
523
|
const NPM_REGISTRY = "https://registry.npmmirror.com/";
|
|
434
|
-
class PackageManager {
|
|
524
|
+
class PackageManager extends PluginManager {
|
|
435
525
|
constructor(params) {
|
|
526
|
+
super({
|
|
527
|
+
cwd: params.cacheDir,
|
|
528
|
+
pluginsPath: path.join(params.cacheDir, "node_modules"),
|
|
529
|
+
npmRegistryUrl: NPM_REGISTRY
|
|
530
|
+
});
|
|
436
531
|
__publicField$1(this, "cacheDir");
|
|
437
532
|
// 依赖安装目录
|
|
438
533
|
__publicField$1(this, "forceUpdate");
|
|
@@ -474,7 +569,6 @@ class PackageManager {
|
|
|
474
569
|
try {
|
|
475
570
|
return require(path.join(pluginDir, module));
|
|
476
571
|
} catch (error) {
|
|
477
|
-
this.logger.warn(`${module}\u672C\u5730\u4F9D\u8D56\u4E0D\u5B58\u5728\uFF0C${pluginDir}`);
|
|
478
572
|
return null;
|
|
479
573
|
}
|
|
480
574
|
}
|
|
@@ -483,27 +577,29 @@ class PackageManager {
|
|
|
483
577
|
return this.getPlugin(module);
|
|
484
578
|
}
|
|
485
579
|
// 安装依赖
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
})
|
|
580
|
+
async installPackage(name, version) {
|
|
581
|
+
try {
|
|
582
|
+
this.logger.info(`${name} \u5F00\u59CB\u5B89\u88C5`);
|
|
583
|
+
await this.install(name, version);
|
|
584
|
+
this.logger.info(`${name} \u5B89\u88C5\u6210\u529F`);
|
|
585
|
+
} catch (error) {
|
|
586
|
+
this.logger.error(`${name} \u5B89\u88C5\u5931\u8D25`, error);
|
|
587
|
+
}
|
|
492
588
|
}
|
|
493
589
|
async init(name, version) {
|
|
494
590
|
const plugin = this.getPlugin(path.join(name, "package.json"));
|
|
495
591
|
this.logger.info(`${name} \u662F\u5426\u5DF2\u5B89\u88C5\uFF1A${!!plugin}`);
|
|
496
592
|
try {
|
|
497
593
|
if (!plugin) {
|
|
498
|
-
await this.
|
|
594
|
+
await this.installPackage(name, version);
|
|
499
595
|
} else if (this.forceUpdate) {
|
|
500
596
|
const pkInfo = await this.getPluginInfo(name);
|
|
501
597
|
if (!pkInfo)
|
|
502
598
|
return;
|
|
503
599
|
const hasNewVersion = semver.gt(pkInfo.version, plugin.version);
|
|
504
600
|
if (hasNewVersion) {
|
|
505
|
-
this.logger.info(`${name} \u68C0\u67E5\u5230\u65B0\u7248\u672C ${pkInfo.version}
|
|
506
|
-
await this.
|
|
601
|
+
this.logger.info(`${name} \u68C0\u67E5\u5230\u65B0\u7248\u672C ${pkInfo.version}`);
|
|
602
|
+
await this.installPackage(name, pkInfo.version);
|
|
507
603
|
}
|
|
508
604
|
}
|
|
509
605
|
this.logger.info(`${name} package manager init done!`);
|
|
@@ -520,6 +616,7 @@ var __publicField = (obj, key, value) => {
|
|
|
520
616
|
return value;
|
|
521
617
|
};
|
|
522
618
|
const PLAYWRIGHT_VERSION = "1.46.1";
|
|
619
|
+
const PLAYWRIGHT_NAME = "playwright-core";
|
|
523
620
|
const ELECTRON_VERSION = "22.3.27";
|
|
524
621
|
class LocalAutomateTask {
|
|
525
622
|
constructor({ cachePath, debug }) {
|
|
@@ -541,37 +638,40 @@ class LocalAutomateTask {
|
|
|
541
638
|
*/
|
|
542
639
|
installPlaywright() {
|
|
543
640
|
const playwrightPackageManager = new PackageManager({
|
|
544
|
-
packageName:
|
|
641
|
+
packageName: PLAYWRIGHT_NAME,
|
|
545
642
|
packageVersion: PLAYWRIGHT_VERSION,
|
|
546
643
|
cacheDir: this.cachePath,
|
|
547
644
|
forceUpdate: false
|
|
548
645
|
});
|
|
549
|
-
return playwrightPackageManager.getPluginAfterInit(
|
|
646
|
+
return playwrightPackageManager.getPluginAfterInit(PLAYWRIGHT_NAME);
|
|
550
647
|
}
|
|
551
648
|
/**
|
|
552
649
|
* 安装 electron
|
|
553
650
|
* @returns
|
|
554
651
|
*/
|
|
555
652
|
installElectron() {
|
|
556
|
-
const
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
return playwrightPackageManager.getPluginAfterInit("electron");
|
|
653
|
+
const electronInstall = new ElectronInstall(
|
|
654
|
+
path.join(this.cachePath, "electron"),
|
|
655
|
+
ELECTRON_VERSION,
|
|
656
|
+
this.logger
|
|
657
|
+
);
|
|
658
|
+
return electronInstall.install();
|
|
563
659
|
}
|
|
564
660
|
/**
|
|
565
661
|
* 启动 Electron
|
|
566
662
|
* @returns
|
|
567
663
|
*/
|
|
568
|
-
async
|
|
664
|
+
async launchApp() {
|
|
569
665
|
if (!this._electronApp) {
|
|
570
|
-
const [playwright] = await Promise.all([
|
|
666
|
+
const [playwright, electronPath] = await Promise.all([
|
|
571
667
|
this.playwrightPackage,
|
|
572
668
|
this.electronPackage
|
|
573
669
|
]);
|
|
574
|
-
this._electronApp = await
|
|
670
|
+
this._electronApp = await launchElectronApp(
|
|
671
|
+
this.cachePath,
|
|
672
|
+
playwright,
|
|
673
|
+
electronPath
|
|
674
|
+
);
|
|
575
675
|
}
|
|
576
676
|
return this._electronApp;
|
|
577
677
|
}
|
|
@@ -602,14 +702,16 @@ class LocalAutomateTask {
|
|
|
602
702
|
async close() {
|
|
603
703
|
this.logger.close();
|
|
604
704
|
this.clearTmpPath();
|
|
605
|
-
const electronApp = await this.
|
|
705
|
+
const electronApp = await this.launchApp();
|
|
606
706
|
return electronApp.close();
|
|
607
707
|
}
|
|
608
708
|
/**
|
|
609
709
|
* 小红书自动化发布
|
|
610
710
|
*/
|
|
611
711
|
async xiaohongshuPublish(params) {
|
|
612
|
-
|
|
712
|
+
this.logger.info("\u5F00\u59CB\u5C0F\u7EA2\u4E66\u53D1\u5E03");
|
|
713
|
+
const electronApp = await this.launchApp();
|
|
714
|
+
this.logger.info("\u5C0F\u7EA2\u4E66\u53D1\u5E03\u4E13\u7528\u5BA2\u6237\u7AEF\u5DF2\u542F\u52A8");
|
|
613
715
|
const commonCookies = {
|
|
614
716
|
path: "/",
|
|
615
717
|
sameSite: "lax",
|
|
@@ -649,6 +751,7 @@ class LocalAutomateTask {
|
|
|
649
751
|
{ pageParams }
|
|
650
752
|
)
|
|
651
753
|
]);
|
|
754
|
+
this.logger.info("\u5DF2\u6253\u5F00\u5C0F\u7EA2\u4E66\u53D1\u5E03\u9875\u9762");
|
|
652
755
|
try {
|
|
653
756
|
const res = await xiaohongshuPublishAction({
|
|
654
757
|
page,
|
|
@@ -656,6 +759,7 @@ class LocalAutomateTask {
|
|
|
656
759
|
tmpCachePath: this.getTmpPath(),
|
|
657
760
|
debug: !!this.debug
|
|
658
761
|
});
|
|
762
|
+
this.logger.info(`\u5C0F\u7EA2\u4E66\u53D1\u5E03\u6210\u529F\uFF1A${res}`);
|
|
659
763
|
return res;
|
|
660
764
|
} catch (error) {
|
|
661
765
|
this.logger.error("\u5C0F\u7EA2\u4E66\u53D1\u5E03\u5931\u8D25", error);
|
|
@@ -665,14 +769,17 @@ class LocalAutomateTask {
|
|
|
665
769
|
}
|
|
666
770
|
const RpaTask = (params) => {
|
|
667
771
|
const logger = Logger.getInstance(params.cachePath);
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
772
|
+
if (params.forceUpdate) {
|
|
773
|
+
logger.info("\u5C1D\u8BD5\u4F7F\u7528\u8FDC\u7A0B\u6700\u65B0\u7248\u672C");
|
|
774
|
+
const packageManager = new PackageManager({
|
|
775
|
+
packageName: packageJson.name,
|
|
776
|
+
cacheDir: params.cachePath
|
|
777
|
+
});
|
|
778
|
+
const localPackge = packageManager.getPlugin(packageJson.name);
|
|
779
|
+
if (localPackge?.LocalAutomateTask && localPackge?.version && semver.gt(localPackge.version, packageJson.version)) {
|
|
780
|
+
logger.info(`\u4F7F\u7528\u8FDC\u7A0B\u7684\u65B0\u7248\u672C\uFF0C\u7248\u672C\u53F7\u4E3A\uFF1A${localPackge.version}`);
|
|
781
|
+
return new localPackge.LocalAutomateTask(params);
|
|
782
|
+
}
|
|
676
783
|
}
|
|
677
784
|
return new LocalAutomateTask(params);
|
|
678
785
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iflyrpa/playwright",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.15",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -14,13 +14,18 @@
|
|
|
14
14
|
"license": "ISC",
|
|
15
15
|
"files": ["dist"],
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"
|
|
18
|
-
"
|
|
17
|
+
"@electron/get": "^2.0.0",
|
|
18
|
+
"extract-zip": "^2.0.1",
|
|
19
|
+
"live-plugin-manager": "^1.0.0",
|
|
20
|
+
"loglevel": "^1.9.2"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"playwright": "^1.46.1"
|
|
19
24
|
},
|
|
20
25
|
"devDependencies": {
|
|
21
26
|
"esno": "^4.7.0",
|
|
27
|
+
"playwright": "^1.46.1",
|
|
22
28
|
"typescript": "^5.5.2",
|
|
23
|
-
"unbuild": "^2.0.0"
|
|
24
|
-
"playwright": "^1.46.1"
|
|
29
|
+
"unbuild": "^2.0.0"
|
|
25
30
|
}
|
|
26
31
|
}
|