|
| 1 | +#!/usr/bin/env node |
| 2 | +// CLI for parsing comment blocks from markdown and other files |
| 3 | +const mri = require('mri') |
| 4 | +const { parseBlocks } = require('./src/index') |
| 5 | + |
| 6 | +const argv = process.argv.slice(2) |
| 7 | +const options = mri(argv) |
| 8 | + |
| 9 | +/** |
| 10 | + * Read all data from stdin |
| 11 | + * @returns {Promise<string>} |
| 12 | + */ |
| 13 | +function readStdin() { |
| 14 | + return new Promise((resolve, reject) => { |
| 15 | + let data = '' |
| 16 | + process.stdin.setEncoding('utf8') |
| 17 | + process.stdin.on('data', chunk => data += chunk) |
| 18 | + process.stdin.on('end', () => resolve(data)) |
| 19 | + process.stdin.on('error', reject) |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Interpret escape sequences in string (e.g., \n -> newline) |
| 25 | + * @param {string} str |
| 26 | + * @returns {string} |
| 27 | + */ |
| 28 | +function interpretEscapes(str) { |
| 29 | + if (!str) return str |
| 30 | + return str.replace(/\\n/g, '\n').replace(/\\t/g, '\t') |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * JSON.stringify replacer that handles RegExp objects |
| 35 | + * @param {string} _key |
| 36 | + * @param {any} value |
| 37 | + */ |
| 38 | +function jsonReplacer(_key, value) { |
| 39 | + if (value instanceof RegExp) { |
| 40 | + return value.toString() |
| 41 | + } |
| 42 | + return value |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Check if string looks like content vs a file path |
| 47 | + * @param {string} str |
| 48 | + * @returns {boolean} |
| 49 | + */ |
| 50 | +function isContent(str) { |
| 51 | + if (!str) return false |
| 52 | + if (str.includes('\n') || str.includes('\\n')) return true |
| 53 | + if (str.includes('<!--')) return true |
| 54 | + if (str.startsWith('#')) return true |
| 55 | + return false |
| 56 | +} |
| 57 | + |
| 58 | +async function run() { |
| 59 | + if (options.help || options.h) { |
| 60 | + console.log(` |
| 61 | +Usage: block-parser [options] [content] |
| 62 | +
|
| 63 | +Options: |
| 64 | + --open Opening comment keyword (default: doc-gen) |
| 65 | + --close Closing comment keyword (default: end-doc-gen) |
| 66 | + --syntax Comment syntax: md, js, html, etc (default: md) |
| 67 | + --help, -h Show this help message |
| 68 | + --version, -v Show version |
| 69 | +
|
| 70 | +Examples: |
| 71 | + block-parser "# Title\\n<!-- doc-gen TOC --><!-- end-doc-gen -->" |
| 72 | + echo "<!-- doc-gen TOC --><!-- end-doc-gen -->" | block-parser |
| 73 | + block-parser --open auto --close /auto "<!-- auto TOC --><!-- /auto -->" |
| 74 | +`) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + if (options.version || options.v) { |
| 79 | + const pkg = require('./package.json') |
| 80 | + console.log(`${pkg.name} v${pkg.version}`) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + const word = options.open || options.match || options.find |
| 85 | + const openKeyword = word || 'doc-gen' |
| 86 | + const closeKeyword = options.close || (word && word !== 'doc-gen' ? `/${word}` : 'end-doc-gen') |
| 87 | + const syntax = options.syntax || 'md' |
| 88 | + |
| 89 | + // Check if first positional arg is content |
| 90 | + let firstArg = options._ && options._[0] |
| 91 | + // Handle mri assigning content to a flag |
| 92 | + if (typeof options.open === 'string' && isContent(options.open)) { |
| 93 | + firstArg = options.open |
| 94 | + } |
| 95 | + |
| 96 | + if (firstArg && isContent(firstArg)) { |
| 97 | + const content = interpretEscapes(firstArg) |
| 98 | + const result = parseBlocks(content, { |
| 99 | + syntax, |
| 100 | + open: openKeyword, |
| 101 | + close: closeKeyword, |
| 102 | + }) |
| 103 | + console.log(JSON.stringify(result, jsonReplacer, 2)) |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + // Check for stdin pipe |
| 108 | + const hasNoFileArgs = !options._ || options._.length === 0 |
| 109 | + if (!process.stdin.isTTY && hasNoFileArgs) { |
| 110 | + const content = await readStdin() |
| 111 | + if (content.trim()) { |
| 112 | + const result = parseBlocks(content, { |
| 113 | + syntax, |
| 114 | + open: openKeyword, |
| 115 | + close: closeKeyword, |
| 116 | + }) |
| 117 | + console.log(JSON.stringify(result, jsonReplacer, 2)) |
| 118 | + return |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // No input provided |
| 123 | + console.error('No input provided. Use --help for usage.') |
| 124 | + process.exit(1) |
| 125 | +} |
| 126 | + |
| 127 | +run().catch(err => { |
| 128 | + console.error(err.message) |
| 129 | + process.exit(1) |
| 130 | +}) |
0 commit comments