diff --git a/package.json b/package.json index f30e3f0..501aa78 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "scripts": { "typecheck": "tsc", "start": "node src/server.ts", - "dev": "node --watch-path=./src --watch-path=./sdk --watch-path=./sources --watch --watch-preserve-output src/server.ts", + "dev": "node scripts/dev.mjs", "test": "pnpm run typecheck && node test/source-simple-test.ts && node test/smoke-test.ts" }, "dependencies": { diff --git a/scripts/dev.mjs b/scripts/dev.mjs new file mode 100644 index 0000000..23a5240 --- /dev/null +++ b/scripts/dev.mjs @@ -0,0 +1,29 @@ +// pnpm dev 的包装:node --watch-path 指向不存在的目录会直接 ENOENT 崩溃。 +// sources/ 不入库(第三方音源脚本,gitignore 整目录),新 clone 的仓库里没有它, +// 所以这里只为实际存在的目录注册 watch。 +import { existsSync } from 'node:fs' +import { spawn } from 'node:child_process' +import { dirname, join, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' + +const root = resolve(dirname(fileURLToPath(import.meta.url)), '..') + +const watchTargets = ['src', 'sdk', 'sources'] + .map((dir) => join(root, dir)) + .filter((dir) => existsSync(dir)) + +const child = spawn( + process.execPath, + [ + ...watchTargets.flatMap((dir) => ['--watch-path', dir]), + '--watch', + '--watch-preserve-output', + join(root, 'src/server.ts'), + ], + { stdio: 'inherit', cwd: root }, +) + +child.on('exit', (code, signal) => { + if (signal) process.kill(process.pid, signal) + else process.exit(code ?? 1) +})