From 7d3f75a6a67d93d0a69bf7bbe20af3410f428b5b Mon Sep 17 00:00:00 2001 From: margrop <1731827+margrop@users.noreply.github.com> Date: Fri, 4 Dec 2026 22:37:28 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E4=BF=AE=E6=AD=A3=20npm=20=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=8F=90=E7=A4=BA=E5=B9=B6=E8=A1=A5=E5=85=A8=E4=B8=AD?= =?UTF-8?q?=E8=8B=B1=E6=96=87=20README=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 监听成功前提示「正在启动服务」而非访问地址,避免端口冲突误导 - npm README 补全 --host/version/help 参数与英文说明 - 单测覆盖启动提示文案与地址映射 - Refs: https://github.com/margrop/wrong-notebook/issues/158 --- packaging/npm/README.md | 60 ++++++++++++++++++++- packaging/runtime/launcher.js | 11 ++-- src/__tests__/unit/package-launcher.test.ts | 8 +++ 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/packaging/npm/README.md b/packaging/npm/README.md index 60ac9979..508dbfca 100644 --- a/packaging/npm/README.md +++ b/packaging/npm/README.md @@ -10,12 +10,26 @@ ```bash npm install -g wrong-notebook -wrong-notebook # 默认监听 0.0.0.0:3000 -wrong-notebook --port 8080 --data-dir ~/wrong-notebook-data +wrong-notebook # 默认监听 0.0.0.0:3000 +wrong-notebook --port 8080 --host 127.0.0.1 --data-dir ~/wrong-notebook-data +wrong-notebook version # 显示版本 +wrong-notebook help # 显示帮助 ``` 浏览器打开 。首次启动时终端会打印一次性初始化链接(`/initialize?token=…`),打开后设置管理员邮箱和密码,再进入安装向导配置 AI 渠道并创建第一个错题本。 +### 命令行参数 + +`start` 可省略;`-h/--help` 与 `-v/--version` 在任意位置都只打印帮助或版本,不启动服务。 + +| 参数 | 说明 | +| --- | --- | +| `-p, --port <端口>` | 监听端口,默认 3000,也可用环境变量 `PORT` | +| `-H, --host <地址>` | 监听地址,默认 0.0.0.0;仅本机访问可设为 127.0.0.1,也可用环境变量 `HOSTNAME` | +| `-d, --data-dir <目录>` | 数据目录,默认 `~/.wrong-notebook`,也可用环境变量 `WRONG_NOTEBOOK_HOME` | +| `version` | 显示当前版本 | +| `help` | 显示用法与全部参数 | + ## 数据目录 默认 `~/.wrong-notebook`,可用 `--data-dir` 或环境变量 `WRONG_NOTEBOOK_HOME` 修改,其中包含: @@ -34,3 +48,45 @@ wrong-notebook --port 8080 --data-dir ~/wrong-notebook-data `PRISMA_ENGINES_MIRROR=https://registry.npmmirror.com/-/binary/prisma npm install -g wrong-notebook` - 使用 `--ignore-scripts` 安装时,首次启动会自动生成 Prisma Client,需要安装目录可写。 - 其余环境变量(如 `AI_PROVIDER`、`OPENAI_API_KEY`、`LOG_LEVEL`)与 Docker 部署相同,见仓库 `.env.example`。 + +## English + +wrong-notebook is an AI-powered mistake notebook: upload question photos or text, and AI recognizes, solves and tags them; it also supports notebook management, practice, statistics, printing and export. + +Project home: . English documentation: [README.en.md](https://github.com/margrop/wrong-notebook/blob/main/README.en.md) and [doc/wiki/en](https://github.com/margrop/wrong-notebook/tree/main/doc/wiki/en). + +Requires Node.js 22.15 or newer. + +```bash +npm install -g wrong-notebook +wrong-notebook # listens on 0.0.0.0:3000 by default +wrong-notebook --port 8080 --host 127.0.0.1 --data-dir ~/wrong-notebook-data +wrong-notebook version # print the version +wrong-notebook help # print usage and all options +``` + +Open . On first start the terminal prints a one-time initialization link (`/initialize?token=…`); set the administrator email and password there, then finish the setup wizard to configure AI providers and create the first notebook. + +### CLI options + +The `start` command may be omitted; `-h/--help` and `-v/--version` print help or the version without starting the server, wherever they appear. + +| Option | Description | +| --- | --- | +| `-p, --port ` | Listen port, default 3000; also `PORT` | +| `-H, --host
` | Listen address, default 0.0.0.0; use 127.0.0.1 for local access only; also `HOSTNAME` | +| `-d, --data-dir ` | Data directory, default `~/.wrong-notebook`; also `WRONG_NOTEBOOK_HOME` | +| `version` | Print the current version | +| `help` | Print usage and all options | + +### Data directory + +Defaults to `~/.wrong-notebook`; change it with `--data-dir` or `WRONG_NOTEBOOK_HOME`. It contains `wrong-notebook.db` (SQLite database), `config/app-config.json` (settings including AI API keys) and `.nextauth_secret` (session secret generated on first start). + +Upgrade with `npm install -g wrong-notebook@latest`; database migrations run at startup and existing data is kept. + +### Notes + +- Installation downloads the Prisma database engine. Behind a slow network in China, set the mirror and reinstall: `PRISMA_ENGINES_MIRROR=https://registry.npmmirror.com/-/binary/prisma npm install -g wrong-notebook`. +- With `--ignore-scripts`, the first start generates Prisma Client itself; the install directory must be writable. +- Other environment variables (such as `AI_PROVIDER`, `OPENAI_API_KEY`, `LOG_LEVEL`) are the same as Docker deployment; see `.env.example` in the repository. diff --git a/packaging/runtime/launcher.js b/packaging/runtime/launcher.js index 417efee2..39cdc114 100644 --- a/packaging/runtime/launcher.js +++ b/packaging/runtime/launcher.js @@ -221,20 +221,25 @@ function prepare(options = {}) { return { dataDir, dbFile, env, version }; } +/** 启动提示:server.js 监听成功前只说明“正在启动”,避免端口被占用时误导用户以为服务已就绪 */ +function startupHint(host, port) { + const shownHost = host === '0.0.0.0' || host === '::' ? '127.0.0.1' : host; + return `[wrong-notebook] 正在启动服务,监听地址:http://${shownHost}:${port}`; +} + function start(options = {}) { const { dataDir, env, version } = prepare(options); const port = String(options.port || process.env.PORT || DEFAULT_PORT); const host = options.host || process.env.HOSTNAME || '0.0.0.0'; Object.assign(process.env, env, { PORT: port, HOSTNAME: host }); console.log(`[wrong-notebook] v${version} 数据目录:${dataDir}`); - const shownHost = host === '0.0.0.0' || host === '::' ? '127.0.0.1' : host; - console.log(`[wrong-notebook] 访问地址:http://${shownHost}:${port}`); + console.log(startupHint(host, port)); installExternalAliasResolver(); // standalone 的 server.js 会切换工作目录到自身所在目录并监听 PORT/HOSTNAME require(path.join(APP_DIR, 'server.js')); } -module.exports = { prepare, start, parseArgs, assertNodeVersion, defaultDataDir, sqliteUrl, ensureSecret }; +module.exports = { prepare, start, parseArgs, assertNodeVersion, defaultDataDir, sqliteUrl, ensureSecret, startupHint }; if (require.main === module) { try { diff --git a/src/__tests__/unit/package-launcher.test.ts b/src/__tests__/unit/package-launcher.test.ts index 76b47425..4b0e0bfc 100644 --- a/src/__tests__/unit/package-launcher.test.ts +++ b/src/__tests__/unit/package-launcher.test.ts @@ -15,6 +15,7 @@ const launcher = require('../../../packaging/runtime/launcher.js') as { assertNodeVersion: () => void; sqliteUrl: (file: string) => string; ensureSecret: (dataDir: string) => string; + startupHint: (host: string, port: string) => string; }; const binSource = path.resolve(__dirname, '../../../packaging/npm/bin/wrong-notebook.js'); const launcherSource = path.resolve(__dirname, '../../../packaging/runtime/launcher.js'); @@ -85,6 +86,13 @@ describe('package launcher', () => { } }); + it('启动提示只说明正在监听,不宣称服务已就绪', () => { + expect(launcher.startupHint('0.0.0.0', '3000')).toBe('[wrong-notebook] 正在启动服务,监听地址:http://127.0.0.1:3000'); + expect(launcher.startupHint('::', '8080')).toContain('127.0.0.1:8080'); + expect(launcher.startupHint('localhost', '3000')).toContain('localhost:3000'); + expect(launcher.startupHint('0.0.0.0', '3000')).not.toContain('访问地址'); + }); + it('SQLite 地址使用正斜杠', () => { const file = ['data', 'wrong-notebook.db'].join(path.sep); expect(launcher.sqliteUrl(file)).toBe('file:data/wrong-notebook.db');