| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| 'use strict'; |
|
|
| const spawn = require('child_process').spawn; |
| const compilerPath = require('google-closure-compiler-java'); |
| const path = require('path'); |
| const contribPath = path.resolve(__dirname, '../../contrib'); |
|
|
| class Compiler { |
| |
| |
| |
| |
| constructor(args, extraCommandArgs) { |
| this.commandArguments = []; |
| this.extraCommandArgs = extraCommandArgs; |
|
|
| if (Compiler.JAR_PATH) { |
| this.JAR_PATH = Compiler.JAR_PATH; |
| } |
|
|
| if (Array.isArray(args)) { |
| this.commandArguments = this.commandArguments.concat(args.slice()); |
| } else { |
| for (let key in args) { |
| if (Array.isArray(args[key])) { |
| for (let i = 0; i < args[key].length; i++) { |
| this.commandArguments.push( |
| this.formatArgument(key, args[key][i])); |
| } |
| } else { |
| this.commandArguments.push( |
| this.formatArgument(key, args[key])); |
| } |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| run(callback) { |
| if (this.JAR_PATH) { |
| this.commandArguments.unshift('-jar', Compiler.JAR_PATH); |
| if (this.extraCommandArgs) { |
| this.commandArguments.unshift(...this.extraCommandArgs); |
| } |
| } |
|
|
| if (this.logger) { |
| this.logger(this.getFullCommand() + '\n'); |
| } |
| let compileProcess = spawn(this.javaPath, this.commandArguments, this.spawnOptions); |
|
|
| let stdOutData = ''; |
| let stdErrData = ''; |
| if (callback) { |
| if (compileProcess.stdout) { |
| compileProcess.stdout.setEncoding('utf8'); |
| compileProcess.stdout.on('data', data => { |
| stdOutData += data; |
| }); |
| compileProcess.stdout.on('error', err => { |
| stdErrData += err.toString(); |
| }); |
| } |
|
|
| if (compileProcess.stderr) { |
| compileProcess.stderr.setEncoding('utf8'); |
| compileProcess.stderr.on('data', data => { |
| stdErrData += data; |
| }); |
| } |
|
|
| compileProcess.on('close', code => { |
| if (code !== 0) { |
| stdErrData = this.prependFullCommand(stdErrData); |
| } |
|
|
| callback(code, stdOutData, stdErrData); |
| }); |
|
|
| compileProcess.on('error', err => { |
| callback(1, stdOutData, |
| this.prependFullCommand('Process spawn error. Is java in the path?\n' + err.message)); |
| }); |
| } |
|
|
| return compileProcess; |
| } |
|
|
|
|
| |
| |
| |
| getFullCommand() { |
| return this.javaPath + ' ' + this.commandArguments.join(' '); |
| } |
|
|
| |
| |
| |
| |
| prependFullCommand(msg) { |
| return this.getFullCommand() + '\n\n' + msg + '\n\n'; |
| } |
|
|
| |
| |
| |
| |
| |
| formatArgument(key, val) { |
| let normalizedKey = key.replace(/[A-Z]/g, match => `_${match.toLowerCase()}`); |
| normalizedKey = normalizedKey.replace(/^--/, ''); |
|
|
| if (val === undefined || val === null) { |
| return `--${normalizedKey}`; |
| } |
|
|
| return `--${normalizedKey}=${val}`; |
| } |
| } |
|
|
| |
| |
| |
| |
| Compiler.JAR_PATH = compilerPath; |
|
|
| |
| |
| |
| Compiler.prototype.javaPath = 'java'; |
|
|
| |
| Compiler.prototype.logger = null; |
|
|
| |
| Compiler.prototype.spawnOptions = undefined; |
|
|
| |
| Compiler.COMPILER_PATH = compilerPath; |
|
|
| |
| Compiler.CONTRIB_PATH = contribPath; |
|
|
| module.exports = Compiler; |
|
|