|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { credentialFlagDefs, GLOBAL_FLAGS, type AnyCommand } from "bailian-cli-core"; |
| 4 | +import { monorepoRoot } from "e2e/monorepo-root"; |
| 5 | +import { describe, expect, test } from "vite-plus/test"; |
| 6 | +import { commands } from "../src/commands.ts"; |
| 7 | + |
| 8 | +const repositoryRoot = monorepoRoot(); |
| 9 | +const installGuide = readFileSync(join(repositoryRoot, "INSTALL.md"), "utf8"); |
| 10 | +const cliPackage = JSON.parse( |
| 11 | + readFileSync(join(repositoryRoot, "packages/cli/package.json"), "utf8"), |
| 12 | +) as { |
| 13 | + engines?: { node?: string }; |
| 14 | +}; |
| 15 | + |
| 16 | +function toFlagName(key: string): string { |
| 17 | + return `--${key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`; |
| 18 | +} |
| 19 | + |
| 20 | +function findDocumentedCommand(snippet: string): { |
| 21 | + commandPath?: string; |
| 22 | + command?: AnyCommand; |
| 23 | +} { |
| 24 | + const argumentText = snippet.slice("bl ".length).trim(); |
| 25 | + const commandPath = Object.keys(commands) |
| 26 | + .sort((leftPath, rightPath) => rightPath.length - leftPath.length) |
| 27 | + .find((candidatePath) => { |
| 28 | + return argumentText === candidatePath || argumentText.startsWith(`${candidatePath} `); |
| 29 | + }); |
| 30 | + |
| 31 | + return commandPath ? { commandPath, command: commands[commandPath] } : {}; |
| 32 | +} |
| 33 | + |
| 34 | +function documentedCommandSnippets(): string[] { |
| 35 | + const fencedCommands = installGuide |
| 36 | + .split("\n") |
| 37 | + .map((line) => line.trim()) |
| 38 | + .filter((line) => line.startsWith("bl ")); |
| 39 | + const inlineCommands = Array.from(installGuide.matchAll(/`(bl [^`\n]+)`/g), (match) => match[1]); |
| 40 | + return [...new Set([...fencedCommands, ...inlineCommands])]; |
| 41 | +} |
| 42 | + |
| 43 | +describe("INSTALL.md", () => { |
| 44 | + test("发布包 Node.js 要求与安装文档一致", () => { |
| 45 | + const nodeEngine = cliPackage.engines?.node; |
| 46 | + expect(nodeEngine).toMatch(/^>=\d+\.\d+\.\d+$/); |
| 47 | + expect(installGuide).toContain(`要求 **≥ ${nodeEngine?.slice(2)}**`); |
| 48 | + }); |
| 49 | + |
| 50 | + test("示例只使用当前命令支持的 flags", () => { |
| 51 | + for (const snippet of documentedCommandSnippets()) { |
| 52 | + const { commandPath, command } = findDocumentedCommand(snippet); |
| 53 | + const argumentText = snippet.slice("bl ".length).trim(); |
| 54 | + |
| 55 | + if (!commandPath || !command) { |
| 56 | + expect(argumentText, `INSTALL.md 中存在未知命令:${snippet}`).toMatch(/^--/); |
| 57 | + } |
| 58 | + |
| 59 | + const supportedFlags = { |
| 60 | + ...GLOBAL_FLAGS, |
| 61 | + ...(command ? credentialFlagDefs(command) : {}), |
| 62 | + ...command?.flags, |
| 63 | + }; |
| 64 | + const supportedFlagNames = new Set(Object.keys(supportedFlags).map(toFlagName)); |
| 65 | + const usedFlagNames = Array.from(snippet.matchAll(/--[a-z0-9-]+/g), (match) => match[0]); |
| 66 | + const unsupportedFlagNames = usedFlagNames.filter( |
| 67 | + (flagName) => !supportedFlagNames.has(flagName), |
| 68 | + ); |
| 69 | + |
| 70 | + expect(unsupportedFlagNames, `INSTALL.md 命令使用了未声明的 flag:${snippet}`).toEqual([]); |
| 71 | + } |
| 72 | + }); |
| 73 | +}); |
0 commit comments