|
| 1 | +#!npx ts-node |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2025 Arm Limited |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import { execFile } from "child_process"; |
| 20 | +import { promisify } from "util"; |
| 21 | +import { resolve } from "path"; |
| 22 | +import yargs from "yargs"; |
| 23 | +import { hideBin } from "yargs/helpers"; |
| 24 | + |
| 25 | +const execFileAsync = promisify(execFile); |
| 26 | + |
| 27 | +async function main() { |
| 28 | + const argv = yargs(hideBin(process.argv)) |
| 29 | + .option("config", { |
| 30 | + alias: "c", |
| 31 | + type: "string", |
| 32 | + description: "Path to markdown-link-check config file" |
| 33 | + }) |
| 34 | + .option("ignore", { |
| 35 | + alias: "i", |
| 36 | + type: "array", |
| 37 | + description: "Directories to ignore", |
| 38 | + default: ["node_modules/**"], |
| 39 | + }) |
| 40 | + .help() |
| 41 | + .alias("help", "h") |
| 42 | + .parseSync(); |
| 43 | + |
| 44 | + const { globby } = await import("globby"); |
| 45 | + const ignorePatterns = (argv.ignore as string[]).map((pattern) => `!${pattern}`); |
| 46 | + const configPath = resolve(argv.config as string); |
| 47 | + const mdFiles = await globby(["**/*.md", ...ignorePatterns]); |
| 48 | + |
| 49 | + if (mdFiles.length === 0) { |
| 50 | + console.log("No markdown files found."); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + console.log(`Checking ${mdFiles.length} markdown file(s)...`); |
| 55 | + for (const file of mdFiles) { |
| 56 | + try { |
| 57 | + const { stdout } = await execFileAsync( |
| 58 | + "npx", ["markdown-link-check", "-v", "-c", configPath, file], { shell: true } |
| 59 | + ); |
| 60 | + console.log(stdout); |
| 61 | + } catch (err: any) { |
| 62 | + console.error(`Error in file: ${file}`); |
| 63 | + console.error(err.stdout || err.message); |
| 64 | + process.exitCode = 1; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +main().catch((error) => { |
| 70 | + console.error(error); |
| 71 | + process.exit(1); |
| 72 | +}); |
0 commit comments