merge: 修正 npm 启动提示并补全 README

This commit is contained in:
margrop
2026-12-04 22:41:51 +08:00
3 changed files with 74 additions and 5 deletions

View File

@@ -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 # 显示帮助
```
浏览器打开 <http://127.0.0.1:3000>。首次启动时终端会打印一次性初始化链接(`/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: <https://github.com/margrop/wrong-notebook>. 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 <http://127.0.0.1:3000>. 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 <port>` | Listen port, default 3000; also `PORT` |
| `-H, --host <address>` | Listen address, default 0.0.0.0; use 127.0.0.1 for local access only; also `HOSTNAME` |
| `-d, --data-dir <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.

View File

@@ -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 {

View File

@@ -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');