|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2019 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var spawn = require( 'child_process' ).spawn; |
| 24 | + |
| 25 | + |
| 26 | +// FUNCTIONS // |
| 27 | + |
| 28 | +/** |
| 29 | +* Callback invoked upon encountering an error. |
| 30 | +* |
| 31 | +* @private |
| 32 | +* @param {Error} error - error object |
| 33 | +*/ |
| 34 | +function onError( error ) { |
| 35 | + process.exitCode = 1; |
| 36 | + console.error( 'Error: %s', error.message ); // eslint-disable-line no-console |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | +* Callback invoked upon child process close. |
| 41 | +* |
| 42 | +* @private |
| 43 | +* @param {number} code - exit code |
| 44 | +* @returns {void} |
| 45 | +*/ |
| 46 | +function onFinish( code ) { |
| 47 | + if ( code !== 0 ) { |
| 48 | + process.exitCode = code; |
| 49 | + return console.error( 'Child process exited with code `'+code + '`.' ); // eslint-disable-line no-console |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | +* Callback invoked upon receiving data from `stdout`. |
| 55 | +* |
| 56 | +* @private |
| 57 | +* @param {Buffer} data - standard output |
| 58 | +*/ |
| 59 | +function stdout( data ) { |
| 60 | + process.stdout.write( data ); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | +* Callback invoked upon receiving data from `stderr`. |
| 65 | +* |
| 66 | +* @private |
| 67 | +* @param {Buffer} data - standard error |
| 68 | +*/ |
| 69 | +function stderr( data ) { |
| 70 | + process.stderr.write( data ); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +// MAIN // |
| 75 | + |
| 76 | +/** |
| 77 | +* Plugin to lint TypeScript declaration files. |
| 78 | +* |
| 79 | +* @param {string} dir - Makefile directory |
| 80 | +* @param {string} cwd - current working directory |
| 81 | +* @param {string} subpath - subdirectory path |
| 82 | +*/ |
| 83 | +function plugin( dir, cwd, subpath ) { |
| 84 | + var opts; |
| 85 | + var args; |
| 86 | + var proc; |
| 87 | + |
| 88 | + opts = {}; |
| 89 | + opts.cwd = dir; |
| 90 | + |
| 91 | + args = []; |
| 92 | + |
| 93 | + // Environment variables: |
| 94 | + if ( subpath ) { |
| 95 | + args.push( 'TYPESCRIPT_DECLARATIONS_FILTER=.*/'+subpath+'/.*' ); |
| 96 | + } |
| 97 | + // Target: |
| 98 | + args.push( 'lint-typescript-declarations' ); |
| 99 | + |
| 100 | + proc = spawn( 'make', args, opts ); |
| 101 | + proc.on( 'error', onError ); |
| 102 | + proc.stdout.on( 'data', stdout ); |
| 103 | + proc.stderr.on( 'data', stderr ); |
| 104 | + proc.on( 'close', onFinish ); |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +// EXPORTS // |
| 109 | + |
| 110 | +module.exports = plugin; |
0 commit comments