Files
wrong-notebook/packaging/npm/bin/wrong-notebook.js
margrop 6529203585 fix(npm): 修复启动器参数解析、CLI 帮助与 Node 版本检查顺序
- 用 indexOf 切分键值,`--data-dir=/tmp/a=b/data` 不再被截断;未知参数、缺值与 1-65535 外端口给出明确错误并以非零码退出。
- 任意位置出现 `-h`/`--help` 只打印帮助,`start --help` 不再启动服务;`-v`/`--version` 同理。
- Node.js 22.15 校验提前到 prepare 最前面,低版本在触碰数据库前即报错。
- prepare-client 包裹异常并显式设置 cwd 与 PRISMA_HIDE_UPDATE_MESSAGE,失败不阻断安装。
- 补充 package-launcher 单测(等号路径、参数校验、CLI 帮助与退出码),lint/tsc/语法检查通过。
Refs: https://github.com/margrop/wrong-notebook/issues/156
2026-12-04 17:38:15 +08:00

80 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* wrong-notebook 命令行入口(npm 全局安装)
* wrong-notebook [start] [--port 3000] [--host 0.0.0.0] [--data-dir ~/.wrong-notebook]
* wrong-notebook prepare-client 生成本机 Prisma Client(postinstall 自动执行)
* wrong-notebook version | help
*/
const path = require('node:path');
const { spawnSync } = require('node:child_process');
const launcher = require('../launcher.js');
const pkg = require('../package.json');
const HELP = `错题本 wrong-notebook v${pkg.version}
用法:
wrong-notebook [start] [选项] 启动服务(默认监听 0.0.0.0:3000)
wrong-notebook version 显示版本
wrong-notebook help 显示帮助
选项:
-p, --port <端口> 监听端口,默认 3000(也可用环境变量 PORT)
-H, --host <地址> 监听地址,默认 0.0.0.0;仅本机访问可设为 127.0.0.1
-d, --data-dir <目录> 数据目录,默认 ~/.wrong-notebook(也可用环境变量 WRONG_NOTEBOOK_HOME)
数据库、配置文件(config/app-config.json)和会话密钥都保存在数据目录中,升级不会丢失数据。
首次启动时终端会打印一次性初始化链接,打开后设置管理员账号(链接只能使用一次)。`;
const raw = process.argv.slice(2);
const has = (...flags) => flags.some((flag) => raw.includes(flag));
// 任意位置出现 -h/--help 都只打印帮助,不启动服务(如 start --help、--port 8080 --help)
if (has('-h', '--help')) {
console.log(HELP);
} else if (has('-v', '--version')) {
console.log(pkg.version);
} else {
const [first, ...rest] = raw;
const command = !first || first.startsWith('-') ? 'start' : first;
const args = command === 'start' && first !== 'start' ? raw : rest;
switch (command) {
case 'start':
try {
launcher.start(launcher.parseArgs(args));
} catch (error) {
console.error(`[wrong-notebook] 启动失败:${error.message}`);
process.exit(1);
}
break;
case 'prepare-client': {
// 安装时生成 Prisma Client;失败不阻断安装,首次启动时会再尝试
try {
const packageRoot = path.join(__dirname, '..');
const cli = require.resolve('prisma/build/index.js', { paths: [packageRoot] });
const schema = path.join(packageRoot, 'prisma', 'schema.prisma');
const result = spawnSync(process.execPath, [cli, 'generate', '--schema', schema], {
cwd: packageRoot,
env: { ...process.env, PRISMA_HIDE_UPDATE_MESSAGE: 'true' },
stdio: 'inherit',
});
if (result.error) throw result.error;
if (result.status !== 0) throw new Error(`Prisma CLI 退出码 ${result.status}`);
} catch (error) {
console.warn(`[wrong-notebook] Prisma Client 生成失败,首次启动时将重试:${error.message}`);
}
break;
}
case 'version':
console.log(pkg.version);
break;
case 'help':
console.log(HELP);
break;
default:
console.error(`未知命令:${command}\n\n${HELP}`);
process.exit(1);
}
}