|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import yargs from "yargs"; |
| 4 | +import ignore from "ignore"; |
| 5 | +import { glob } from "glob"; |
| 6 | +import { remark } from "remark"; |
| 7 | +import { reporter } from "vfile-reporter"; |
| 8 | +import { hideBin } from "yargs/helpers"; |
| 9 | +import {read} from 'to-vfile' |
| 10 | + |
| 11 | +// Counter to keep track of total warnings |
| 12 | +let totalWarnings = 0; |
| 13 | + |
| 14 | +/** |
| 15 | + * Load and parse the .remarkignore file. |
| 16 | + * @param {string} path path to the ignore file. |
| 17 | + * @returns {Promise<ignore>} A promise that resolves with the ignore instance. |
| 18 | + */ |
| 19 | +async function loadIgnoreFile(path) { |
| 20 | + try { |
| 21 | + const ignoreFileContent = fs.readFileSync(path, "utf-8"); |
| 22 | + const ig = ignore().add(ignoreFileContent); |
| 23 | + return ig; |
| 24 | + } catch (err) { |
| 25 | + console.warn("No .remarkignore file found, proceeding without ignoring files."); |
| 26 | + return ignore(); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Process a single file with the given preset and formatting flag. |
| 32 | + * @param {string} filePath The path to the file |
| 33 | + * @param {string} preset The preset object to use for linting with array of remark plugins |
| 34 | + * @param {boolean} shouldFormat Flag to indicate whether formatting (changing markdown files) should be applied |
| 35 | + */ |
| 36 | +async function formatSingleFile(filePath, preset, shouldFormat) { |
| 37 | + try { |
| 38 | + // Create a virtual file with metadata like `stem`. |
| 39 | + // This is needed for rules related to files. |
| 40 | + const vfile = await read(filePath); |
| 41 | + |
| 42 | + // Process the file with the given preset |
| 43 | + const file = await remark().use(preset).process(vfile); |
| 44 | + |
| 45 | + // Check if there are any issues |
| 46 | + const issues = file.messages.length; |
| 47 | + |
| 48 | + // Print the issues |
| 49 | + if (issues === 0) { |
| 50 | + console.log(`${filePath}: no issues found`); |
| 51 | + } else { |
| 52 | + totalWarnings += file.messages.length; |
| 53 | + console.error(reporter(file)); |
| 54 | + } |
| 55 | + |
| 56 | + // Write the file back if formatting is enabled |
| 57 | + if (shouldFormat) { |
| 58 | + fs.writeFileSync(filePath, file.value.toString()); |
| 59 | + } |
| 60 | + } catch (err) { |
| 61 | + console.error(`Error processing file ${filePath}:`, err); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Main function to handle glob pattern and process files |
| 66 | +/** |
| 67 | + * Process files based on the given pattern, preset, and formatting flag. |
| 68 | + * @param {string} pattern The glob pattern to match files. |
| 69 | + * @param {string} pattern The path to the ignore file. |
| 70 | + * @param {string} preset The path to the preset object to use for linting with array of remark plugins. |
| 71 | + * @param {boolean} shouldFormat Flag to indicate whether formatting (changing markdown files) should be applied. |
| 72 | + * @param {boolean} failOnError Flag to indicate whether to fail command if any error is found. |
| 73 | + * @returns {Promise<void>} A promise that resolves when all files are processed. |
| 74 | + */ |
| 75 | +export async function processFiles(pattern, ignoreFile, preset, shouldFormat, failOnError) { |
| 76 | + try { |
| 77 | + // Load the ignore file and get the list of files |
| 78 | + const ig = await loadIgnoreFile(ignoreFile); |
| 79 | + const files = await glob(pattern); |
| 80 | + |
| 81 | + if (files.length === 0) { |
| 82 | + console.log("No files matched the given pattern."); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Filter out files that are ignored |
| 87 | + const filteredFiles = files.filter((file) => !ig.ignores(file)); |
| 88 | + |
| 89 | + if (filteredFiles.length === 0) { |
| 90 | + console.log("All matched files are ignored."); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + // Process each file |
| 95 | + for (const file of filteredFiles) { |
| 96 | + await formatSingleFile(file, preset, shouldFormat); |
| 97 | + } |
| 98 | + |
| 99 | + // Print total warnings and fail command if needed |
| 100 | + if (totalWarnings > 0) { |
| 101 | + console.log(`⚠️ Total ${totalWarnings} warning(s)`); |
| 102 | + if (failOnError) { |
| 103 | + process.exit(1); |
| 104 | + } |
| 105 | + } |
| 106 | + } catch (err) { |
| 107 | + console.error("Error during file processing:", err); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Use yargs to handle command-line arguments |
| 112 | +const argv = yargs(hideBin(process.argv)) |
| 113 | + .option("pattern", { |
| 114 | + alias: "p", |
| 115 | + type: "string", |
| 116 | + description: "Glob pattern to match files", |
| 117 | + demandOption: true, |
| 118 | + }) |
| 119 | + .option("preset", { |
| 120 | + alias: "r", |
| 121 | + type: "string", |
| 122 | + description: "Path to the preset file", |
| 123 | + demandOption: true, |
| 124 | + }) |
| 125 | + .option("ignoreFile", { |
| 126 | + alias: "i", |
| 127 | + type: "string", |
| 128 | + description: "Path to the ignore file", |
| 129 | + demandOption: false, |
| 130 | + }) |
| 131 | + .option("format", { |
| 132 | + alias: "f", |
| 133 | + type: "boolean", |
| 134 | + description: "Flag to indicate whether formatting should be applied", |
| 135 | + default: false, |
| 136 | + }) |
| 137 | + .option("failOnError", { |
| 138 | + alias: "e", |
| 139 | + type: "boolean", |
| 140 | + description: "Flag to indicate whether to fail command if any error is found", |
| 141 | + default: false, |
| 142 | + }).argv; |
| 143 | + |
| 144 | +// Dynamically import the preset file |
| 145 | +const presetPath = path.resolve(argv.preset); |
| 146 | +import(presetPath) |
| 147 | +.then((preset) => |
| 148 | + // Start processing files |
| 149 | + processFiles(argv.pattern, argv.ignoreFile, preset.default, argv.format, argv.failOnError) |
| 150 | +); |
| 151 | + |
0 commit comments