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 CHANGED
@@ -17,11 +17,13 @@ Let product managers run "requirements → PRD → HTML prototype" as a single c
17
17
 
18
18
  </div>
19
19
 
20
- <p align="center">
21
- <img src="image/README/workbench-overview.png" alt="openprototype workbench preview" width="1100">
22
- </p>
20
+ <table>
21
+ <tr>
22
+ <td><img src="image/README/workbench-overview.png" alt="openprototype workbench preview" width="440"></td>
23
+ <td><img src="image/README/workbench-prd.png" alt="openprototype workbench prd preview" width="440"></td>
24
+ </tr>
25
+ </table>
23
26
 
24
- ---
25
27
 
26
28
  ## Why it exists
27
29
 
package/README.zh-CN.md CHANGED
@@ -17,11 +17,13 @@
17
17
 
18
18
  </div>
19
19
 
20
- <p align="center">
21
- <img src="image/README/workbench-overview.png" alt="openprototype 工作台预览" width="1100">
22
- </p>
20
+ <table>
21
+ <tr>
22
+ <td><img src="image/README/workbench-overview.png" alt="openprototype workbench preview" width="440"></td>
23
+ <td><img src="image/README/workbench-prd.png" alt="openprototype workbench prd preview" width="440"></td>
24
+ </tr>
25
+ </table>
23
26
 
24
- ---
25
27
 
26
28
  ## 为什么要有它
27
29
 
package/bin/cli.js CHANGED
@@ -8,6 +8,7 @@
8
8
  * openprototype init 把框架植入当前已有项目(场景②,非破坏式)
9
9
  * openprototype add-product <id> 新增一个产品壳(默认 pc)
10
10
  * openprototype serve 启动本地服务器
11
+ * openprototype service <action> 管理本地常驻服务
11
12
  * openprototype check [--changed] 跑自动化检查
12
13
  * openprototype nav:sync 重建各产品 nav-tree.json
13
14
  * openprototype doctor 体检:Node / OpenCode / 配置 / Playwright
@@ -18,6 +19,16 @@ const fs = require('fs');
18
19
  const path = require('path');
19
20
  const { spawnSync } = require('child_process');
20
21
  const { detectOpenCodeBin, CONFIG_FILENAME, DEFAULTS } = require('../lib/config');
22
+ const {
23
+ installService,
24
+ startService,
25
+ stopService,
26
+ uninstallService,
27
+ pruneServices,
28
+ serviceStatus,
29
+ readLog,
30
+ isSupportedPlatform
31
+ } = require('../lib/service-manager');
21
32
 
22
33
  const PKG_ROOT = path.resolve(__dirname, '..');
23
34
  const TPL = path.join(PKG_ROOT, 'templates');
@@ -135,6 +146,7 @@ function cmdCreate(argv) {
135
146
  'check:changed': 'openprototype check --changed',
136
147
  'nav:sync': 'openprototype nav:sync'
137
148
  },
149
+ allowScripts: { openprototype: true },
138
150
  dependencies: { 'openprototype': kitDependencySpec() }
139
151
  };
140
152
  fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(pkg, null, 2) + '\n');
@@ -147,13 +159,15 @@ function cmdCreate(argv) {
147
159
  info(C.g('\n✔ 项目已创建。下一步:\n'));
148
160
  info(` cd ${dir}`);
149
161
  info(' npm install');
150
- info(' npm run serve ' + C.dim('# 打开 http://127.0.0.1:8082/product/demo/pc/index.html'));
162
+ info(' ' + C.dim('# npm 安装后会自动启动常驻服务'));
163
+ info(' 浏览器打开 http://127.0.0.1:8082/product/demo/pc/index.html');
151
164
  info('\n ' + C.dim('(右侧 AI 面板需要本机安装 OpenCode,npm install 后运行 npx openprototype doctor 检查)\n'));
152
165
  }
153
166
 
154
167
  // ── 命令:init(植入已有项目) ──────────────────────────
155
- function cmdInit() {
168
+ async function cmdInit() {
156
169
  const root = process.cwd();
170
+ let serviceInstallDenied = false;
157
171
  info(C.b('\n把 openprototype 植入当前项目(非破坏式)…\n'));
158
172
 
159
173
  copyIfAbsent(path.join(TPL, 'skills'), path.join(root, 'skills'));
@@ -176,16 +190,45 @@ function cmdInit() {
176
190
  'nav:sync': 'openprototype nav:sync'
177
191
  };
178
192
  let added = 0;
193
+ let allowedInstallScript = false;
179
194
  for (const [k, v] of Object.entries(want)) {
180
195
  if (!pkg.scripts[k]) { pkg.scripts[k] = v; added++; }
181
196
  else info(C.dim(` 保留你已有的 scripts.${k}`));
182
197
  }
198
+ if (pkg.allowScripts === undefined) pkg.allowScripts = {};
199
+ if (pkg.allowScripts && typeof pkg.allowScripts === 'object' && pkg.allowScripts.openprototype === false) {
200
+ serviceInstallDenied = true;
201
+ }
202
+ if (pkg.allowScripts && typeof pkg.allowScripts === 'object' && pkg.allowScripts.openprototype === undefined) {
203
+ pkg.allowScripts.openprototype = true;
204
+ allowedInstallScript = true;
205
+ }
183
206
  fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
184
207
  ok(`package.json 脚本已合并(新增 ${added} 条)`);
208
+ if (allowedInstallScript) ok('已允许 openprototype 运行 npm 安装钩子');
185
209
  } else {
186
210
  info(C.y(' 未发现 package.json,跳过脚本合并(可手动 npm init)'));
187
211
  }
188
212
 
213
+ const envServicePolicy = String(process.env.OPENPROTOTYPE_SERVICE_AUTO_INSTALL || '').toLowerCase();
214
+ const envServiceDisabled = ['0', 'false', 'off'].includes(envServicePolicy);
215
+ if (isSupportedPlatform() && !serviceInstallDenied && !envServiceDisabled) {
216
+ const config = loadOrInitConfig(root);
217
+ if (!config.service || config.service.autoInstall !== false) {
218
+ try {
219
+ const result = await installService({ projectRoot: root, explicit: false });
220
+ if (!result.skipped) ok(`常驻服务已启动:http://127.0.0.1:${config.port}`);
221
+ } catch (err) {
222
+ info(C.y(`! 常驻服务未能自动启动:${err.message}`));
223
+ info(C.dim(' 可稍后运行 `npx openprototype service install` 重试。'));
224
+ }
225
+ } else {
226
+ info(C.dim(' 已按 service.autoInstall=false 跳过常驻服务。'));
227
+ }
228
+ } else if (serviceInstallDenied || envServiceDisabled) {
229
+ info(C.dim(' 已按安装脚本策略跳过常驻服务。'));
230
+ }
231
+
189
232
  info(C.g('\n✔ 已植入。运行 `npx openprototype add-product <id>` 建第一个产品壳。\n'));
190
233
  }
191
234
 
@@ -218,8 +261,87 @@ function runNode(scriptRel, extraArgs) {
218
261
  process.exit(r.status || 0);
219
262
  }
220
263
 
264
+ function printServiceStatus(status) {
265
+ const labels = {
266
+ running: C.g('运行中'),
267
+ stopped: C.y('已注册,当前停止'),
268
+ 'port-conflict': C.r('端口被其他进程占用'),
269
+ stale: C.y('注册已失效'),
270
+ unregistered: C.dim('未安装')
271
+ };
272
+ info(`状态: ${labels[status.state] || status.state}`);
273
+ info(`项目: ${status.projectRoot}`);
274
+ info(`服务 ID: ${status.serviceId}`);
275
+ info(`地址: http://127.0.0.1:${status.port}`);
276
+ if (status.pid) info(`PID: ${status.pid}`);
277
+ if (status.version) info(`版本: ${status.version}`);
278
+ if (status.startedAt) info(`启动时间: ${status.startedAt}`);
279
+ if (status.configDrift || status.versionDrift || status.nodeDrift) {
280
+ info(C.y('配置、包版本或 Node 路径已变化,请运行 openprototype service restart'));
281
+ }
282
+ info(`日志: ${status.logPath}`);
283
+ }
284
+
285
+ async function cmdService(argv) {
286
+ const action = argv[0] || 'status';
287
+ const json = argv.includes('--json');
288
+ try {
289
+ if (action === 'install' || action === 'restart') {
290
+ const result = await installService({
291
+ projectRoot: process.cwd(),
292
+ allowLan: argv.includes('--allow-lan'),
293
+ explicit: true
294
+ });
295
+ const status = await serviceStatus(process.cwd());
296
+ if (json) info(JSON.stringify(status, null, 2));
297
+ else { ok(action === 'restart' ? '常驻服务已重启' : '常驻服务已安装并启动'); printServiceStatus(status); }
298
+ return result;
299
+ }
300
+ if (action === 'start') {
301
+ const status = await startService(process.cwd());
302
+ if (json) info(JSON.stringify(status, null, 2));
303
+ else { ok('常驻服务已启动'); printServiceStatus(status); }
304
+ return;
305
+ }
306
+ if (action === 'stop') {
307
+ const result = stopService(process.cwd());
308
+ if (json) info(JSON.stringify(result, null, 2));
309
+ else ok('常驻服务已停止;下次登录仍会自动启动,永久取消请运行 service uninstall');
310
+ return;
311
+ }
312
+ if (action === 'uninstall') {
313
+ const result = uninstallService(process.cwd());
314
+ if (json) info(JSON.stringify(result, null, 2));
315
+ else { ok('常驻服务注册已删除'); info(C.dim(`日志保留在 ${result.logPath}`)); }
316
+ return;
317
+ }
318
+ if (action === 'prune') {
319
+ const removed = pruneServices();
320
+ if (json) info(JSON.stringify({ removed }, null, 2));
321
+ else if (removed.length) ok(`已清理 ${removed.length} 个失效服务注册`);
322
+ else info(C.dim('没有需要清理的失效服务。'));
323
+ return;
324
+ }
325
+ if (action === 'logs') {
326
+ const log = readLog(process.cwd(), 100);
327
+ if (json) info(JSON.stringify(log, null, 2));
328
+ else { info(C.dim(`日志:${log.path}\n`)); info(log.content || C.dim('暂无日志。')); }
329
+ return;
330
+ }
331
+ if (action === 'status') {
332
+ const status = await serviceStatus(process.cwd());
333
+ if (json) info(JSON.stringify(status, null, 2));
334
+ else printServiceStatus(status);
335
+ return;
336
+ }
337
+ die('用法:openprototype service <install|start|stop|restart|status|logs|uninstall|prune>');
338
+ } catch (err) {
339
+ die(err.message);
340
+ }
341
+ }
342
+
221
343
  // ── 命令:doctor ────────────────────────────────────────
222
- function cmdDoctor() {
344
+ async function cmdDoctor() {
223
345
  info(C.b('\nopenprototype 体检\n'));
224
346
  let warn = 0;
225
347
 
@@ -243,6 +365,29 @@ function cmdDoctor() {
243
365
  try { require.resolve('playwright'); ok('Playwright 已安装(冒烟测试可用)'); }
244
366
  catch { console.log(C.y('! ') + 'Playwright 未安装(`npm i -D playwright && npx playwright install chromium` 后可跑冒烟测试)'); warn++; }
245
367
 
368
+ if (isSupportedPlatform() && fs.existsSync(cfg)) {
369
+ try {
370
+ const status = await serviceStatus(process.cwd());
371
+ if (status.running && !status.configDrift && !status.versionDrift && !status.nodeDrift) {
372
+ ok(`常驻服务运行中(PID ${status.pid},端口 ${status.port})`);
373
+ } else if (status.running) {
374
+ console.log(C.y('! ') + '常驻服务正在运行,但配置、包版本或 Node 路径已变化');
375
+ info(C.dim(' 应用更新:npx openprototype service restart'));
376
+ warn++;
377
+ }
378
+ else {
379
+ console.log(C.y('! ') + `常驻服务状态:${status.state}`);
380
+ info(C.dim(' 修复:npx openprototype service install'));
381
+ warn++;
382
+ }
383
+ } catch (err) {
384
+ console.log(C.y('! ') + `常驻服务检查失败:${err.message}`);
385
+ warn++;
386
+ }
387
+ } else if (!isSupportedPlatform()) {
388
+ info(C.dim(' 常驻服务:当前平台不支持(仅 macOS / Windows)'));
389
+ }
390
+
246
391
  info(warn ? C.y(`\n完成,${warn} 项需要注意。\n`) : C.g('\n全部正常。\n'));
247
392
  }
248
393
 
@@ -267,6 +412,7 @@ ${C.b('openprototype')} — 本地原型工作台脚手架
267
412
  ${C.g('openprototype init')} 把框架植入当前已有项目(非破坏式)
268
413
  ${C.g('openprototype add-product <id>')} 新增一个产品壳(默认 pc)
269
414
  ${C.g('openprototype serve')} 启动本地服务器
415
+ ${C.g('openprototype service <action>')} 管理常驻服务(install/start/stop/restart/status/logs/uninstall/prune)
270
416
  ${C.g('openprototype check [--changed]')} 自动化检查(静态红线 + 冒烟)
271
417
  ${C.g('openprototype nav:sync')} 重建各产品 nav-tree.json
272
418
  ${C.g('openprototype doctor')} 体检(Node / OpenCode / 配置 / Playwright)
@@ -274,13 +420,14 @@ ${C.b('openprototype')} — 本地原型工作台脚手架
274
420
  `);
275
421
  }
276
422
 
277
- function main() {
423
+ async function main() {
278
424
  const [cmd, ...argv] = process.argv.slice(2);
279
425
  switch (cmd) {
280
426
  case 'create': return cmdCreate(argv);
281
427
  case 'init': return cmdInit();
282
428
  case 'add-product': return cmdAddProduct(argv);
283
- case 'serve': return runNode('runtime/server.js', []);
429
+ case 'serve': return runNode('runtime/server.js', argv);
430
+ case 'service': return cmdService(argv);
284
431
  case 'check': return runNode('scripts/check/run-checks.js', argv);
285
432
  case 'nav:sync':
286
433
  case 'nav-sync': return runNode('scripts/check/sync-nav-tree.js', argv);
@@ -294,4 +441,4 @@ function main() {
294
441
  }
295
442
  }
296
443
 
297
- main();
444
+ main().catch((err) => die(err.message));
package/lib/config.js CHANGED
@@ -18,6 +18,9 @@ const DEFAULTS = {
18
18
  // 服务器监听地址。默认只绑本机;需要给局域网同事演示时改成 '0.0.0.0'
19
19
  // (写入 / Agent 类 /api/* 接口始终只接受本机请求,不随该项放开)。
20
20
  host: '127.0.0.1',
21
+ service: {
22
+ autoInstall: true
23
+ },
21
24
  opencode: {
22
25
  bin: 'auto',
23
26
  model: 'deepseek/deepseek-v4-flash',
@@ -144,6 +147,9 @@ function loadConfig(startDir) {
144
147
  hasConfigFile: fs.existsSync(path.join(rootDir, CONFIG_FILENAME)),
145
148
  port: merged.port,
146
149
  host: merged.host,
150
+ service: {
151
+ autoInstall: !merged.service || merged.service.autoInstall !== false
152
+ },
147
153
  opencode: {
148
154
  bin: resolvedBin, // null 表示没找到
149
155
  binConfigured: merged.opencode.bin,