@iflyrpa/playwright 1.0.15-beta.1 → 1.0.15-beta.2

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
@@ -1,29 +1,30 @@
1
1
  'use strict';
2
2
 
3
+ const actions = require('@iflyrpa/actions');
3
4
  const fs = require('node:fs');
4
5
  const path = require('node:path');
5
- const https = require('node:https');
6
6
  const childProcess = require('node:child_process');
7
7
  const os = require('node:os');
8
8
  const get = require('@electron/get');
9
9
  const extract = require('extract-zip');
10
+ const share = require('@iflyrpa/share');
10
11
  const log = require('loglevel');
11
12
  const node = require('@sentry/node');
12
- const livePluginManager = require('live-plugin-manager');
13
+ const pacote = require('pacote');
13
14
 
14
15
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
15
16
 
16
17
  const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
17
18
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
18
- const https__default = /*#__PURE__*/_interopDefaultCompat(https);
19
19
  const childProcess__default = /*#__PURE__*/_interopDefaultCompat(childProcess);
20
20
  const os__default = /*#__PURE__*/_interopDefaultCompat(os);
21
21
  const extract__default = /*#__PURE__*/_interopDefaultCompat(extract);
22
22
  const log__default = /*#__PURE__*/_interopDefaultCompat(log);
23
+ const pacote__default = /*#__PURE__*/_interopDefaultCompat(pacote);
23
24
 
24
25
  const name = "@iflyrpa/playwright";
25
26
  const type = "module";
26
- const version$1 = "1.0.15-beta.1";
27
+ const version$1 = "1.0.15-beta.2";
27
28
  const main = "./dist/index.cjs";
28
29
  const module$1 = "./dist/index.mjs";
29
30
  const types = "./dist/index.d.ts";
@@ -37,22 +38,26 @@ const license = "ISC";
37
38
  const files = [
38
39
  "dist"
39
40
  ];
40
- const dependencies = {
41
- "@electron/get": "^2.0.0",
42
- "@sentry/node": "^5.5.0",
43
- "extract-zip": "^2.0.1",
44
- "live-plugin-manager": "^1.0.0",
45
- loglevel: "^1.9.2"
46
- };
47
41
  const peerDependencies = {
48
42
  playwright: "^1.46.1"
49
43
  };
50
44
  const devDependencies = {
45
+ "@iflyrpa/share": "workspace:*",
46
+ "@types/pacote": "^11.1.8",
51
47
  esno: "^4.7.0",
52
48
  playwright: "^1.46.1",
53
49
  typescript: "^5.5.2",
54
50
  unbuild: "^2.0.0"
55
51
  };
52
+ const dependencies = {
53
+ "@iflyrpa/actions": "workspace:*",
54
+ "@iflyrpa/share": "workspace:*",
55
+ "@electron/get": "^2.0.0",
56
+ "@sentry/node": "^5.5.0",
57
+ "extract-zip": "^2.0.1",
58
+ loglevel: "^1.9.2",
59
+ pacote: "^20.0.0"
60
+ };
56
61
  const packageJson = {
57
62
  name: name,
58
63
  type: type,
@@ -64,290 +69,9 @@ const packageJson = {
64
69
  author: author,
65
70
  license: license,
66
71
  files: files,
67
- dependencies: dependencies,
68
72
  peerDependencies: peerDependencies,
69
- devDependencies: devDependencies
70
- };
71
-
72
- async function downloadImage(url, savePath) {
73
- await ensureFile(savePath);
74
- return new Promise((resolve, reject) => {
75
- https__default.get(url, (response) => {
76
- if (response.statusCode === 200) {
77
- const fileStream = fs__default.createWriteStream(savePath);
78
- response.pipe(fileStream);
79
- fileStream.on("finish", () => {
80
- fileStream.close();
81
- console.log("\u4E0B\u8F7D\u5B8C\u6210\uFF0C\u6587\u4EF6\u5DF2\u4FDD\u5B58\u81F3:", savePath);
82
- resolve(savePath);
83
- });
84
- } else {
85
- console.log("\u56FE\u7247\u4E0B\u8F7D\u5931\u8D25:", response.statusCode);
86
- response.resume();
87
- reject();
88
- }
89
- }).on("error", (error) => {
90
- console.error("\u8BF7\u6C42\u56FE\u7247\u65F6\u53D1\u751F\u9519\u8BEF:", error.message);
91
- reject();
92
- });
93
- });
94
- }
95
- function getFilenameFromUrl(imageUrl) {
96
- const parsedUrl = new URL(imageUrl);
97
- return path__default.basename(parsedUrl.pathname);
98
- }
99
- function pathExists(path2) {
100
- return new Promise((resolve) => {
101
- fs__default.stat(path2, (err) => {
102
- resolve(!err);
103
- });
104
- });
105
- }
106
- function ensureFile(filePath) {
107
- return new Promise((resolve, reject) => {
108
- const dirPath = path__default.dirname(filePath);
109
- fs__default.stat(dirPath, (err, stats) => {
110
- if (err) {
111
- if (err.code === "ENOENT") {
112
- fs__default.mkdir(dirPath, { recursive: true }, (err2) => {
113
- if (err2) {
114
- return reject(err2);
115
- }
116
- createFile();
117
- });
118
- } else {
119
- return reject(err);
120
- }
121
- } else if (stats.isDirectory()) {
122
- checkFile();
123
- } else {
124
- reject(new Error(`${dirPath} is not a directory`));
125
- }
126
- });
127
- function checkFile() {
128
- fs__default.stat(filePath, (err, stats) => {
129
- if (err) {
130
- if (err.code === "ENOENT") {
131
- createFile();
132
- } else {
133
- reject(err);
134
- }
135
- } else if (stats.isFile()) {
136
- resolve();
137
- } else {
138
- reject(new Error(`${filePath} is not a file`));
139
- }
140
- });
141
- }
142
- function createFile() {
143
- fs__default.writeFile(filePath, "", (err) => {
144
- if (err) {
145
- reject(err);
146
- } else {
147
- resolve();
148
- }
149
- });
150
- }
151
- });
152
- }
153
- function ensureFileSync(filePath) {
154
- const dirPath = path__default.dirname(filePath);
155
- try {
156
- if (!fs__default.existsSync(dirPath)) {
157
- fs__default.mkdirSync(dirPath, { recursive: true });
158
- }
159
- } catch (err) {
160
- if (err instanceof Error) {
161
- throw new Error(`Error creating directory: ${err.message}`);
162
- }
163
- }
164
- try {
165
- if (!fs__default.existsSync(filePath)) {
166
- fs__default.writeFileSync(filePath, "");
167
- }
168
- } catch (err) {
169
- if (err instanceof Error) {
170
- throw new Error(`Error creating file: ${err.message}`);
171
- }
172
- }
173
- }
174
- function writeFile(filePath, data) {
175
- return new Promise((resolve, reject) => {
176
- fs__default.writeFile(filePath, data, (err) => {
177
- if (err) {
178
- console.error("Error writing file:", err);
179
- reject();
180
- } else {
181
- console.log("File written successfully");
182
- resolve();
183
- }
184
- });
185
- });
186
- }
187
- function compareVersions(v1, v2) {
188
- const parts1 = v1.split(".").map(Number);
189
- const parts2 = v2.split(".").map(Number);
190
- for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
191
- const num1 = i < parts1.length ? parts1[i] : 0;
192
- const num2 = i < parts2.length ? parts2[i] : 0;
193
- if (num1 > num2)
194
- return 1;
195
- if (num1 < num2)
196
- return -1;
197
- }
198
- return 0;
199
- }
200
- const semver = {
201
- gt: (v1, v2) => compareVersions(v1, v2) === 1
202
- };
203
- function fetchJSON(url) {
204
- return new Promise((resolve, reject) => {
205
- https__default.get(url, (res) => {
206
- let data = "";
207
- res.on("data", (chunk) => {
208
- data += chunk;
209
- });
210
- res.on("end", () => {
211
- try {
212
- const parsedData = JSON.parse(data);
213
- resolve(parsedData);
214
- } catch (e) {
215
- if (e instanceof Error) {
216
- reject(new Error(`Error parsing JSON: ${e.message}`));
217
- }
218
- }
219
- });
220
- }).on("error", (err) => {
221
- reject(new Error(`Request failed: ${err.message}`));
222
- });
223
- });
224
- }
225
- function isNil(value) {
226
- return value === null || value === void 0;
227
- }
228
-
229
- const visibleRangeTexts = {
230
- public: "\u516C\u5F00",
231
- private: "\u79C1\u5BC6"
232
- };
233
- const xiaohongshuPublishAction = async (props) => {
234
- const { page, tmpCachePath, params } = props;
235
- const selectAddress = async (selector, address) => {
236
- const instance = typeof selector === "string" ? page.locator(selector) : selector;
237
- await instance.click();
238
- await instance.locator("input").fill(address);
239
- const poperInstance = page.locator(
240
- '.d-popover:not([style*="display: none"]) .d-options .d-grid-item'
241
- );
242
- await poperInstance.first().waitFor();
243
- await poperInstance.first().click();
244
- };
245
- const selectDate = async (selector, date) => {
246
- const instance = typeof selector === "string" ? page.locator(selector) : selector;
247
- await instance.click();
248
- await instance.fill(date);
249
- await instance.blur();
250
- };
251
- await page.waitForSelector("#CreatorPlatform", { state: "visible" }).catch(() => {
252
- throw new Error("\u767B\u5F55\u5931\u8D25");
253
- });
254
- await page.locator("#content-area .menu-container .publish-video a").click().catch(() => {
255
- throw new Error("\u672A\u627E\u5230\u53D1\u5E03\u7B14\u8BB0\u6309\u94AE");
256
- });
257
- await page.locator(".creator-container .header .title").filter({ hasText: /^上传图文$/ }).click().catch(() => {
258
- throw new Error("\u672A\u627E\u5230\u4E0A\u4F20\u56FE\u6587\u6309\u94AE");
259
- });
260
- const images = await Promise.all(
261
- params.banners.map((url) => {
262
- const fileName = getFilenameFromUrl(url);
263
- return downloadImage(url, path__default.join(tmpCachePath, fileName));
264
- })
265
- );
266
- const fileChooserPromise = page.waitForEvent("filechooser");
267
- await page.getByRole("textbox").click();
268
- const fileChooser = await fileChooserPromise;
269
- await fileChooser.setFiles(images);
270
- const titleInstance = page.locator(".input.titleInput input");
271
- await titleInstance.click();
272
- await titleInstance.fill(params.title);
273
- const descInstance = page.locator("#post-textarea");
274
- await descInstance.click();
275
- await descInstance.fill(params.content);
276
- const container = page.locator(".creator-container .content .scroll-content");
277
- await container.focus();
278
- await page.mouse.wheel(0, 500);
279
- if (params.address) {
280
- await selectAddress(
281
- page.locator(".media-extension .address-input").filter({ hasText: "\u6DFB\u52A0\u5730\u70B9" }),
282
- params.address
283
- );
284
- }
285
- if (params.selfDeclaration) {
286
- await page.locator(".declaration-wrapper").click();
287
- const selfDeclarationInstance = page.locator(
288
- ".el-popper[aria-hidden=false] ul li[role=menuitem]"
289
- );
290
- if (params.selfDeclaration.type === "fictional-rendition") {
291
- await selfDeclarationInstance.filter({ hasText: "\u865A\u6784\u6F14\u7ECE\uFF0C\u4EC5\u4F9B\u5A31\u4E50" }).click();
292
- } else if (params.selfDeclaration.type === "ai-generated") {
293
- await selfDeclarationInstance.filter({ hasText: "\u7B14\u8BB0\u542BAI\u5408\u6210\u5185\u5BB9" }).click();
294
- } else if (params.selfDeclaration.type === "source-statement") {
295
- await selfDeclarationInstance.filter({ hasText: "\u5185\u5BB9\u6765\u6E90\u58F0\u660E" }).click();
296
- const selfDeclarationSecondaryMenuInstance = page.locator(".el-popper[aria-hidden=false] .el-cascader-menu").nth(1).locator("ul li[role=menuitem]");
297
- await selfDeclarationSecondaryMenuInstance.first().waitFor();
298
- if (params.selfDeclaration.childType === "self-labeling") {
299
- await selfDeclarationSecondaryMenuInstance.filter({ hasText: "\u5DF2\u81EA\u4E3B\u6807\u6CE8" }).click();
300
- } else if (params.selfDeclaration.childType === "self-shooting") {
301
- const { shootingDate, shootingLocation } = params.selfDeclaration;
302
- await selfDeclarationSecondaryMenuInstance.filter({ hasText: "\u81EA\u4E3B\u62CD\u6444" }).click();
303
- const selfShootingPopup = page.locator(".el-overlay-dialog[aria-modal=true][role=dialog]").filter({ hasText: "\u81EA\u4E3B\u62CD\u6444" });
304
- await selfShootingPopup.waitFor();
305
- const hasCustomContent = shootingDate || shootingLocation;
306
- if (shootingLocation) {
307
- await selectAddress(
308
- selfShootingPopup.locator(".address-input"),
309
- shootingLocation
310
- );
311
- }
312
- if (shootingDate) {
313
- await selectDate(
314
- selfShootingPopup.locator(".date-picker input"),
315
- shootingDate
316
- );
317
- }
318
- await selfShootingPopup.locator("footer button").filter({ hasText: hasCustomContent ? "\u786E\u8BA4" : "\u53D6\u6D88" }).click();
319
- } else if (params.selfDeclaration.childType === "transshipment") {
320
- await selfDeclarationSecondaryMenuInstance.filter({ hasText: "\u6765\u6E90\u8F6C\u8F7D" }).click();
321
- const selfShootingPopup = page.locator(".el-overlay-dialog[aria-modal=true][role=dialog]").filter({ hasText: "\u6765\u6E90\u5A92\u4F53" });
322
- await selfShootingPopup.waitFor();
323
- const sourceMedia = params.selfDeclaration.sourceMedia;
324
- if (sourceMedia) {
325
- await selfShootingPopup.locator(".el-input input").fill(sourceMedia);
326
- }
327
- await selfShootingPopup.locator("footer button").filter({ hasText: sourceMedia ? "\u786E\u8BA4" : "\u53D6\u6D88" }).click();
328
- }
329
- }
330
- }
331
- const publicLabelInstance = page.locator("label").filter({ hasText: visibleRangeTexts[params.visibleRange] });
332
- await publicLabelInstance.click();
333
- const releaseTimeInstance = page.locator("label").filter({ hasText: params.isImmediatelyPublish ? "\u7ACB\u5373\u53D1\u5E03" : "\u5B9A\u65F6\u53D1\u5E03" });
334
- await releaseTimeInstance.click();
335
- if (params.scheduledPublish) {
336
- await selectDate(".date-picker input", params.scheduledPublish);
337
- }
338
- const response = await new Promise((resolve) => {
339
- const handleResponse = async (response2) => {
340
- if (response2.url().includes("/web_api/sns/v2/note")) {
341
- const jsonResponse = await response2.json();
342
- page.off("response", handleResponse);
343
- resolve(jsonResponse?.data?.id);
344
- }
345
- };
346
- page.on("response", handleResponse);
347
- page.locator(".submit .publishBtn").click();
348
- });
349
- await page.close();
350
- return response;
73
+ devDependencies: devDependencies,
74
+ dependencies: dependencies
351
75
  };
352
76
 
353
77
  var __defProp$4 = Object.defineProperty;
@@ -357,15 +81,17 @@ var __publicField$4 = (obj, key, value) => {
357
81
  return value;
358
82
  };
359
83
  const ELECTRON_MIRROR = "http://npmmirror.com/mirrors/electron/";
84
+ const ELECTRON_VERSION = "22.3.27";
360
85
  class ElectronInstall {
361
- constructor(rootDir, version, logger) {
362
- this.rootDir = rootDir;
363
- this.version = version;
364
- this.logger = logger;
86
+ constructor(task) {
87
+ this.task = task;
88
+ __publicField$4(this, "rootDir");
365
89
  __publicField$4(this, "electronPath");
366
90
  __publicField$4(this, "platformPath");
91
+ __publicField$4(this, "version", ELECTRON_VERSION);
367
92
  this.platformPath = this.getPlatformPath();
368
- this.electronPath = path__default.join(rootDir, this.platformPath);
93
+ this.rootDir = path__default.join(task.packagesDir, `electron@${this.version}`);
94
+ this.electronPath = path__default.join(this.rootDir, this.platformPath);
369
95
  }
370
96
  isInstalled() {
371
97
  try {
@@ -398,7 +124,7 @@ class ElectronInstall {
398
124
  }
399
125
  async install() {
400
126
  const installed = this.isInstalled();
401
- this.logger.info(`electron@${this.version} \u662F\u5426\u5DF2\u5B89\u88C5\uFF1A${installed}`);
127
+ this.task.logger.info(`electron@${this.version} \u662F\u5426\u5DF2\u5B89\u88C5\uFF1A${installed}`);
402
128
  if (installed)
403
129
  return this.electronPath;
404
130
  const platform = process.env.npm_config_platform || process.platform;
@@ -415,7 +141,7 @@ class ElectronInstall {
415
141
  }
416
142
  }
417
143
  try {
418
- this.logger.info(`electron@${this.version} \u5F00\u59CB\u4E0B\u8F7D\u8D44\u6E90`);
144
+ this.task.logger.info(`electron@${this.version} \u5F00\u59CB\u4E0B\u8F7D\u8D44\u6E90`);
419
145
  const zipPath = await get.downloadArtifact({
420
146
  version: this.version,
421
147
  artifactName: "electron",
@@ -427,13 +153,13 @@ class ElectronInstall {
427
153
  platform,
428
154
  arch
429
155
  });
430
- this.logger.info(`electron@${this.version} \u5F00\u59CB\u89E3\u538B\u8D44\u6E90`);
156
+ this.task.logger.info(`electron@${this.version} \u5F00\u59CB\u89E3\u538B\u8D44\u6E90`);
431
157
  process.noAsar = true;
432
158
  await extract__default(zipPath, { dir: this.rootDir });
433
- this.logger.info(`electron@${this.version} \u4E0B\u8F7D\u6210\u529F`);
159
+ this.task.logger.info(`electron@${this.version} \u4E0B\u8F7D\u6210\u529F`);
434
160
  return this.electronPath;
435
161
  } catch (error) {
436
- this.logger.error(`electron@${this.version} \u4E0B\u8F7D\u5931\u8D25`, error);
162
+ this.task.logger.error(`electron@${this.version} \u4E0B\u8F7D\u5931\u8D25`, error);
437
163
  throw error;
438
164
  }
439
165
  }
@@ -529,11 +255,11 @@ app.whenReady().then(() => {});
529
255
  app.on("window-all-closed", (e) => e.preventDefault());
530
256
  `;
531
257
  const generateFile = async (dir) => {
532
- const filePath = path__default.join(dir, "src", "main.js");
533
- const isPathExists = await pathExists(filePath);
258
+ const filePath = path__default.join(dir, "main.js");
259
+ const isPathExists = await share.pathExists(filePath);
534
260
  if (!isPathExists) {
535
- await ensureFile(filePath);
536
- await writeFile(filePath, template);
261
+ await share.ensureFile(filePath);
262
+ await share.writeFile(filePath, template);
537
263
  }
538
264
  return filePath;
539
265
  };
@@ -555,125 +281,92 @@ const launchElectronApp = async (cachePath, playwright, electronPath) => {
555
281
  }
556
282
  };
557
283
 
558
- var __defProp$1 = Object.defineProperty;
559
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
560
- var __publicField$1 = (obj, key, value) => {
561
- __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
562
- return value;
563
- };
564
284
  const NPM_REGISTRY = "https://registry.npmmirror.com/";
565
- class PackageManager extends livePluginManager.PluginManager {
566
- constructor(params) {
567
- super({
568
- cwd: params.cacheDir,
569
- pluginsPath: path__default.join(params.cacheDir, "node_modules"),
570
- npmRegistryUrl: NPM_REGISTRY
571
- });
572
- __publicField$1(this, "cacheDir");
573
- // 依赖安装目录
574
- __publicField$1(this, "forceUpdate");
575
- // 是否强制更新
576
- __publicField$1(this, "initPromise");
577
- __publicField$1(this, "logger");
578
- this.logger = Logger.getInstance(params.cacheDir);
579
- this.cacheDir = params.cacheDir;
580
- this.forceUpdate = isNil(params.forceUpdate) ? true : params.forceUpdate;
581
- this.initCacheProject();
582
- this.initPromise = this.init(params.packageName, params.packageVersion);
583
- }
584
- // 构建 package.json 文件,有了该文件后,依赖可以安装在该目录下,避免污染全局环境
585
- initCacheProject() {
586
- const packagePath = path__default.join(this.cacheDir, "package.json");
587
- if (!fs__default.existsSync(packagePath)) {
588
- this.logger.info("package.json \u4E0D\u5B58\u5728\uFF0C\u6DFB\u52A0\u8BE5\u6587\u4EF6");
589
- const pkg = {
590
- name: "rpa-plugins",
591
- description: "rpa-plugins",
592
- license: "MIT",
593
- main: "./src/main.js"
594
- };
595
- ensureFileSync(packagePath);
596
- fs__default.writeFileSync(packagePath, JSON.stringify(pkg), "utf8");
597
- }
285
+ class PackageManager {
286
+ constructor(task) {
287
+ this.task = task;
598
288
  }
599
289
  // 获取依赖信息
600
- async getPluginInfo(module) {
601
- const res = await fetchJSON(
602
- `https://registry.npmjs.com/-/v1/search?text=${module}`
603
- );
604
- const packages = res.objects;
605
- return packages.find((it) => it.package.name === module)?.package;
606
- }
607
- // 查询本地安装的依赖
608
- getPlugin(module) {
609
- const pluginDir = path__default.join(this.cacheDir, "node_modules");
290
+ async getManifest(module) {
610
291
  try {
611
- return require(path__default.join(pluginDir, module));
292
+ return pacote__default.manifest(module, { registry: NPM_REGISTRY });
612
293
  } catch (error) {
613
- return null;
294
+ this.task.logger.error("\u83B7\u53D6\u4F9D\u8D56\u4FE1\u606F\u5931\u8D25", error);
295
+ throw error;
614
296
  }
615
297
  }
616
- async getPluginAfterInit(module) {
617
- await this.initPromise;
618
- return this.getPlugin(module);
619
- }
620
298
  // 安装依赖
621
- async installPackage(name, version) {
299
+ async extract(name, version = "latest") {
300
+ const packageName = `${name}@${version}`;
622
301
  try {
623
- this.logger.info(`${name} \u5F00\u59CB\u5B89\u88C5`);
624
- await this.install(name, version);
625
- this.logger.info(`${name} \u5B89\u88C5\u6210\u529F`);
302
+ this.task.logger.info(`${packageName} \u5F00\u59CB\u5B89\u88C5`);
303
+ await pacote__default.extract(
304
+ packageName,
305
+ path__default.join(this.task.packagesDir, packageName),
306
+ { registry: NPM_REGISTRY }
307
+ );
308
+ this.task.logger.info(`${packageName} \u5B89\u88C5\u6210\u529F`);
626
309
  } catch (error) {
627
- this.logger.error(`${name} \u5B89\u88C5\u5931\u8D25`, error);
310
+ this.task.logger.error(`${packageName} \u5B89\u88C5\u5931\u8D25`, error);
628
311
  }
629
312
  }
630
- async init(name, version) {
631
- const plugin = this.getPlugin(path__default.join(name, "package.json"));
632
- this.logger.info(`${name} \u662F\u5426\u5DF2\u5B89\u88C5\uFF1A${!!plugin}`);
313
+ // 查询本地安装的依赖
314
+ require(module) {
633
315
  try {
634
- if (!plugin) {
635
- await this.installPackage(name, version);
636
- } else if (this.forceUpdate) {
637
- const pkInfo = await this.getPluginInfo(name);
638
- if (!pkInfo)
639
- return;
640
- const hasNewVersion = semver.gt(pkInfo.version, plugin.version);
641
- if (hasNewVersion) {
642
- this.logger.info(`${name} \u68C0\u67E5\u5230\u65B0\u7248\u672C ${pkInfo.version}`);
643
- await this.installPackage(name, pkInfo.version);
644
- }
645
- }
646
- this.logger.info(`${name} package manager init done!`);
316
+ return require(path__default.join(this.task.packagesDir, module));
647
317
  } catch (error) {
648
- this.logger.error(`${name} package manager init error`, error);
318
+ return null;
319
+ }
320
+ }
321
+ // 安装依赖
322
+ async install(name, version) {
323
+ const packageName = `${name}@${version}`;
324
+ const plugin = this.require(packageName);
325
+ this.task.logger.info(`${packageName} \u662F\u5426\u5DF2\u5B89\u88C5\uFF1A${!!plugin}`);
326
+ if (!plugin) {
327
+ await this.extract(name, version);
649
328
  }
650
329
  }
330
+ // 更新依赖
331
+ async update(name) {
332
+ const manifest = await this.getManifest(`${name}@latest`);
333
+ const version = manifest.version;
334
+ const plugin = this.require(`${name}@${version}`);
335
+ if (!plugin) {
336
+ await this.extract(name, version);
337
+ }
338
+ return version;
339
+ }
651
340
  }
652
341
 
653
- var __defProp = Object.defineProperty;
654
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
655
- var __publicField = (obj, key, value) => {
656
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
342
+ var __defProp$1 = Object.defineProperty;
343
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
344
+ var __publicField$1 = (obj, key, value) => {
345
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
657
346
  return value;
658
347
  };
659
348
  const PLAYWRIGHT_VERSION = "1.46.1";
660
349
  const PLAYWRIGHT_NAME = "playwright-core";
661
- const ELECTRON_VERSION = "22.3.27";
662
- class LocalAutomateTask {
350
+ class Task {
663
351
  constructor({ cachePath, debug }) {
664
- __publicField(this, "logger");
665
- __publicField(this, "cachePath");
666
- __publicField(this, "debug");
667
- __publicField(this, "playwrightPackage");
668
- __publicField(this, "electronPackage");
669
- __publicField(this, "_electronApp", null);
352
+ __publicField$1(this, "logger");
353
+ __publicField$1(this, "cachePath");
354
+ __publicField$1(this, "debug");
355
+ __publicField$1(this, "packagesDir");
356
+ // 依赖安装目录
357
+ __publicField$1(this, "packageManager");
358
+ __publicField$1(this, "playwrightPackage");
359
+ __publicField$1(this, "electronPackage");
360
+ __publicField$1(this, "_electronApp", null);
670
361
  /**
671
362
  * 应用是否已关闭
672
363
  */
673
- __publicField(this, "isClosed", false);
364
+ __publicField$1(this, "isClosed", false);
674
365
  this.cachePath = cachePath;
366
+ this.packagesDir = path__default.join(cachePath, "packages");
675
367
  this.debug = debug || false;
676
368
  this.logger = Logger.getInstance(cachePath);
369
+ this.packageManager = new PackageManager(this);
677
370
  this.playwrightPackage = this.installPlaywright();
678
371
  this.electronPackage = this.installElectron();
679
372
  }
@@ -681,26 +374,17 @@ class LocalAutomateTask {
681
374
  * 安装 playwright
682
375
  * @returns
683
376
  */
684
- installPlaywright() {
685
- const playwrightPackageManager = new PackageManager({
686
- packageName: PLAYWRIGHT_NAME,
687
- packageVersion: PLAYWRIGHT_VERSION,
688
- cacheDir: this.cachePath,
689
- forceUpdate: false
690
- });
691
- return playwrightPackageManager.getPluginAfterInit(PLAYWRIGHT_NAME);
377
+ async installPlaywright() {
378
+ const packageName = `${PLAYWRIGHT_NAME}@${PLAYWRIGHT_VERSION}`;
379
+ await this.packageManager.install(PLAYWRIGHT_NAME, PLAYWRIGHT_VERSION);
380
+ return this.packageManager.require(packageName);
692
381
  }
693
382
  /**
694
383
  * 安装 electron
695
384
  * @returns
696
385
  */
697
386
  installElectron() {
698
- const electronInstall = new ElectronInstall(
699
- path__default.join(this.cachePath, "electron"),
700
- ELECTRON_VERSION,
701
- this.logger
702
- );
703
- return electronInstall.install();
387
+ return new ElectronInstall(this).install();
704
388
  }
705
389
  /**
706
390
  * 启动 Electron
@@ -752,26 +436,8 @@ class LocalAutomateTask {
752
436
  this.isClosed = true;
753
437
  this._electronApp = null;
754
438
  }
755
- /**
756
- * 小红书自动化发布
757
- */
758
- async xiaohongshuPublish(params) {
759
- this.logger.info("\u5F00\u59CB\u5C0F\u7EA2\u4E66\u53D1\u5E03");
439
+ async createPage(pageParams) {
760
440
  const electronApp = await this.launchApp();
761
- this.logger.info("\u5C0F\u7EA2\u4E66\u53D1\u5E03\u4E13\u7528\u5BA2\u6237\u7AEF\u5DF2\u542F\u52A8");
762
- const commonCookies = {
763
- path: "/",
764
- sameSite: "lax",
765
- secure: false,
766
- domain: "xiaohongshu.com",
767
- url: "https://creator.xiaohongshu.com",
768
- httpOnly: true
769
- };
770
- const pageParams = {
771
- show: this.debug,
772
- url: params.url || "https://creator.xiaohongshu.com/publish/publish",
773
- cookies: params.cookies?.map((it) => Object.assign(commonCookies, it)) || []
774
- };
775
441
  const [page] = await Promise.all([
776
442
  electronApp.waitForEvent("window"),
777
443
  electronApp.evaluate(
@@ -798,40 +464,37 @@ class LocalAutomateTask {
798
464
  { pageParams }
799
465
  )
800
466
  ]);
801
- this.logger.info("\u5DF2\u6253\u5F00\u5C0F\u7EA2\u4E66\u53D1\u5E03\u9875\u9762");
467
+ return page;
468
+ }
469
+ }
470
+
471
+ var __defProp = Object.defineProperty;
472
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
473
+ var __publicField = (obj, key, value) => {
474
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
475
+ return value;
476
+ };
477
+ class RpaTask extends Task {
478
+ constructor(params) {
479
+ super(params);
480
+ __publicField(this, "actions", actions.actions);
481
+ params.forceUpdate && this.update();
482
+ }
483
+ async update() {
802
484
  try {
803
- const res = await xiaohongshuPublishAction({
804
- page,
805
- params,
806
- tmpCachePath: this.getTmpPath(),
807
- debug: !!this.debug
808
- });
809
- this.logger.info(`\u5C0F\u7EA2\u4E66\u53D1\u5E03\u6210\u529F\uFF1A${res}`);
810
- return res;
485
+ const name = "@iflyrpa/actions";
486
+ const version2 = await this.packageManager.update(name);
487
+ const actionPackage = this.packageManager.require(`${name}@${version2}`);
488
+ actionPackage?.actions && (this.actions = actionPackage.actions);
811
489
  } catch (error) {
812
- this.logger.error("\u5C0F\u7EA2\u4E66\u53D1\u5E03\u5931\u8D25", error);
813
- throw error;
490
+ this.logger.error("\u66F4\u65B0\u4F9D\u8D56\u5931\u8D25", error);
814
491
  }
815
492
  }
816
- }
817
- const RpaTask = (params) => {
818
- const logger = Logger.getInstance(params.cachePath);
819
- if (params.forceUpdate) {
820
- logger.info("\u5C1D\u8BD5\u4F7F\u7528\u8FDC\u7A0B\u6700\u65B0\u7248\u672C");
821
- const packageManager = new PackageManager({
822
- packageName: packageJson.name,
823
- cacheDir: params.cachePath
824
- });
825
- const localPackge = packageManager.getPlugin(packageJson.name);
826
- if (localPackge?.LocalAutomateTask && localPackge?.version && semver.gt(localPackge.version, packageJson.version)) {
827
- logger.info(`\u4F7F\u7528\u8FDC\u7A0B\u7684\u65B0\u7248\u672C\uFF0C\u7248\u672C\u53F7\u4E3A\uFF1A${localPackge.version}`);
828
- return new localPackge.LocalAutomateTask(params);
829
- }
493
+ xiaohongshuPublish(params) {
494
+ return this.actions.xiaohongshuPublish(this, params);
830
495
  }
831
- return new LocalAutomateTask(params);
832
- };
496
+ }
833
497
  const version = packageJson.version;
834
498
 
835
- exports.LocalAutomateTask = LocalAutomateTask;
836
499
  exports.RpaTask = RpaTask;
837
500
  exports.version = version;