|
| 1 | +// Utility to run files through Alex.js (http://alexjs.com/) |
| 2 | +// run using `npm run alex` |
| 3 | +var fs = require('fs') |
| 4 | +var path = require('path') |
| 5 | +var toVFile = require('to-vfile') |
| 6 | +var argv = require('argv') |
| 7 | +var alex = require('alex') |
| 8 | +var colors = require('colors/safe') |
| 9 | + |
| 10 | +colors.setTheme({ |
| 11 | + info: 'green', |
| 12 | + warn: 'yellow', |
| 13 | + error: 'red' |
| 14 | +}) |
| 15 | + |
| 16 | +var options = [ |
| 17 | + { |
| 18 | + name: 'dir', |
| 19 | + short: 'd', |
| 20 | + type: 'path', |
| 21 | + description: 'directory to run through Alex.js', |
| 22 | + example: 'npm run alex -- -d _posts' |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'file', |
| 26 | + short: 'f', |
| 27 | + type: 'path', |
| 28 | + description: 'file to run through Alex.js', |
| 29 | + example: 'npm run alex -- -f _posts/2013-06-08-hacker-news-thread-about-hoodie.md' |
| 30 | + } |
| 31 | +] |
| 32 | + |
| 33 | +var args = argv.option(options).run() |
| 34 | + |
| 35 | +if (args.options.dir) { |
| 36 | + scanDir(args.options.dir) |
| 37 | +} |
| 38 | + |
| 39 | +if (args.options.file) { |
| 40 | + scanFile(args.options.file) |
| 41 | +} |
| 42 | + |
| 43 | +function scanFile (file) { |
| 44 | + try { |
| 45 | + var vfile = toVFile.readSync(file) |
| 46 | + var messages = alex.markdown(vfile).messages |
| 47 | + if (messages.length) { |
| 48 | + console.log(colors.warn(messages)) |
| 49 | + } else { |
| 50 | + console.log(colors.info('alex.js says ' + file + ' looks good!')) |
| 51 | + } |
| 52 | + } catch (error) { |
| 53 | + console.error(colors.error('Error in reading file ' + file + ' - ' + error.message)) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function scanDir (dir) { |
| 58 | + try { |
| 59 | + var files = fs.readdirSync(dir) |
| 60 | + files.forEach(function (file) { |
| 61 | + scanFile(dir + path.sep + file) |
| 62 | + }) |
| 63 | + } catch (error) { |
| 64 | + console.log(colors.error('Error in reading directory ' + dir + ' - ' + error.message)) |
| 65 | + } |
| 66 | +} |
0 commit comments