aggroot 1.4.1 → 1.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aggroot",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "AggRoot - AI Assistant (Node.js/TypeScript Version)",
5
5
  "keywords": [
6
6
  "ai",
@@ -1,132 +1,132 @@
1
- #!/usr/bin/env node
2
-
3
- import { spawn } from "child_process";
4
- import { existsSync, readdirSync, readFileSync } from "node:fs";
5
- import { join } from "node:path";
6
- import { homedir } from "node:os";
7
-
8
- const MIRRORS = [
9
- { host: "https://npmmirror.com/mirrors/playwright", name: "淘宝镜像" },
10
- { host: "", name: "官方源" },
11
- ];
12
-
13
- // npm postinstall 会吞掉 stdout,用 stderr 输出状态让用户看到
14
- function status(msg) {
15
- process.stderr.write(`\n[browsers] ${msg}\n`);
16
- }
17
-
18
- /**
19
- * 获取当前 playwright 包需要的 chromium 版本号
20
- * 从 playwright-core 的 registry 中读取
21
- */
22
- function getRequiredChromiumVersion() {
23
- try {
24
- // 尝试从 playwright-core 的 registry 文件读取
25
- const registryPaths = [
26
- join(import.meta.url.replace('file:///', '').replace(/\/scripts\/.*/, ''), 'node_modules', 'playwright-core', 'browsers.json'),
27
- join(process.cwd(), 'node_modules', 'playwright-core', 'browsers.json'),
28
- ];
29
-
30
- // 也检查全局安装路径
31
- const globalPaths = [
32
- join(homedir(), 'AppData', 'Roaming', 'npm', 'node_modules', 'aggroot', 'node_modules', 'playwright-core', 'browsers.json'),
33
- join('/usr', 'local', 'lib', 'node_modules', 'aggroot', 'node_modules', 'playwright-core', 'browsers.json'),
34
- ];
35
-
36
- for (const p of [...registryPaths, ...globalPaths]) {
37
- const filePath = p.startsWith('/') ? p : p.replace(/\//g, '\\');
38
- if (existsSync(filePath)) {
39
- const data = JSON.parse(readFileSync(filePath, 'utf-8'));
40
- const chromium = data.browsers?.find(b => b.name === 'chromium');
41
- if (chromium?.revision) return chromium.revision;
42
- }
43
- }
44
- } catch { /* fallback to npx */ }
45
- return null;
46
- }
47
-
48
- /**
49
- * 检查 Playwright Chromium 浏览器是否已安装且版本匹配
50
- */
51
- function isChromiumInstalled() {
52
- const playwrightDir = process.platform === 'win32'
53
- ? join(homedir(), 'AppData', 'Local', 'ms-playwright')
54
- : join(homedir(), '.cache', 'ms-playwright');
55
-
56
- if (!existsSync(playwrightDir)) return false;
57
-
58
- // 查找已安装的 chromium 目录
59
- const dirs = readdirSync(playwrightDir, { withFileTypes: true })
60
- .filter(d => d.isDirectory() && d.name.startsWith('chromium-'))
61
- .map(d => d.name);
62
-
63
- if (dirs.length === 0) return false;
64
-
65
- // 检查是否有可执行文件
66
- const latestDir = dirs.sort().pop();
67
- const exeName = process.platform === 'win32' ? 'chrome.exe' : 'chrome';
68
- const possiblePaths = [
69
- join(playwrightDir, latestDir, 'chrome-win64', exeName),
70
- join(playwrightDir, latestDir, 'chrome-linux', exeName),
71
- join(playwrightDir, latestDir, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium'),
72
- ];
73
-
74
- return possiblePaths.some(p => existsSync(p));
75
- }
76
-
77
- function tryInstall(mirror) {
78
- return new Promise((resolve) => {
79
- const env = { ...process.env };
80
- if (mirror.host) {
81
- env.PLAYWRIGHT_DOWNLOAD_HOST = mirror.host;
82
- } else {
83
- delete env.PLAYWRIGHT_DOWNLOAD_HOST;
84
- }
85
-
86
- status(`正在下载 Playwright Chromium (${mirror.name})...`);
87
-
88
- const child = spawn("npx", ["playwright", "install", "chromium"], {
89
- stdio: "inherit",
90
- shell: true,
91
- env,
92
- });
93
-
94
- let timedOut = false;
95
- const timer = setTimeout(() => {
96
- timedOut = true;
97
- child.kill();
98
- }, 120000);
99
-
100
- child.on("close", (code) => {
101
- clearTimeout(timer);
102
- resolve(timedOut ? "timeout" : code);
103
- });
104
-
105
- child.on("error", () => {
106
- clearTimeout(timer);
107
- resolve("error");
108
- });
109
- });
110
- }
111
-
112
- (async () => {
113
- // 先检查是否已安装
114
- if (isChromiumInstalled()) {
115
- status("Playwright Chromium 已安装,跳过下载。");
116
- return;
117
- }
118
-
119
- status("Playwright Chromium 未安装,开始下载...");
120
-
121
- for (const mirror of MIRRORS) {
122
- const result = await tryInstall(mirror);
123
- if (result === 0) {
124
- status("浏览器安装完成。");
125
- return;
126
- }
127
- if (mirror === MIRRORS[0]) {
128
- status(`${mirror.name}下载失败,尝试${MIRRORS[1].name}...`);
129
- }
130
- }
131
- status("浏览器安装失败,将在首次使用时自动下载。");
132
- })();
1
+ #!/usr/bin/env node
2
+
3
+ import { spawn } from "child_process";
4
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
5
+ import { join } from "node:path";
6
+ import { homedir } from "node:os";
7
+
8
+ const MIRRORS = [
9
+ { host: "https://npmmirror.com/mirrors/playwright", name: "淘宝镜像" },
10
+ { host: "", name: "官方源" },
11
+ ];
12
+
13
+ // npm postinstall 会吞掉 stdout,用 stderr 输出状态让用户看到
14
+ function status(msg) {
15
+ process.stderr.write(`\n[browsers] ${msg}\n`);
16
+ }
17
+
18
+ /**
19
+ * 获取当前 playwright 包需要的 chromium 版本号
20
+ * 从 playwright-core 的 registry 中读取
21
+ */
22
+ function getRequiredChromiumVersion() {
23
+ try {
24
+ // 尝试从 playwright-core 的 registry 文件读取
25
+ const registryPaths = [
26
+ join(import.meta.url.replace('file:///', '').replace(/\/scripts\/.*/, ''), 'node_modules', 'playwright-core', 'browsers.json'),
27
+ join(process.cwd(), 'node_modules', 'playwright-core', 'browsers.json'),
28
+ ];
29
+
30
+ // 也检查全局安装路径
31
+ const globalPaths = [
32
+ join(homedir(), 'AppData', 'Roaming', 'npm', 'node_modules', 'aggroot', 'node_modules', 'playwright-core', 'browsers.json'),
33
+ join('/usr', 'local', 'lib', 'node_modules', 'aggroot', 'node_modules', 'playwright-core', 'browsers.json'),
34
+ ];
35
+
36
+ for (const p of [...registryPaths, ...globalPaths]) {
37
+ const filePath = p.startsWith('/') ? p : p.replace(/\//g, '\\');
38
+ if (existsSync(filePath)) {
39
+ const data = JSON.parse(readFileSync(filePath, 'utf-8'));
40
+ const chromium = data.browsers?.find(b => b.name === 'chromium');
41
+ if (chromium?.revision) return chromium.revision;
42
+ }
43
+ }
44
+ } catch { /* fallback to npx */ }
45
+ return null;
46
+ }
47
+
48
+ /**
49
+ * 检查 Playwright Chromium 浏览器是否已安装且版本匹配
50
+ */
51
+ function isChromiumInstalled() {
52
+ const playwrightDir = process.platform === 'win32'
53
+ ? join(homedir(), 'AppData', 'Local', 'ms-playwright')
54
+ : join(homedir(), '.cache', 'ms-playwright');
55
+
56
+ if (!existsSync(playwrightDir)) return false;
57
+
58
+ // 查找已安装的 chromium 目录
59
+ const dirs = readdirSync(playwrightDir, { withFileTypes: true })
60
+ .filter(d => d.isDirectory() && d.name.startsWith('chromium-'))
61
+ .map(d => d.name);
62
+
63
+ if (dirs.length === 0) return false;
64
+
65
+ // 检查是否有可执行文件
66
+ const latestDir = dirs.sort().pop();
67
+ const exeName = process.platform === 'win32' ? 'chrome.exe' : 'chrome';
68
+ const possiblePaths = [
69
+ join(playwrightDir, latestDir, 'chrome-win64', exeName),
70
+ join(playwrightDir, latestDir, 'chrome-linux', exeName),
71
+ join(playwrightDir, latestDir, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium'),
72
+ ];
73
+
74
+ return possiblePaths.some(p => existsSync(p));
75
+ }
76
+
77
+ function tryInstall(mirror) {
78
+ return new Promise((resolve) => {
79
+ const env = { ...process.env };
80
+ if (mirror.host) {
81
+ env.PLAYWRIGHT_DOWNLOAD_HOST = mirror.host;
82
+ } else {
83
+ delete env.PLAYWRIGHT_DOWNLOAD_HOST;
84
+ }
85
+
86
+ status(`正在下载 Playwright Chromium (${mirror.name})...`);
87
+
88
+ const child = spawn("npx", ["playwright", "install", "chromium"], {
89
+ stdio: "inherit",
90
+ shell: true,
91
+ env,
92
+ });
93
+
94
+ let timedOut = false;
95
+ const timer = setTimeout(() => {
96
+ timedOut = true;
97
+ child.kill();
98
+ }, 120000);
99
+
100
+ child.on("close", (code) => {
101
+ clearTimeout(timer);
102
+ resolve(timedOut ? "timeout" : code);
103
+ });
104
+
105
+ child.on("error", () => {
106
+ clearTimeout(timer);
107
+ resolve("error");
108
+ });
109
+ });
110
+ }
111
+
112
+ (async () => {
113
+ // 先检查是否已安装
114
+ if (isChromiumInstalled()) {
115
+ status("Playwright Chromium 已安装,跳过下载。");
116
+ return;
117
+ }
118
+
119
+ status("Playwright Chromium 未安装,开始下载...");
120
+
121
+ for (const mirror of MIRRORS) {
122
+ const result = await tryInstall(mirror);
123
+ if (result === 0) {
124
+ status("浏览器安装完成。");
125
+ return;
126
+ }
127
+ if (mirror === MIRRORS[0]) {
128
+ status(`${mirror.name}下载失败,尝试${MIRRORS[1].name}...`);
129
+ }
130
+ }
131
+ status("浏览器安装失败,将在首次使用时自动下载。");
132
+ })();