|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// MODULES // |
| 4 | + |
| 5 | +var spawn = require( 'child_process' ).spawn; |
| 6 | + |
| 7 | + |
| 8 | +// FUNCTIONS // |
| 9 | + |
| 10 | +/** |
| 11 | +* Callback invoked upon encountering an error. |
| 12 | +* |
| 13 | +* @private |
| 14 | +* @param {Error} error - error object |
| 15 | +*/ |
| 16 | +function onError( error ) { |
| 17 | + process.stderr.write( error.message+'\n', 'utf8' ); |
| 18 | + return process.exit( 1 ); |
| 19 | +} // end FUNCTION onError() |
| 20 | + |
| 21 | +/** |
| 22 | +* Callback invoked upon child process close. |
| 23 | +* |
| 24 | +* @private |
| 25 | +* @param {number} code - exit code |
| 26 | +*/ |
| 27 | +function onFinish( code ) { |
| 28 | + if ( code !== 0 ) { |
| 29 | + process.stderr.write( '`make` process exited with code `'+code + '.\n' ); |
| 30 | + return process.exit( code ); |
| 31 | + } |
| 32 | + // Cannot write to `stdout` and then immediately `exit` as `buffer` may not yet have drained https://github.com/nodejs/node/issues/6456. |
| 33 | + // process.exit( 0 ); |
| 34 | + |
| 35 | + // HACK: workaround is to use `console.log` and no exit: |
| 36 | + console.log( '' ); |
| 37 | +} // end FUNCTION onFinish() |
| 38 | + |
| 39 | +/** |
| 40 | +* Callback invoked upon receiving data from `stdout`. |
| 41 | +* |
| 42 | +* @private |
| 43 | +* @param {Buffer} data - standard output |
| 44 | +*/ |
| 45 | +function stdout( data ) { |
| 46 | + process.stdout.write( data ); |
| 47 | +} // end FUNCTION stdout() |
| 48 | + |
| 49 | +/** |
| 50 | +* Callback invoked upon receiving data from `stderr`. |
| 51 | +* |
| 52 | +* @private |
| 53 | +* @param {Buffer} data - standard error |
| 54 | +*/ |
| 55 | +function stderr( data ) { |
| 56 | + process.stderr.write( data ); |
| 57 | +} // end FUNCTION stderr() |
| 58 | + |
| 59 | + |
| 60 | +// PLUGIN // |
| 61 | + |
| 62 | +/** |
| 63 | +* `makie` plugin to lint Markdown files. |
| 64 | +* |
| 65 | +* @param {string} dir - Makefile directory |
| 66 | +* @param {string} cwd - current working directory |
| 67 | +* @param {string} subpath - subdirectory path |
| 68 | +*/ |
| 69 | +function plugin( dir, cwd, subpath ) { |
| 70 | + var opts; |
| 71 | + var args; |
| 72 | + var proc; |
| 73 | + |
| 74 | + opts = {}; |
| 75 | + opts.cwd = dir; |
| 76 | + |
| 77 | + args = []; |
| 78 | + |
| 79 | + // Environment variables: |
| 80 | + if ( subpath ) { |
| 81 | + args.push( 'MARKDOWN_FILTER=.*/'+subpath+'/.*' ); |
| 82 | + } |
| 83 | + // Target: |
| 84 | + args.push( 'lint-markdown' ); |
| 85 | + |
| 86 | + proc = spawn( 'make', args, opts ); |
| 87 | + proc.on( 'error', onError ); |
| 88 | + proc.stdout.on( 'data', stdout ); |
| 89 | + proc.stderr.on( 'data', stderr ); |
| 90 | + proc.on( 'close', onFinish ); |
| 91 | +} // end FUNCTION plugin() |
| 92 | + |
| 93 | + |
| 94 | +// EXPORTS // |
| 95 | + |
| 96 | +module.exports = plugin; |
0 commit comments