Skip to content
This repository was archived by the owner on Sep 19, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 62 additions & 71 deletions lib/gulp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,87 +140,78 @@ module.exports = function(initOptions) {
// Add the gulp-specific argument so the compiler will understand the JSON encoded input
// for gulp, the stream mode will be 'BOTH', but when invoked from grunt, we only use
// a stream mode of 'IN'
if (jsonFiles.length > 0) {
compiler.commandArguments.push('--json_streams', this.streamMode_);
}

var compilerProcess = compiler.run();

compiler.run(function(compilerProcess) {
var stdOutData = '', stdErrData = '';

compilerProcess.stdout.on('data', function (data) {
stdOutData += data;
});
compilerProcess.stderr.on('data', function (data) {
stdErrData += data;
});

Promise.all([
new Promise(function(resolve) {
compilerProcess.on('close', function(code) {
resolve(code);
});
}),
new Promise(function(resolve) {
compilerProcess.stdout.on('end', function() {
resolve();
});
}),
new Promise(function(resolve) {
compilerProcess.stderr.on('end', function() {
resolve();
});
})
]).then((function(results) {
var code = results[0];

// standard error will contain compilation warnings, log those
if (stdErrData.trim().length > 0) {
logger(chalk.yellow(this.PLUGIN_NAME_) + ': ' + stdErrData);
}

// non-zero exit means a compilation error
if (code !== 0) {
this.emit('error', new PluginError(this.PLUGIN_NAME_, 'Compilation error'));
}
compilerProcess.stdout.on('data', function (data) {
stdOutData += data;
});
compilerProcess.stderr.on('data', function (data) {
stdErrData += data;
});

Promise.all([
new Promise(function(resolve) {
compilerProcess.stdout.on('end', function() {
resolve();
});
}),
new Promise(function(resolve) {
compilerProcess.stderr.on('end', function() {
resolve();
});
})
]).then((function(results) {
if (stdErrData.trim().length > 0) {
logger(chalk.yellow(this.PLUGIN_NAME_) + ': ' + stdErrData);
}

// If present, standard output will be a string of JSON encoded files.
// Convert these back to vinyl
if (stdOutData.trim().length > 0) {
var outputFiles;
try {
outputFiles = jsonToVinyl(stdOutData);
} catch (e) {
this.emit('error', new PluginError(this.PLUGIN_NAME_, 'Error parsing json encoded files'));
cb();
return;
}
// If present, standard output will be a string of JSON encoded files.
// Convert these back to vinyl
if (stdOutData.trim().length > 0) {
var outputFiles;
try {
outputFiles = jsonToVinyl(stdOutData);
} catch (e) {
this.emit('error', new PluginError(this.PLUGIN_NAME_, 'Error parsing json encoded files'));
cb();
return;
}

for (var i = 0; i < outputFiles.length; i++) {
if (outputFiles[i].sourceMap) {
applySourceMap(outputFiles[i], outputFiles[i].sourceMap);
for (var i = 0; i < outputFiles.length; i++) {
if (outputFiles[i].sourceMap) {
applySourceMap(outputFiles[i], outputFiles[i].sourceMap);
}
this.push(outputFiles[i]);
}
this.push(outputFiles[i]);
}
}
cb();
}).bind(this));
cb();
}).bind(this));

// Error events occur when there was a problem spawning the compiler process
compilerProcess.on('error', (function (err) {
this.emit('error', new PluginError(this.PLUGIN_NAME_,
'Process spawn error. Is java in the path?\n' + err.message));
cb();
}).bind(this));
// Error events occur when there was a problem spawning the compiler process
compilerProcess.on('error', (function (err) {
this.emit('error', new PluginError(this.PLUGIN_NAME_,
'Process spawn error. Is java in the path?\n' + err.message));
cb();
}).bind(this));

compilerProcess.stdin.on('error', (function(err) {
this.emit('Error', new PluginError(this.PLUGIN_NAME_,
'Error writing to stdin of the compiler.\n' + err.message));
cb();
}).bind(this));

var stdInStream = new stream.Readable({ read: function() {}});
stdInStream.pipe(compilerProcess.stdin);
stdInStream.push(JSON.stringify(jsonFiles));
stdInStream.push(null);
compilerProcess.stdin.on('error', (function(err) {
this.emit('Error', new PluginError(this.PLUGIN_NAME_,
'Error writing to stdin of the compiler.\n' + err.message));
cb();
}).bind(this));

if (jsonFiles.length > 0) {
var stdInStream = new stream.Readable({ read: function() {}});
stdInStream.pipe(compilerProcess.stdin);
stdInStream.push(JSON.stringify(jsonFiles));
stdInStream.push(null);
}
}.bind(this));
};


Expand Down
45 changes: 9 additions & 36 deletions lib/node/closure-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var spawn = require('child_process').spawn;
var compilerPath = require.resolve('../../compiler.jar');
var path = require('path');
var contribPath = path.dirname(compilerPath) + '/contrib';
var nailgunServer = require('node-nailgun').createServer();

/**
* @constructor
Expand All @@ -36,10 +37,6 @@ var contribPath = path.dirname(compilerPath) + '/contrib';
function Compiler(args, extraCommandArgs) {
this.commandArguments = (extraCommandArgs || []).slice();

if (Compiler.JAR_PATH) {
this.commandArguments.push('-jar', Compiler.JAR_PATH);
}

if (Array.isArray(args)) {
this.commandArguments = this.commandArguments.concat(args.slice());
} else {
Expand All @@ -63,11 +60,6 @@ function Compiler(args, extraCommandArgs) {
*/
Compiler.JAR_PATH = compilerPath;

/**
* @type {string}
*/
Compiler.prototype.javaPath = 'java';

/** @type {function(...*)|null} */
Compiler.prototype.logger = null;

Expand All @@ -76,40 +68,21 @@ Compiler.prototype.spawnOptions = undefined;

/**
* @param {function(number, string, string)=} callback
* @return {child_process.ChildProcess}
*/
Compiler.prototype.run = function(callback) {
if (this.logger) {
this.logger(this.getFullCommand() + '\n');
}

var compileProcess = spawn(this.javaPath, this.commandArguments, this.spawnOptions);

var stdOutData = '', stdErrData = '';
if (callback) {
compileProcess.stdout.on('data', function (data) {
stdOutData += data;
});

compileProcess.stderr.on('data', function (data) {
stdErrData += data;
});

compileProcess.on('close', (function (code) {
if (code !== 0) {
stdErrData = this.prependFullCommand(stdErrData);
}

callback(code, stdOutData, stdErrData);
}).bind(this));

compileProcess.on('error', (function (err) {
callback(1, stdOutData,
this.prependFullCommand('Process spawn error. Is java in the path?\n' + err.message));
}).bind(this));
}
var compileProcess = nailgunServer.spawnJar(Compiler.JAR_PATH, this.commandArguments, function(error, compileProcess) {
if (compileProcess !== undefined) {
callback(compileProcess);
}

return compileProcess;
if (error !== undefined && error !== null) {
console.error(error);
}
});
};

/** @type {string} */
Expand Down
Loading