|
| 1 | +#!/usr/bin/env node |
| 2 | +// @ts-check |
| 3 | + |
| 4 | +const debug = require('debug')('cypress-markdown-preprocessor') |
| 5 | +const fs = require('fs') |
| 6 | +const arg = require('arg') |
| 7 | +const globby = require('globby') |
| 8 | +const { collectFiddles } = require('../src/collect-utils') |
| 9 | + |
| 10 | +const args = arg( |
| 11 | + { |
| 12 | + '--print': Boolean, // to STDOUT |
| 13 | + '--filename': String, // output filename |
| 14 | + // aliases |
| 15 | + '-p': '--print', |
| 16 | + '-f': '--filename', |
| 17 | + }, |
| 18 | + { argv: process.argv }, |
| 19 | +) |
| 20 | +debug('arguments %o', args) |
| 21 | + |
| 22 | +// remove "node" and the script name from the list of arguments |
| 23 | +const markdownPattern = args._.slice(2) |
| 24 | +const sourceFiles = globby.sync(markdownPattern) |
| 25 | +debug('source files') |
| 26 | +debug(sourceFiles) |
| 27 | + |
| 28 | +if (!sourceFiles.length) { |
| 29 | + console.error('Could not find any Markdown files') |
| 30 | + process.exit(1) |
| 31 | +} |
| 32 | + |
| 33 | +console.log( |
| 34 | + 'Searching for fiddles in %d Markdown file(s)', |
| 35 | + sourceFiles.length, |
| 36 | +) |
| 37 | + |
| 38 | +const fiddles = collectFiddles(sourceFiles) |
| 39 | + |
| 40 | +console.log( |
| 41 | + 'found %d fiddle(s) across %d Markdown file(s)', |
| 42 | + fiddles.length, |
| 43 | + sourceFiles.length, |
| 44 | +) |
| 45 | + |
| 46 | +if (args['--print']) { |
| 47 | + console.log(fiddles) |
| 48 | +} |
| 49 | + |
| 50 | +if (args['--filename']) { |
| 51 | + const text = JSON.stringify(fiddles, null, 2) + '\n' |
| 52 | + fs.writeFileSync(args['--filename'], text) |
| 53 | + console.log('write fiddles to JSON file %s', args['--filename']) |
| 54 | +} |
0 commit comments