claude-coder 1.9.0 → 1.9.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/README.md +214 -214
- package/bin/cli.js +155 -155
- package/package.json +55 -55
- package/recipes/_shared/roles/developer.md +11 -11
- package/recipes/_shared/roles/product.md +12 -12
- package/recipes/_shared/roles/tester.md +12 -12
- package/recipes/_shared/test/report-format.md +86 -86
- package/recipes/backend/base.md +27 -27
- package/recipes/backend/components/auth.md +18 -18
- package/recipes/backend/components/crud-api.md +18 -18
- package/recipes/backend/components/file-service.md +15 -15
- package/recipes/backend/manifest.json +20 -20
- package/recipes/backend/test/api-test.md +25 -25
- package/recipes/console/base.md +37 -37
- package/recipes/console/components/modal-form.md +20 -20
- package/recipes/console/components/pagination.md +17 -17
- package/recipes/console/components/search.md +17 -17
- package/recipes/console/components/table-list.md +18 -18
- package/recipes/console/components/tabs.md +14 -14
- package/recipes/console/components/tree.md +15 -15
- package/recipes/console/components/upload.md +15 -15
- package/recipes/console/manifest.json +24 -24
- package/recipes/console/test/crud-e2e.md +47 -47
- package/recipes/h5/base.md +26 -26
- package/recipes/h5/components/animation.md +11 -11
- package/recipes/h5/components/countdown.md +11 -11
- package/recipes/h5/components/share.md +11 -11
- package/recipes/h5/components/swiper.md +11 -11
- package/recipes/h5/manifest.json +21 -21
- package/recipes/h5/test/h5-e2e.md +20 -20
- package/src/commands/auth.js +420 -362
- package/src/commands/setup-modules/helpers.js +100 -100
- package/src/commands/setup-modules/index.js +25 -25
- package/src/commands/setup-modules/mcp.js +115 -115
- package/src/commands/setup-modules/provider.js +260 -260
- package/src/commands/setup-modules/safety.js +47 -47
- package/src/commands/setup-modules/simplify.js +52 -52
- package/src/commands/setup.js +172 -172
- package/src/common/assets.js +245 -245
- package/src/common/config.js +125 -125
- package/src/common/constants.js +55 -55
- package/src/common/indicator.js +260 -260
- package/src/common/interaction.js +170 -170
- package/src/common/logging.js +77 -77
- package/src/common/sdk.js +50 -50
- package/src/common/tasks.js +88 -88
- package/src/common/utils.js +213 -213
- package/src/core/coding.js +33 -33
- package/src/core/go.js +264 -264
- package/src/core/hooks.js +500 -500
- package/src/core/init.js +166 -165
- package/src/core/plan.js +188 -187
- package/src/core/prompts.js +247 -247
- package/src/core/repair.js +36 -36
- package/src/core/runner.js +471 -458
- package/src/core/scan.js +93 -93
- package/src/core/session.js +280 -271
- package/src/core/simplify.js +74 -74
- package/src/core/state.js +105 -105
- package/src/index.js +76 -76
- package/templates/bash-process.md +12 -12
- package/templates/codingSystem.md +65 -65
- package/templates/codingUser.md +17 -17
- package/templates/coreProtocol.md +29 -29
- package/templates/goSystem.md +130 -130
- package/templates/guidance.json +72 -72
- package/templates/planSystem.md +78 -78
- package/templates/planUser.md +8 -8
- package/templates/requirements.example.md +57 -57
- package/templates/scanSystem.md +120 -120
- package/templates/scanUser.md +10 -10
- package/templates/test_rule.md +194 -194
- package/templates/web-testing.md +17 -17
- package/types/index.d.ts +217 -217
package/src/commands/auth.js
CHANGED
|
@@ -1,362 +1,420 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const os = require('os');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const
|
|
7
|
-
const {
|
|
8
|
-
const {
|
|
9
|
-
const {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
mcpConfig.mcpServers
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
content
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
//
|
|
112
|
-
//
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
`
|
|
119
|
-
`
|
|
120
|
-
` const
|
|
121
|
-
`
|
|
122
|
-
`
|
|
123
|
-
`
|
|
124
|
-
`
|
|
125
|
-
`
|
|
126
|
-
` const
|
|
127
|
-
`
|
|
128
|
-
`
|
|
129
|
-
`
|
|
130
|
-
`
|
|
131
|
-
`
|
|
132
|
-
`
|
|
133
|
-
`
|
|
134
|
-
`
|
|
135
|
-
`
|
|
136
|
-
`
|
|
137
|
-
`
|
|
138
|
-
`
|
|
139
|
-
`
|
|
140
|
-
`
|
|
141
|
-
`
|
|
142
|
-
`
|
|
143
|
-
`
|
|
144
|
-
`
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
//
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
log('
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
console.log('
|
|
184
|
-
console.log('
|
|
185
|
-
console.log('
|
|
186
|
-
console.log('
|
|
187
|
-
console.log('');
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
log('
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
log('
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
log('info',
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
console.log('
|
|
222
|
-
console.log('
|
|
223
|
-
console.log('
|
|
224
|
-
console.log('
|
|
225
|
-
console.log('');
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
log('
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
log('
|
|
253
|
-
log('
|
|
254
|
-
log('info', '
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
console.log('');
|
|
260
|
-
console.log('
|
|
261
|
-
console.log('
|
|
262
|
-
console.log('');
|
|
263
|
-
console.log('
|
|
264
|
-
console.log('
|
|
265
|
-
console.log('
|
|
266
|
-
console.log('
|
|
267
|
-
console.log('
|
|
268
|
-
console.log('');
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
log('
|
|
276
|
-
log('
|
|
277
|
-
log('info', '
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const http = require('http');
|
|
7
|
+
const { execSync } = require('child_process');
|
|
8
|
+
const { loadConfig, log } = require('../common/config');
|
|
9
|
+
const { assets } = require('../common/assets');
|
|
10
|
+
const { appendGitignore } = require('../common/utils');
|
|
11
|
+
|
|
12
|
+
function resolvePlaywright() {
|
|
13
|
+
const { createRequire } = require('module');
|
|
14
|
+
const pkg = 'playwright';
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
return path.dirname(require.resolve(`${pkg}/package.json`));
|
|
18
|
+
} catch {}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const r = createRequire(path.join(process.cwd(), 'noop.js'));
|
|
22
|
+
return path.dirname(r.resolve(`${pkg}/package.json`));
|
|
23
|
+
} catch {}
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const globalRoot = execSync('npm root -g', { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] }).trim();
|
|
27
|
+
const pkgJsonPath = path.join(globalRoot, pkg, 'package.json');
|
|
28
|
+
if (fs.existsSync(pkgJsonPath)) return path.join(globalRoot, pkg);
|
|
29
|
+
} catch {}
|
|
30
|
+
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function normalizeUrl(url) {
|
|
35
|
+
if (!url) return null;
|
|
36
|
+
return /^https?:\/\//.test(url) ? url : `http://${url}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function updateGitignore(entry) {
|
|
40
|
+
if (appendGitignore(assets.projectRoot, entry)) {
|
|
41
|
+
log('ok', `.gitignore 已添加: ${entry}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ─────────────────────────────────────────────────────────────
|
|
46
|
+
// .mcp.json 配置(Playwright / Chrome DevTools 共用)
|
|
47
|
+
// ─────────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
function updateMcpConfig(mcpPath, tool, mode) {
|
|
50
|
+
let mcpConfig = {};
|
|
51
|
+
if (fs.existsSync(mcpPath)) {
|
|
52
|
+
try { mcpConfig = JSON.parse(fs.readFileSync(mcpPath, 'utf8')); } catch {}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!mcpConfig.mcpServers) mcpConfig.mcpServers = {};
|
|
56
|
+
|
|
57
|
+
if (tool === 'chrome-devtools') {
|
|
58
|
+
delete mcpConfig.mcpServers.playwright;
|
|
59
|
+
mcpConfig.mcpServers['chrome-devtools'] = {
|
|
60
|
+
command: 'npx',
|
|
61
|
+
args: ['-y', 'chrome-devtools-mcp@latest', '--autoConnect'],
|
|
62
|
+
};
|
|
63
|
+
fs.writeFileSync(mcpPath, JSON.stringify(mcpConfig, null, 2) + '\n', 'utf8');
|
|
64
|
+
log('ok', '.mcp.json 已配置 Chrome DevTools MCP (autoConnect)');
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Playwright MCP
|
|
69
|
+
delete mcpConfig.mcpServers['chrome-devtools'];
|
|
70
|
+
const args = ['@playwright/mcp@latest'];
|
|
71
|
+
const projectRoot = assets.projectRoot;
|
|
72
|
+
|
|
73
|
+
switch (mode) {
|
|
74
|
+
case 'persistent': {
|
|
75
|
+
const browserProfilePath = assets.path('browserProfile');
|
|
76
|
+
const relProfile = path.relative(projectRoot, browserProfilePath).split(path.sep).join('/');
|
|
77
|
+
args.push(`--user-data-dir=${relProfile}`);
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case 'isolated': {
|
|
81
|
+
const playwrightAuthPath = assets.path('playwrightAuth');
|
|
82
|
+
const relAuth = path.relative(projectRoot, playwrightAuthPath).split(path.sep).join('/');
|
|
83
|
+
args.push('--isolated', `--storage-state=${relAuth}`);
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case 'extension':
|
|
87
|
+
args.push('--extension');
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
mcpConfig.mcpServers.playwright = { command: 'npx', args };
|
|
92
|
+
fs.writeFileSync(mcpPath, JSON.stringify(mcpConfig, null, 2) + '\n', 'utf8');
|
|
93
|
+
log('ok', `.mcp.json 已配置 Playwright MCP (${mode} 模式)`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function enableWebTestEnv(tool) {
|
|
97
|
+
const envPath = assets.path('env');
|
|
98
|
+
if (!envPath || !fs.existsSync(envPath)) return;
|
|
99
|
+
|
|
100
|
+
let content = fs.readFileSync(envPath, 'utf8');
|
|
101
|
+
if (/^WEB_TEST_TOOL=/m.test(content)) {
|
|
102
|
+
content = content.replace(/^WEB_TEST_TOOL=.*/m, `WEB_TEST_TOOL=${tool}`);
|
|
103
|
+
} else {
|
|
104
|
+
const suffix = content.endsWith('\n') ? '' : '\n';
|
|
105
|
+
content += `${suffix}WEB_TEST_TOOL=${tool}\n`;
|
|
106
|
+
}
|
|
107
|
+
fs.writeFileSync(envPath, content, 'utf8');
|
|
108
|
+
log('ok', `.claude-coder/.env 已设置 WEB_TEST_TOOL=${tool}`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ─────────────────────────────────────────────────────────────
|
|
112
|
+
// 浏览器脚本(Playwright persistent 模式用)
|
|
113
|
+
// ─────────────────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
function buildBrowserScript(playwrightDir, profileDir, url) {
|
|
116
|
+
const thirtyDays = Math.floor(Date.now() / 1000) + 86400 * 30;
|
|
117
|
+
return [
|
|
118
|
+
`const { chromium } = require(${JSON.stringify(playwrightDir)});`,
|
|
119
|
+
`(async () => {`,
|
|
120
|
+
` const ctx = await chromium.launchPersistentContext(${JSON.stringify(profileDir)}, { headless: false });`,
|
|
121
|
+
` const page = ctx.pages()[0] || await ctx.newPage();`,
|
|
122
|
+
` try { await page.goto(${JSON.stringify(url)}); } catch {}`,
|
|
123
|
+
` console.log('请在浏览器中完成操作后关闭窗口...');`,
|
|
124
|
+
` const persistSessionCookies = async () => {`,
|
|
125
|
+
` try {`,
|
|
126
|
+
` const cookies = await ctx.cookies();`,
|
|
127
|
+
` const session = cookies.filter(c => c.expires === -1);`,
|
|
128
|
+
` if (session.length > 0) {`,
|
|
129
|
+
` await ctx.addCookies(session.map(c => ({ ...c, expires: ${thirtyDays} })));`,
|
|
130
|
+
` console.log('已将 ' + session.length + ' 个 session cookie 转为持久化');`,
|
|
131
|
+
` }`,
|
|
132
|
+
` } catch {}`,
|
|
133
|
+
` };`,
|
|
134
|
+
` ctx.on('page', p => p.on('close', () => persistSessionCookies()));`,
|
|
135
|
+
` for (const p of ctx.pages()) p.on('close', () => persistSessionCookies());`,
|
|
136
|
+
` await new Promise(r => {`,
|
|
137
|
+
` ctx.on('close', r);`,
|
|
138
|
+
` const t = setInterval(async () => {`,
|
|
139
|
+
` try {`,
|
|
140
|
+
` if (!ctx.pages().length) { clearInterval(t); await persistSessionCookies(); r(); }`,
|
|
141
|
+
` } catch { clearInterval(t); r(); }`,
|
|
142
|
+
` }, 2000);`,
|
|
143
|
+
` });`,
|
|
144
|
+
` try { await ctx.close(); } catch {}`,
|
|
145
|
+
`})().then(() => process.exit(0)).catch(() => process.exit(0));`,
|
|
146
|
+
].join('\n');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function runBrowserScript(script, cwd) {
|
|
150
|
+
const tmpScript = path.join(os.tmpdir(), `pw-auth-${Date.now()}.js`);
|
|
151
|
+
fs.writeFileSync(tmpScript, script);
|
|
152
|
+
try {
|
|
153
|
+
execSync(`node "${tmpScript}"`, { stdio: 'inherit', cwd });
|
|
154
|
+
return true;
|
|
155
|
+
} catch {
|
|
156
|
+
return false;
|
|
157
|
+
} finally {
|
|
158
|
+
try { fs.unlinkSync(tmpScript); } catch {}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// ─────────────────────────────────────────────────────────────
|
|
163
|
+
// auth 模式实现
|
|
164
|
+
// ─────────────────────────────────────────────────────────────
|
|
165
|
+
|
|
166
|
+
async function authPersistent(url) {
|
|
167
|
+
const playwrightDir = resolvePlaywright();
|
|
168
|
+
if (!playwrightDir) {
|
|
169
|
+
log('error', '未找到 playwright 模块');
|
|
170
|
+
log('info', '请安装: npm install -g playwright && npx playwright install chromium');
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const profileDir = assets.path('browserProfile');
|
|
175
|
+
if (!fs.existsSync(profileDir)) fs.mkdirSync(profileDir, { recursive: true });
|
|
176
|
+
|
|
177
|
+
const lockFile = path.join(profileDir, 'SingletonLock');
|
|
178
|
+
if (fs.existsSync(lockFile)) {
|
|
179
|
+
fs.unlinkSync(lockFile);
|
|
180
|
+
log('warn', '已清理残留的 SingletonLock(上次浏览器未正常关闭)');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
console.log('操作步骤:');
|
|
184
|
+
console.log(' 1. 浏览器将自动打开,请手动完成登录');
|
|
185
|
+
console.log(' 2. 登录成功后关闭浏览器窗口');
|
|
186
|
+
console.log(' 3. 登录状态将保存在持久化配置中(session cookie 自动转持久化)');
|
|
187
|
+
console.log(' 4. MCP 后续会话自动复用此登录状态');
|
|
188
|
+
console.log('');
|
|
189
|
+
|
|
190
|
+
const script = buildBrowserScript(playwrightDir, profileDir, url);
|
|
191
|
+
const projectRoot = assets.projectRoot;
|
|
192
|
+
|
|
193
|
+
const ok = runBrowserScript(script, projectRoot);
|
|
194
|
+
if (!ok) {
|
|
195
|
+
const profileFiles = fs.readdirSync(profileDir);
|
|
196
|
+
if (profileFiles.length <= 2) {
|
|
197
|
+
log('error', 'Playwright 启动失败,且未检测到有效的浏览器配置');
|
|
198
|
+
log('info', '请确保已安装 Chromium: npx playwright install chromium');
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
log('warn', '浏览器退出码非零,但已检测到有效配置,继续...');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const mcpPath = assets.path('mcpConfig');
|
|
205
|
+
log('ok', '登录状态已保存到持久化配置');
|
|
206
|
+
updateMcpConfig(mcpPath, 'playwright', 'persistent');
|
|
207
|
+
updateGitignore('.claude-coder/.runtime/browser-profile');
|
|
208
|
+
enableWebTestEnv('playwright');
|
|
209
|
+
|
|
210
|
+
console.log('');
|
|
211
|
+
log('ok', '配置完成!');
|
|
212
|
+
const relProfile = path.relative(projectRoot, profileDir);
|
|
213
|
+
log('info', `MCP 使用 persistent 模式 (user-data-dir: ${relProfile})`);
|
|
214
|
+
log('info', '验证: 再次运行 claude-coder auth <URL>,浏览器应直接进入已登录状态');
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function authIsolated(url) {
|
|
218
|
+
const playwrightAuthPath = assets.path('playwrightAuth');
|
|
219
|
+
const projectRoot = assets.projectRoot;
|
|
220
|
+
|
|
221
|
+
console.log('操作步骤:');
|
|
222
|
+
console.log(' 1. 浏览器将自动打开,请手动完成登录');
|
|
223
|
+
console.log(' 2. 登录成功后关闭浏览器窗口');
|
|
224
|
+
console.log(' 3. 登录状态(cookies + localStorage)将保存到 playwright-auth.json');
|
|
225
|
+
console.log(' 4. MCP 每次会话自动从此文件加载初始状态');
|
|
226
|
+
console.log('');
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
execSync(
|
|
230
|
+
`npx playwright codegen --save-storage="${playwrightAuthPath}" "${url}"`,
|
|
231
|
+
{ stdio: 'inherit', cwd: projectRoot }
|
|
232
|
+
);
|
|
233
|
+
} catch (err) {
|
|
234
|
+
if (!fs.existsSync(playwrightAuthPath)) {
|
|
235
|
+
log('error', `Playwright 登录状态导出失败: ${err.message}`);
|
|
236
|
+
log('info', '请确保已安装: npx playwright install chromium');
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (!fs.existsSync(playwrightAuthPath)) {
|
|
242
|
+
log('error', '未检测到导出的登录状态文件');
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const mcpPath = assets.path('mcpConfig');
|
|
247
|
+
log('ok', '登录状态已保存到 playwright-auth.json');
|
|
248
|
+
updateMcpConfig(mcpPath, 'playwright', 'isolated');
|
|
249
|
+
updateGitignore('.claude-coder/playwright-auth.json');
|
|
250
|
+
enableWebTestEnv('playwright');
|
|
251
|
+
|
|
252
|
+
console.log('');
|
|
253
|
+
log('ok', '配置完成!');
|
|
254
|
+
log('info', 'MCP 使用 isolated 模式 (storage-state)');
|
|
255
|
+
log('info', 'cookies 和 localStorage 每次会话自动从 playwright-auth.json 加载');
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function authExtension() {
|
|
259
|
+
console.log('Extension 模式说明:');
|
|
260
|
+
console.log('');
|
|
261
|
+
console.log(' 此模式通过 Chrome 扩展连接到您正在运行的浏览器。');
|
|
262
|
+
console.log(' MCP 将直接使用浏览器中已有的登录态和扩展。');
|
|
263
|
+
console.log('');
|
|
264
|
+
console.log(' 前置条件:');
|
|
265
|
+
console.log(' 1. 安装 "Playwright MCP Bridge" Chrome/Edge 扩展');
|
|
266
|
+
console.log(' https://chromewebstore.google.com/detail/playwright-mcp-bridge/mmlmfjhmonkocbjadbfplnigmagldckm');
|
|
267
|
+
console.log(' 2. 确保浏览器已启动且扩展已启用');
|
|
268
|
+
console.log(' 3. 无需额外认证操作,您的浏览器登录态将自动可用');
|
|
269
|
+
console.log('');
|
|
270
|
+
|
|
271
|
+
const mcpPath = assets.path('mcpConfig');
|
|
272
|
+
updateMcpConfig(mcpPath, 'playwright', 'extension');
|
|
273
|
+
enableWebTestEnv('playwright');
|
|
274
|
+
|
|
275
|
+
console.log('');
|
|
276
|
+
log('ok', '配置完成!');
|
|
277
|
+
log('info', 'MCP 使用 extension 模式(连接真实浏览器)');
|
|
278
|
+
log('info', '确保 Chrome/Edge 已运行且 Playwright MCP Bridge 扩展已启用');
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function getChromeCommand() {
|
|
282
|
+
if (process.platform === 'win32') {
|
|
283
|
+
const prefixes = [
|
|
284
|
+
process.env['PROGRAMFILES(X86)'],
|
|
285
|
+
process.env.PROGRAMFILES,
|
|
286
|
+
process.env.LOCALAPPDATA,
|
|
287
|
+
].filter(Boolean);
|
|
288
|
+
for (const prefix of prefixes) {
|
|
289
|
+
const p = path.join(prefix, 'Google', 'Chrome', 'Application', 'chrome.exe');
|
|
290
|
+
if (fs.existsSync(p)) return `"${p}"`;
|
|
291
|
+
}
|
|
292
|
+
return 'start chrome';
|
|
293
|
+
}
|
|
294
|
+
if (process.platform === 'darwin') return 'open -a "Google Chrome" --args';
|
|
295
|
+
return 'google-chrome';
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function checkCdpConnection(port = 9222, timeoutMs = 5000) {
|
|
299
|
+
return new Promise(resolve => {
|
|
300
|
+
const req = http.get(`http://127.0.0.1:${port}/json/version`, res => {
|
|
301
|
+
let data = '';
|
|
302
|
+
res.on('data', chunk => { data += chunk; });
|
|
303
|
+
res.on('end', () => {
|
|
304
|
+
try {
|
|
305
|
+
const info = JSON.parse(data);
|
|
306
|
+
resolve({ ok: true, browser: info.Browser || 'Chrome', wsUrl: info.webSocketDebuggerUrl || '' });
|
|
307
|
+
} catch {
|
|
308
|
+
resolve({ ok: false });
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
req.on('error', () => resolve({ ok: false }));
|
|
313
|
+
req.setTimeout(timeoutMs, () => { req.destroy(); resolve({ ok: false }); });
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async function authChromeDevTools(url) {
|
|
318
|
+
const mcpPath = assets.path('mcpConfig');
|
|
319
|
+
updateMcpConfig(mcpPath, 'chrome-devtools');
|
|
320
|
+
enableWebTestEnv('chrome-devtools');
|
|
321
|
+
|
|
322
|
+
log('ok', '.mcp.json 已配置完成');
|
|
323
|
+
console.log('');
|
|
324
|
+
|
|
325
|
+
log('info', '正在检测 Chrome DevTools 连接...');
|
|
326
|
+
let conn = await checkCdpConnection();
|
|
327
|
+
|
|
328
|
+
if (!conn.ok && url) {
|
|
329
|
+
log('info', '未检测到 Chrome 远程调试实例,尝试启动 Chrome...');
|
|
330
|
+
const chromeCmd = getChromeCommand();
|
|
331
|
+
const launchCmd = `${chromeCmd} --remote-debugging-port=9222 "${url}"`;
|
|
332
|
+
try {
|
|
333
|
+
const { spawn } = require('child_process');
|
|
334
|
+
const child = spawn(launchCmd, { shell: true, detached: true, stdio: 'ignore' });
|
|
335
|
+
child.unref();
|
|
336
|
+
} catch (err) {
|
|
337
|
+
log('warn', `Chrome 启动失败: ${err.message}`);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
for (let i = 0; i < 6; i++) {
|
|
341
|
+
await new Promise(r => setTimeout(r, 2000));
|
|
342
|
+
conn = await checkCdpConnection();
|
|
343
|
+
if (conn.ok) break;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
console.log('');
|
|
348
|
+
if (conn.ok) {
|
|
349
|
+
log('ok', `Chrome DevTools 连接成功: ${conn.browser}`);
|
|
350
|
+
if (conn.wsUrl) log('info', `WebSocket: ${conn.wsUrl}`);
|
|
351
|
+
log('ok', '配置验证通过!MCP 可以正常连接 Chrome。');
|
|
352
|
+
} else {
|
|
353
|
+
log('warn', '未检测到 Chrome 远程调试实例');
|
|
354
|
+
console.log('');
|
|
355
|
+
console.log(' 请确保:');
|
|
356
|
+
console.log(' 1. Chrome 144+ 已安装');
|
|
357
|
+
console.log(' 2. 打开 chrome://inspect/#remote-debugging 启用远程调试');
|
|
358
|
+
console.log(' 3. 允许传入调试连接');
|
|
359
|
+
console.log('');
|
|
360
|
+
console.log(' 或手动启动带远程调试的 Chrome:');
|
|
361
|
+
const chromeCmd = getChromeCommand();
|
|
362
|
+
console.log(` ${chromeCmd} --remote-debugging-port=9222`);
|
|
363
|
+
console.log('');
|
|
364
|
+
log('info', '.mcp.json 已配置,Chrome 就绪后 MCP 会自动连接 (autoConnect)');
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async function auth(url) {
|
|
369
|
+
assets.ensureDirs();
|
|
370
|
+
const config = loadConfig();
|
|
371
|
+
const tool = config.webTestTool;
|
|
372
|
+
|
|
373
|
+
if (!tool) {
|
|
374
|
+
log('error', '未配置浏览器测试工具');
|
|
375
|
+
log('info', '请先运行 claude-coder setup 选择 Playwright MCP 或 Chrome DevTools MCP');
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (tool === 'chrome-devtools') {
|
|
380
|
+
const [major, minor] = process.versions.node.split('.').map(Number);
|
|
381
|
+
if (major < 20 || (major === 20 && minor < 19)) {
|
|
382
|
+
log('warn', `当前 Node.js 版本 v${process.versions.node},Chrome DevTools MCP 要求 v20.19+`);
|
|
383
|
+
log('info', 'nvm 用户请执行: nvm alias default 22 && nvm use 22');
|
|
384
|
+
log('info', '升级后重新运行此命令');
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const targetUrl = normalizeUrl(url) || null;
|
|
388
|
+
log('info', '浏览器工具: Chrome DevTools MCP');
|
|
389
|
+
if (targetUrl) log('info', `目标 URL: ${targetUrl}`);
|
|
390
|
+
console.log('');
|
|
391
|
+
await authChromeDevTools(targetUrl);
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Playwright MCP
|
|
396
|
+
const mode = config.webTestMode;
|
|
397
|
+
const targetUrl = normalizeUrl(url) || 'http://localhost:3000';
|
|
398
|
+
|
|
399
|
+
log('info', `浏览器工具: Playwright MCP (${mode} 模式)`);
|
|
400
|
+
log('info', `目标 URL: ${targetUrl}`);
|
|
401
|
+
console.log('');
|
|
402
|
+
|
|
403
|
+
switch (mode) {
|
|
404
|
+
case 'persistent':
|
|
405
|
+
await authPersistent(targetUrl);
|
|
406
|
+
break;
|
|
407
|
+
case 'isolated':
|
|
408
|
+
await authIsolated(targetUrl);
|
|
409
|
+
break;
|
|
410
|
+
case 'extension':
|
|
411
|
+
authExtension();
|
|
412
|
+
break;
|
|
413
|
+
default:
|
|
414
|
+
log('error', `未知的 Playwright 模式: ${mode}`);
|
|
415
|
+
log('info', '请运行 claude-coder setup 重新配置');
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
module.exports = { auth, updateMcpConfig };
|