openprototype 1.0.1 → 1.0.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 +6 -4
- package/README.zh-CN.md +6 -4
- package/bin/cli.js +153 -6
- package/lib/config.js +6 -0
- package/lib/service-manager.js +452 -0
- package/package.json +3 -1
- package/product/demo/pc/index.html +180 -9
- package/product/demo/pc/nav-tree.json +4 -4
- package/proto-kit.config.json +3 -0
- package/runtime/server.js +81 -13
- package/runtime/service-runner.js +138 -0
- package/runtime/shared/common/prd-panel.js +55 -5
- package/runtime/shell/agent-panel.css +65 -0
- package/runtime/shell/agent-panel.js +62 -20
- package/scripts/check/service-smoke-test.js +172 -0
- package/scripts/check/sync-nav-tree.js +29 -1
- package/scripts/postinstall.js +52 -0
- package/templates/product-shell/index.html +180 -9
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
// 页面可在引入本模块前声明这些全局以覆盖默认行为;未声明时使用默认值。
|
|
7
|
-
// PRD_OVERVIEW_VERSION
|
|
7
|
+
// “当前版本”优先读取主 PRD 历史记录中的最大版本;PRD_OVERVIEW_VERSION 仅作兼容兜底。
|
|
8
|
+
// PRD_WORKSPACE_MAP 配置多文档工作区。
|
|
8
9
|
var PRD_BASE_PATH = typeof PRD_BASE_PATH !== 'undefined' ? PRD_BASE_PATH : './';
|
|
9
10
|
var PRD_FILE_MAP = typeof PRD_FILE_MAP !== 'undefined' ? PRD_FILE_MAP : {};
|
|
10
11
|
var PRD_CACHE = typeof PRD_CACHE !== 'undefined' ? PRD_CACHE : {};
|
|
@@ -185,6 +186,11 @@ function ensurePrdPanelDom() {
|
|
|
185
186
|
document.body.appendChild(prdFloatBtn);
|
|
186
187
|
}
|
|
187
188
|
|
|
189
|
+
var legacyPrdBtn = document.getElementById('openPrd');
|
|
190
|
+
if (legacyPrdBtn) {
|
|
191
|
+
legacyPrdBtn.style.display = 'none';
|
|
192
|
+
}
|
|
193
|
+
|
|
188
194
|
var prdPanel = document.getElementById('prdPanel');
|
|
189
195
|
if (!prdPanel) {
|
|
190
196
|
prdPanel = document.createElement('div');
|
|
@@ -212,7 +218,10 @@ function initPrdPanel() {
|
|
|
212
218
|
|
|
213
219
|
var prdFloatBtn = document.getElementById('prdFloatBtn');
|
|
214
220
|
if (prdFloatBtn) {
|
|
215
|
-
prdFloatBtn.addEventListener('click',
|
|
221
|
+
prdFloatBtn.addEventListener('click', function(event) {
|
|
222
|
+
event.stopPropagation();
|
|
223
|
+
togglePrdPanel();
|
|
224
|
+
});
|
|
216
225
|
}
|
|
217
226
|
|
|
218
227
|
var prdPanelCloseBtn = document.getElementById('prdPanelCloseBtn');
|
|
@@ -331,6 +340,7 @@ var prdWorkspaceTabs = [];
|
|
|
331
340
|
var prdEditMode = false;
|
|
332
341
|
var prdWorkspaceRootPath = '';
|
|
333
342
|
var prdOverviewMarkdown = '';
|
|
343
|
+
var prdOverviewVersion = '';
|
|
334
344
|
var prdOverviewBuildToken = 0;
|
|
335
345
|
var PRD_OVERVIEW_PATH = './__openprototype_current_version__.md';
|
|
336
346
|
|
|
@@ -424,7 +434,42 @@ function getPrdDisplayName(filePath) {
|
|
|
424
434
|
return (filePath || '').split('/').pop().replace(/\.md(?:#.*)?$/i, '').replace(/_/g, ' ');
|
|
425
435
|
}
|
|
426
436
|
|
|
427
|
-
function
|
|
437
|
+
function comparePrdVersions(left, right) {
|
|
438
|
+
var leftParts = String(left || '').replace(/^v/i, '').split('.');
|
|
439
|
+
var rightParts = String(right || '').replace(/^v/i, '').split('.');
|
|
440
|
+
var length = Math.max(leftParts.length, rightParts.length);
|
|
441
|
+
|
|
442
|
+
for (var index = 0; index < length; index += 1) {
|
|
443
|
+
var leftPart = leftParts[index] || '0';
|
|
444
|
+
var rightPart = rightParts[index] || '0';
|
|
445
|
+
var leftNumber = /^\d+$/.test(leftPart) ? Number(leftPart) : null;
|
|
446
|
+
var rightNumber = /^\d+$/.test(rightPart) ? Number(rightPart) : null;
|
|
447
|
+
var comparison = leftNumber !== null && rightNumber !== null
|
|
448
|
+
? leftNumber - rightNumber
|
|
449
|
+
: leftPart.localeCompare(rightPart, undefined, { numeric: true, sensitivity: 'base' });
|
|
450
|
+
if (comparison !== 0) return comparison;
|
|
451
|
+
}
|
|
452
|
+
return 0;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function getPrdHistoryMaxVersion(content) {
|
|
456
|
+
var plainText = String(content || '').replace(/<[^>]+>/g, '');
|
|
457
|
+
var pattern = /^\|\s*(v\d+(?:\.[0-9a-z]+)+)\s*\|/gim;
|
|
458
|
+
var versions = [];
|
|
459
|
+
var match;
|
|
460
|
+
while ((match = pattern.exec(plainText)) !== null) {
|
|
461
|
+
versions.push(match[1]);
|
|
462
|
+
}
|
|
463
|
+
if (!versions.length) return '';
|
|
464
|
+
return versions.reduce(function(maxVersion, version) {
|
|
465
|
+
return comparePrdVersions(version, maxVersion) > 0 ? version : maxVersion;
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function getPrdOverviewVersion(rootContent) {
|
|
470
|
+
var historyMaxVersion = getPrdHistoryMaxVersion(rootContent);
|
|
471
|
+
if (historyMaxVersion) return historyMaxVersion;
|
|
472
|
+
if (prdOverviewVersion) return prdOverviewVersion;
|
|
428
473
|
if (typeof PRD_OVERVIEW_VERSION === 'undefined') return '';
|
|
429
474
|
return String(PRD_OVERVIEW_VERSION || '').trim();
|
|
430
475
|
}
|
|
@@ -544,7 +589,7 @@ function buildPrdOverviewMarkdown(tabs, contents, version) {
|
|
|
544
589
|
}
|
|
545
590
|
|
|
546
591
|
function renderPrdOverview() {
|
|
547
|
-
var version =
|
|
592
|
+
var version = prdOverviewVersion;
|
|
548
593
|
var contentEl = document.getElementById('prdPanelContent');
|
|
549
594
|
var titleEl = document.getElementById('prdPanelTitle');
|
|
550
595
|
var fileNameEl = document.getElementById('prdPanelFileName');
|
|
@@ -567,13 +612,17 @@ function renderPrdOverview() {
|
|
|
567
612
|
|
|
568
613
|
function preparePrdOverview(rootPath, options) {
|
|
569
614
|
options = options || {};
|
|
570
|
-
var version = getPrdOverviewVersion();
|
|
571
615
|
var realTabs = prdWorkspaceTabs.filter(function(tab) {
|
|
572
616
|
return !isPrdOverviewPath(tab.path);
|
|
573
617
|
});
|
|
574
618
|
var contentOverrides = options.contentOverrides || {};
|
|
575
619
|
var buildToken = ++prdOverviewBuildToken;
|
|
576
620
|
var normalizedRootPath = normalizePrdPath(rootPath);
|
|
621
|
+
var rootContent = Object.prototype.hasOwnProperty.call(contentOverrides, normalizedRootPath)
|
|
622
|
+
? contentOverrides[normalizedRootPath]
|
|
623
|
+
: '';
|
|
624
|
+
var version = getPrdOverviewVersion(rootContent);
|
|
625
|
+
prdOverviewVersion = version;
|
|
577
626
|
|
|
578
627
|
if (!version || !realTabs.length) {
|
|
579
628
|
prdOverviewMarkdown = '';
|
|
@@ -842,6 +891,7 @@ function updatePrdContent() {
|
|
|
842
891
|
prdOverviewBuildToken += 1;
|
|
843
892
|
prdWorkspaceRootPath = '';
|
|
844
893
|
prdOverviewMarkdown = '';
|
|
894
|
+
prdOverviewVersion = '';
|
|
845
895
|
|
|
846
896
|
if (!prdFileName) {
|
|
847
897
|
prdContentEl.innerHTML = '<div class="error" style="padding:20px;color:var(--text-secondary);">当前页面暂无对应的PRD文档</div>';
|
|
@@ -342,6 +342,7 @@
|
|
|
342
342
|
width: 100%;
|
|
343
343
|
height: 28px;
|
|
344
344
|
margin-top: 6px;
|
|
345
|
+
padding: 0 8px;
|
|
345
346
|
border: 1px solid #d8dfe9;
|
|
346
347
|
border-radius: 4px;
|
|
347
348
|
background: #fff;
|
|
@@ -350,6 +351,70 @@
|
|
|
350
351
|
font-size: 10px;
|
|
351
352
|
}
|
|
352
353
|
|
|
354
|
+
.agent-question {
|
|
355
|
+
padding: 8px 0;
|
|
356
|
+
border-top: 1px solid #f0e4c4;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
.agent-request-title + .agent-question {
|
|
360
|
+
border-top: 0;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
.agent-question-header {
|
|
364
|
+
margin-bottom: 3px;
|
|
365
|
+
color: #9a6700;
|
|
366
|
+
font-size: 10px;
|
|
367
|
+
font-weight: 600;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
.agent-question-options {
|
|
371
|
+
display: grid;
|
|
372
|
+
gap: 5px;
|
|
373
|
+
margin-top: 7px;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
.agent-question-option {
|
|
377
|
+
display: flex;
|
|
378
|
+
gap: 7px;
|
|
379
|
+
align-items: flex-start;
|
|
380
|
+
padding: 7px 8px;
|
|
381
|
+
border: 1px solid #e1e6ee;
|
|
382
|
+
border-radius: 5px;
|
|
383
|
+
background: #fff;
|
|
384
|
+
cursor: pointer;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
.agent-question-option:has(.agent-question-choice:checked) {
|
|
388
|
+
border-color: #1677ff;
|
|
389
|
+
background: #f0f7ff;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
.agent-question-choice {
|
|
393
|
+
flex: 0 0 auto;
|
|
394
|
+
margin: 2px 0 0;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
.agent-question-option-label {
|
|
398
|
+
color: #20242d;
|
|
399
|
+
font-size: 10px;
|
|
400
|
+
font-weight: 600;
|
|
401
|
+
line-height: 1.4;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
.agent-question-option-desc {
|
|
405
|
+
margin-top: 1px;
|
|
406
|
+
color: #7b8492;
|
|
407
|
+
font-size: 9px;
|
|
408
|
+
line-height: 1.4;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
.agent-question-error {
|
|
412
|
+
min-height: 14px;
|
|
413
|
+
color: #d4380d;
|
|
414
|
+
font-size: 9px;
|
|
415
|
+
line-height: 1.4;
|
|
416
|
+
}
|
|
417
|
+
|
|
353
418
|
.agent-composer {
|
|
354
419
|
flex: 0 0 auto;
|
|
355
420
|
padding: 10px 12px 12px;
|
|
@@ -200,7 +200,10 @@
|
|
|
200
200
|
const dot = document.createElement('span');
|
|
201
201
|
dot.className = 'agent-tool-status';
|
|
202
202
|
tool.appendChild(dot);
|
|
203
|
-
|
|
203
|
+
const fallbackTitle = part.tool === 'question'
|
|
204
|
+
? (status === 'completed' ? '已收到回答' : status === 'error' ? '提问失败' : '等待你的回答')
|
|
205
|
+
: (part.tool || '执行工具');
|
|
206
|
+
appendText(tool, '', (part.state && part.state.title) || fallbackTitle);
|
|
204
207
|
wrapper.appendChild(tool);
|
|
205
208
|
});
|
|
206
209
|
return wrapper;
|
|
@@ -229,26 +232,56 @@
|
|
|
229
232
|
function renderQuestion(item) {
|
|
230
233
|
const card = document.createElement('div');
|
|
231
234
|
card.className = 'agent-request-card';
|
|
232
|
-
appendText(card, 'agent-request-title', 'OpenCode
|
|
235
|
+
appendText(card, 'agent-request-title', 'OpenCode 正在等待你的回答');
|
|
233
236
|
const questions = Array.isArray(item.questions) ? item.questions : [];
|
|
234
237
|
questions.forEach((question, index) => {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
238
|
+
const block = document.createElement('div');
|
|
239
|
+
block.className = 'agent-question';
|
|
240
|
+
block.dataset.questionIndex = String(index);
|
|
241
|
+
if (question.header) appendText(block, 'agent-question-header', question.header);
|
|
242
|
+
appendText(block, 'agent-request-desc', question.question || question.header || `问题 ${index + 1}`);
|
|
243
|
+
|
|
244
|
+
const options = Array.isArray(question.options) ? question.options : [];
|
|
245
|
+
if (options.length) {
|
|
246
|
+
const optionList = document.createElement('div');
|
|
247
|
+
optionList.className = 'agent-question-options';
|
|
248
|
+
options.forEach(option => {
|
|
249
|
+
const label = document.createElement('label');
|
|
250
|
+
label.className = 'agent-question-option';
|
|
251
|
+
const input = document.createElement('input');
|
|
252
|
+
input.type = question.multiple ? 'checkbox' : 'radio';
|
|
253
|
+
input.name = `agent-question-${item.id}-${index}`;
|
|
254
|
+
input.value = option.label || option.value || String(option);
|
|
255
|
+
input.className = 'agent-question-choice';
|
|
256
|
+
label.appendChild(input);
|
|
257
|
+
const content = document.createElement('span');
|
|
258
|
+
appendText(content, 'agent-question-option-label', input.value);
|
|
259
|
+
if (option.description) appendText(content, 'agent-question-option-desc', option.description);
|
|
260
|
+
label.appendChild(content);
|
|
261
|
+
optionList.appendChild(label);
|
|
245
262
|
});
|
|
246
|
-
|
|
247
|
-
field.type = 'text';
|
|
248
|
-
field.placeholder = '输入回答';
|
|
263
|
+
block.appendChild(optionList);
|
|
249
264
|
}
|
|
250
|
-
|
|
265
|
+
|
|
266
|
+
if (question.custom !== false) {
|
|
267
|
+
const custom = document.createElement('input');
|
|
268
|
+
custom.type = 'text';
|
|
269
|
+
custom.className = 'agent-question-field';
|
|
270
|
+
custom.placeholder = options.length ? '其他,请输入' : '输入回答';
|
|
271
|
+
if (!question.multiple) {
|
|
272
|
+
custom.addEventListener('input', () => {
|
|
273
|
+
if (!custom.value.trim()) return;
|
|
274
|
+
block.querySelectorAll('.agent-question-choice').forEach(choice => { choice.checked = false; });
|
|
275
|
+
});
|
|
276
|
+
block.querySelectorAll('.agent-question-choice').forEach(choice => {
|
|
277
|
+
choice.addEventListener('change', () => { if (choice.checked) custom.value = ''; });
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
block.appendChild(custom);
|
|
281
|
+
}
|
|
282
|
+
card.appendChild(block);
|
|
251
283
|
});
|
|
284
|
+
const error = appendText(card, 'agent-question-error', '');
|
|
252
285
|
const actions = document.createElement('div');
|
|
253
286
|
actions.className = 'agent-request-actions';
|
|
254
287
|
const submit = document.createElement('button');
|
|
@@ -256,7 +289,16 @@
|
|
|
256
289
|
submit.className = 'agent-request-btn is-primary';
|
|
257
290
|
submit.textContent = '提交回答';
|
|
258
291
|
submit.addEventListener('click', () => {
|
|
259
|
-
const answers = Array.from(card.querySelectorAll('.agent-question
|
|
292
|
+
const answers = Array.from(card.querySelectorAll('.agent-question')).map(block => {
|
|
293
|
+
const selected = Array.from(block.querySelectorAll('.agent-question-choice:checked')).map(choice => choice.value);
|
|
294
|
+
const custom = block.querySelector('.agent-question-field');
|
|
295
|
+
if (custom && custom.value.trim()) selected.push(custom.value.trim());
|
|
296
|
+
return selected;
|
|
297
|
+
});
|
|
298
|
+
if (answers.some(answer => !answer.length)) {
|
|
299
|
+
error.textContent = '请回答全部问题后再提交';
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
260
302
|
replyQuestion(item.id, answers);
|
|
261
303
|
});
|
|
262
304
|
actions.appendChild(submit);
|
|
@@ -280,9 +322,6 @@
|
|
|
280
322
|
|
|
281
323
|
function renderMessages() {
|
|
282
324
|
refs.messages.replaceChildren();
|
|
283
|
-
state.pending.permissions.forEach(item => refs.messages.appendChild(renderPermission(item)));
|
|
284
|
-
state.pending.questions.forEach(item => refs.messages.appendChild(renderQuestion(item)));
|
|
285
|
-
|
|
286
325
|
const diffCard = renderDiff();
|
|
287
326
|
if (diffCard) refs.messages.appendChild(diffCard);
|
|
288
327
|
|
|
@@ -299,6 +338,9 @@
|
|
|
299
338
|
if (message) refs.messages.appendChild(message);
|
|
300
339
|
}
|
|
301
340
|
|
|
341
|
+
state.pending.permissions.forEach(item => refs.messages.appendChild(renderPermission(item)));
|
|
342
|
+
state.pending.questions.forEach(item => refs.messages.appendChild(renderQuestion(item)));
|
|
343
|
+
|
|
302
344
|
if (!refs.messages.children.length) {
|
|
303
345
|
const empty = document.createElement('div');
|
|
304
346
|
empty.className = 'agent-empty';
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const net = require('net');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const { spawnSync } = require('child_process');
|
|
9
|
+
|
|
10
|
+
const PKG_ROOT = path.resolve(__dirname, '..', '..');
|
|
11
|
+
const NPM_CLI = process.platform === 'win32'
|
|
12
|
+
? (process.env.npm_execpath || path.join(path.dirname(process.execPath), 'node_modules', 'npm', 'bin', 'npm-cli.js'))
|
|
13
|
+
: null;
|
|
14
|
+
|
|
15
|
+
function run(command, args, cwd, options) {
|
|
16
|
+
const result = spawnSync(command, args, {
|
|
17
|
+
cwd,
|
|
18
|
+
encoding: 'utf8',
|
|
19
|
+
windowsHide: true,
|
|
20
|
+
env: Object.assign({}, process.env, options && options.env),
|
|
21
|
+
timeout: options && options.timeout ? options.timeout : 120000
|
|
22
|
+
});
|
|
23
|
+
if ((result.error || result.status !== 0) && !(options && options.allowFailure)) {
|
|
24
|
+
const spawnError = result.error ? `\n${result.error.stack || result.error.message}` : '';
|
|
25
|
+
throw new Error(`${command} ${args.join(' ')} 失败\n${result.stdout || ''}\n${result.stderr || ''}${spawnError}`);
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function npm(args, cwd, options) {
|
|
31
|
+
return process.platform === 'win32'
|
|
32
|
+
? run(process.execPath, [NPM_CLI, ...args], cwd, options)
|
|
33
|
+
: run('npm', args, cwd, options);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function freePort() {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const server = net.createServer();
|
|
39
|
+
server.once('error', reject);
|
|
40
|
+
server.listen(0, '127.0.0.1', () => {
|
|
41
|
+
const port = server.address().port;
|
|
42
|
+
server.close((err) => err ? reject(err) : resolve(port));
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function cliPath(projectRoot) {
|
|
48
|
+
return path.join(projectRoot, 'node_modules', 'openprototype', 'bin', 'cli.js');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function cli(projectRoot, args, options) {
|
|
52
|
+
return run(process.execPath, [cliPath(projectRoot), ...args], projectRoot, options);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function status(projectRoot) {
|
|
56
|
+
const result = cli(projectRoot, ['service', 'status', '--json']);
|
|
57
|
+
return JSON.parse(result.stdout);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function waitFor(projectRoot, predicate, timeoutMs) {
|
|
61
|
+
const deadline = Date.now() + timeoutMs;
|
|
62
|
+
let current;
|
|
63
|
+
while (Date.now() < deadline) {
|
|
64
|
+
try {
|
|
65
|
+
current = status(projectRoot);
|
|
66
|
+
if (predicate(current)) return current;
|
|
67
|
+
} catch {}
|
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
69
|
+
}
|
|
70
|
+
throw new Error(`等待服务状态超时:${JSON.stringify(current)}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function main() {
|
|
74
|
+
if (!['darwin', 'win32'].includes(process.platform)) {
|
|
75
|
+
console.log('当前平台不支持常驻服务,跳过。');
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'openprototype 服务测试 '));
|
|
80
|
+
const packDir = path.join(tempRoot, 'pack');
|
|
81
|
+
const projectRoot = path.join(tempRoot, '中文 project');
|
|
82
|
+
process.env.NPM_CONFIG_CACHE = process.env.NPM_CONFIG_CACHE || path.join(tempRoot, 'npm-cache');
|
|
83
|
+
fs.mkdirSync(packDir, { recursive: true });
|
|
84
|
+
fs.mkdirSync(projectRoot, { recursive: true });
|
|
85
|
+
|
|
86
|
+
let installed = false;
|
|
87
|
+
try {
|
|
88
|
+
const packed = npm(['pack', '--json', '--pack-destination', packDir], PKG_ROOT);
|
|
89
|
+
const packResult = JSON.parse(packed.stdout);
|
|
90
|
+
const packItem = Array.isArray(packResult) ? packResult[0] : (packResult.openprototype || Object.values(packResult)[0]);
|
|
91
|
+
if (!packItem || !packItem.filename) throw new Error(`无法解析 npm pack 输出:${packed.stdout}`);
|
|
92
|
+
const tarball = path.join(packDir, packItem.filename);
|
|
93
|
+
const dependencySpec = `file:${tarball.split(path.sep).join('/')}`;
|
|
94
|
+
const port = await freePort();
|
|
95
|
+
|
|
96
|
+
fs.writeFileSync(path.join(projectRoot, 'package.json'), JSON.stringify({
|
|
97
|
+
name: 'openprototype-service-smoke',
|
|
98
|
+
private: true,
|
|
99
|
+
version: '1.0.0',
|
|
100
|
+
// npm 12 对本地 tarball 按来源路径授权;正式 registry 安装使用包名授权。
|
|
101
|
+
allowScripts: { [dependencySpec]: true },
|
|
102
|
+
dependencies: { openprototype: dependencySpec }
|
|
103
|
+
}, null, 2) + '\n');
|
|
104
|
+
fs.writeFileSync(path.join(projectRoot, 'proto-kit.config.json'), JSON.stringify({
|
|
105
|
+
port,
|
|
106
|
+
host: '127.0.0.1',
|
|
107
|
+
service: { autoInstall: true },
|
|
108
|
+
products: []
|
|
109
|
+
}, null, 2) + '\n');
|
|
110
|
+
|
|
111
|
+
const installResult = npm(['install', '--foreground-scripts'], projectRoot, {
|
|
112
|
+
env: { OPENPROTOTYPE_SERVICE_AUTO_INSTALL: 'force' }
|
|
113
|
+
});
|
|
114
|
+
process.stdout.write(installResult.stdout || '');
|
|
115
|
+
process.stderr.write(installResult.stderr || '');
|
|
116
|
+
installed = true;
|
|
117
|
+
|
|
118
|
+
const initial = await waitFor(projectRoot, (item) => item.running, 20000);
|
|
119
|
+
if (!initial.registered || initial.port !== port || !initial.pid) throw new Error('自动安装后的服务状态不完整');
|
|
120
|
+
|
|
121
|
+
process.kill(initial.pid, 'SIGKILL');
|
|
122
|
+
const restarted = await waitFor(projectRoot, (item) => item.running && item.pid !== initial.pid, process.platform === 'win32' ? 90000 : 30000);
|
|
123
|
+
if (restarted.version !== require(path.join(PKG_ROOT, 'package.json')).version) throw new Error('服务版本不正确');
|
|
124
|
+
|
|
125
|
+
cli(projectRoot, ['service', 'stop', '--json']);
|
|
126
|
+
await waitFor(projectRoot, (item) => !item.running, 15000);
|
|
127
|
+
cli(projectRoot, ['service', 'start', '--json']);
|
|
128
|
+
await waitFor(projectRoot, (item) => item.running, 20000);
|
|
129
|
+
cli(projectRoot, ['service', 'restart', '--json']);
|
|
130
|
+
await waitFor(projectRoot, (item) => item.running, 20000);
|
|
131
|
+
|
|
132
|
+
const logResult = cli(projectRoot, ['service', 'logs', '--json']);
|
|
133
|
+
const log = JSON.parse(logResult.stdout);
|
|
134
|
+
if (!log.path || !log.content.includes('openprototype')) throw new Error('服务日志不可用');
|
|
135
|
+
|
|
136
|
+
cli(projectRoot, ['service', 'uninstall', '--json']);
|
|
137
|
+
installed = false;
|
|
138
|
+
const removed = await waitFor(projectRoot, (item) => !item.registered && !item.running, 15000);
|
|
139
|
+
if (removed.state !== 'unregistered') throw new Error('服务未完全卸载');
|
|
140
|
+
|
|
141
|
+
const configPath = path.join(projectRoot, 'proto-kit.config.json');
|
|
142
|
+
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
143
|
+
config.host = '0.0.0.0';
|
|
144
|
+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + '\n');
|
|
145
|
+
const lanResult = cli(projectRoot, ['service', 'install'], { allowFailure: true });
|
|
146
|
+
if (lanResult.status === 0 || !`${lanResult.stdout}\n${lanResult.stderr}`.includes('--allow-lan')) {
|
|
147
|
+
throw new Error('非回环 host 未被安全拦截');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
config.host = '127.0.0.1';
|
|
151
|
+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + '\n');
|
|
152
|
+
const blocker = net.createServer();
|
|
153
|
+
await new Promise((resolve, reject) => blocker.once('error', reject).listen(port, '127.0.0.1', resolve));
|
|
154
|
+
const conflictResult = cli(projectRoot, ['service', 'install'], { allowFailure: true });
|
|
155
|
+
await new Promise((resolve) => blocker.close(resolve));
|
|
156
|
+
if (conflictResult.status === 0 || !`${conflictResult.stdout}\n${conflictResult.stderr}`.includes('端口')) {
|
|
157
|
+
throw new Error('端口冲突未被安装前检查拦截');
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
console.log(`service smoke passed: ${process.platform}, port=${port}`);
|
|
161
|
+
} finally {
|
|
162
|
+
if (fs.existsSync(cliPath(projectRoot))) {
|
|
163
|
+
cli(projectRoot, ['service', 'uninstall', '--json'], { allowFailure: true });
|
|
164
|
+
}
|
|
165
|
+
fs.rmSync(tempRoot, { recursive: true, force: true });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
main().catch((err) => {
|
|
170
|
+
console.error(err.stack || err.message);
|
|
171
|
+
process.exit(1);
|
|
172
|
+
});
|
|
@@ -75,6 +75,34 @@ function sortTree(nodes) {
|
|
|
75
75
|
return nodes;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
function nodeKey(node) {
|
|
79
|
+
return node.file ? `file:${node.file}` : `folder:${node.name}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 保留 nav-tree.json 中的人工顺序,仅把磁盘新增节点按默认顺序追加到同级末尾。 */
|
|
83
|
+
function preserveTreeOrder(nodes, previousNodes) {
|
|
84
|
+
const currentByKey = new Map(nodes.map((node) => [nodeKey(node), node]));
|
|
85
|
+
const previousByKey = new Map((previousNodes || []).map((node) => [nodeKey(node), node]));
|
|
86
|
+
const ordered = [];
|
|
87
|
+
|
|
88
|
+
(previousNodes || []).forEach((previousNode) => {
|
|
89
|
+
const node = currentByKey.get(nodeKey(previousNode));
|
|
90
|
+
if (!node) return;
|
|
91
|
+
ordered.push(node);
|
|
92
|
+
currentByKey.delete(nodeKey(previousNode));
|
|
93
|
+
});
|
|
94
|
+
nodes.forEach((node) => {
|
|
95
|
+
if (currentByKey.has(nodeKey(node))) ordered.push(node);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
ordered.forEach((node) => {
|
|
99
|
+
if (!node.children) return;
|
|
100
|
+
const previousNode = previousByKey.get(nodeKey(node));
|
|
101
|
+
node.children = preserveTreeOrder(node.children, previousNode && previousNode.children);
|
|
102
|
+
});
|
|
103
|
+
return ordered;
|
|
104
|
+
}
|
|
105
|
+
|
|
78
106
|
function buildTree(paths, dirs) {
|
|
79
107
|
const tree = [];
|
|
80
108
|
// 先登记磁盘上的所有目录(含空目录,保住导航页刚创建、还没放页面的文件夹)
|
|
@@ -95,7 +123,7 @@ function syncTarget(target) {
|
|
|
95
123
|
const previous = collectRegisteredPaths(previousTree, new Set());
|
|
96
124
|
const { files: diskPaths, dirs } = listDiskPaths(target.dir);
|
|
97
125
|
const onDisk = new Set(diskPaths);
|
|
98
|
-
const tree = buildTree(diskPaths, dirs);
|
|
126
|
+
const tree = preserveTreeOrder(buildTree(diskPaths, dirs), previousTree);
|
|
99
127
|
const next = collectRegisteredPaths(tree, new Set());
|
|
100
128
|
const added = [...next].filter((p) => !previous.has(p));
|
|
101
129
|
const removed = [...previous].filter((p) => !onDisk.has(p));
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const { findProjectRoot, loadConfig } = require('../lib/config');
|
|
7
|
+
const { installService, isSupportedPlatform, isLoopbackHost } = require('../lib/service-manager');
|
|
8
|
+
|
|
9
|
+
const PKG_ROOT = path.resolve(__dirname, '..');
|
|
10
|
+
|
|
11
|
+
function isTruthy(value) {
|
|
12
|
+
return !!value && !['0', 'false', 'no', 'off'].includes(String(value).toLowerCase());
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isConsumerInstall(projectRoot) {
|
|
16
|
+
try {
|
|
17
|
+
const installedPackage = path.dirname(require.resolve('openprototype/package.json', { paths: [projectRoot] }));
|
|
18
|
+
return fs.realpathSync(installedPackage) === fs.realpathSync(PKG_ROOT) && fs.realpathSync(projectRoot) !== fs.realpathSync(PKG_ROOT);
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
(async () => {
|
|
25
|
+
const policy = String(process.env.OPENPROTOTYPE_SERVICE_AUTO_INSTALL || '').toLowerCase();
|
|
26
|
+
if (policy === '0' || policy === 'false' || policy === 'off') return;
|
|
27
|
+
if (!isSupportedPlatform()) return;
|
|
28
|
+
if (isTruthy(process.env.CI) && policy !== 'force') return;
|
|
29
|
+
|
|
30
|
+
const startDir = process.env.INIT_CWD || process.cwd();
|
|
31
|
+
const projectRoot = findProjectRoot(startDir);
|
|
32
|
+
const config = loadConfig(projectRoot);
|
|
33
|
+
if (!config.hasConfigFile || !isConsumerInstall(projectRoot)) return;
|
|
34
|
+
if (config.service && config.service.autoInstall === false) return;
|
|
35
|
+
if (!isLoopbackHost(config.host)) {
|
|
36
|
+
console.warn(`[openprototype] 已跳过常驻服务:host=${config.host} 不是本机回环地址。`);
|
|
37
|
+
console.warn('[openprototype] 如确认需要长期开放局域网访问,请运行:npx openprototype service install --allow-lan');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const result = await installService({ projectRoot, explicit: false });
|
|
43
|
+
if (!result.skipped) console.log(`[openprototype] 常驻服务已启动:http://127.0.0.1:${config.port}`);
|
|
44
|
+
} catch (err) {
|
|
45
|
+
console.warn(`[openprototype] 常驻服务自动安装失败:${err.message}`);
|
|
46
|
+
console.warn('[openprototype] npm 安装不受影响;可稍后运行:npx openprototype service install');
|
|
47
|
+
}
|
|
48
|
+
})().catch((err) => {
|
|
49
|
+
console.warn(`[openprototype] 常驻服务自动安装失败:${err.message}`);
|
|
50
|
+
process.exitCode = 0;
|
|
51
|
+
});
|
|
52
|
+
|