|
| 1 | +// 发布前校验 src/ 与 lib/ 一致性。 |
| 2 | +// |
| 3 | +// npm 包实际加载 lib/(package main → lib/index.js),而 root 层 `npm publish` |
| 4 | +// 不触发 dsh-mneme/ 的 prepack → sync 同步。历史教训:PR #60 只改了 src 忘了同步 |
| 5 | +// lib,v0.7.8 发出去的 npm 包跑的还是旧代码(issue #65)。此脚本让这类漂移在发布时 |
| 6 | +// 直接 fail,而不是带着旧产物上线。 |
| 7 | +// |
| 8 | +// 用法:node scripts/check-sync.js (root package.json 的 prepack 钩子自动调用) |
| 9 | +import { existsSync, readFileSync, readdirSync } from "node:fs"; |
| 10 | +import { join, relative } from "node:path"; |
| 11 | +import { fileURLToPath } from "node:url"; |
| 12 | + |
| 13 | +const root = join(fileURLToPath(new URL("..", import.meta.url))); // dsh-mneme/ |
| 14 | +const srcDir = join(root, "src"); |
| 15 | +const libDir = join(root, "lib"); |
| 16 | + |
| 17 | +function walk(dir) { |
| 18 | + const out = []; |
| 19 | + for (const entry of readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) { |
| 20 | + const full = join(dir, entry.name); |
| 21 | + if (entry.isDirectory()) out.push(...walk(full)); |
| 22 | + else if (entry.isFile()) out.push(full); |
| 23 | + } |
| 24 | + return out; |
| 25 | +} |
| 26 | + |
| 27 | +const bad = []; |
| 28 | +for (const file of walk(srcDir)) { |
| 29 | + const rel = relative(srcDir, file); |
| 30 | + const dest = join(libDir, rel); |
| 31 | + if (!existsSync(dest)) { |
| 32 | + bad.push(`missing lib/${rel}`); |
| 33 | + } else if (!readFileSync(file).equals(readFileSync(dest))) { |
| 34 | + bad.push(`differ lib/${rel}`); |
| 35 | + } |
| 36 | +} |
| 37 | +if (bad.length) { |
| 38 | + console.error(`✗ src/ 与 lib/ 不一致(${bad.length} 处)——npm 包实际加载 lib/,请先 npm run sync 并提交:`); |
| 39 | + for (const line of bad) console.error(` ${line}`); |
| 40 | + process.exit(1); |
| 41 | +} |
| 42 | +console.log(`✓ src/ 与 lib/ 一致(${walk(srcDir).length} 个文件)`); |
0 commit comments