Skip to content

Commit

Permalink
Refactors code into separate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mairatma committed Dec 15, 2016
1 parent 4e33ceb commit 44dba5d
Showing 1 changed file with 60 additions and 49 deletions.
109 changes: 60 additions & 49 deletions lib/pipelines/compileSoy.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ function addTemplateParam(soyJsPath, templateName, param) {
templateData[soyJsPath][templateName].params.push(param);
}

function buildSoyCompilerArgs(files, outputDir, soyDeps) {
var args = [
'-jar',
PATH_TO_JAR,
'--outputPathFormat',
path.join(outputDir, '{INPUT_DIRECTORY}{INPUT_FILE_NAME}.js')
].concat(files.map(function(singleFile) {
return path.relative(process.cwd(), singleFile.path);
}));

if (soyDeps) {
var expanded = expand(soyDeps);
for (var i = 0; i < expanded.length; i++) {
args.push('--deps', expanded[i]);
}
}
return args;
}

function buildSoyCompilerError(errorMsg) {
var msg = 'Compile error:\n';
if (errorMsg.indexOf('UnsupportedClassVersionError') !== -1) {
msg += 'Make sure that you have Java version 8 or higher installed.\n';
}
return new gutil.PluginError('metal-tools-soy', msg + errorMsg);
}

function compileToIncDom(options) {
var files = [];
return through.obj(
Expand All @@ -59,59 +86,21 @@ function compileToIncDom(options) {
callback();
return;
}

var stream = this;
var outputDir = '/tmp/metal-tools-soy';
var args = [
'-jar',
PATH_TO_JAR,
'--outputPathFormat',
path.join(outputDir, '{INPUT_DIRECTORY}{INPUT_FILE_NAME}.js')
].concat(files.map(function(singleFile) {
return path.relative(process.cwd(), singleFile.path);
}));

if (options.soyDeps) {
var expanded = expand(options.soyDeps);
for (var i = 0; i < expanded.length; i++) {
args.push('--deps', expanded[i]);
}
}

var cp = childProcess.spawn('java', args, {cwd: process.cwd()});
var stderr = '';
cp.stderr.on('data', function(data) {
stderr += data;
});
cp.on('exit', function(code) {
if (code === 0) {
for (var i = 0; i < files.length; i++) {
var realPath = path.join(outputDir, path.relative(process.cwd(), files[i].path) + '.js');
var compiled = new gutil.File({
base: files[i].base,
contents: fs.readFileSync(realPath),
cwd: files[i].cwd,
path: files[i].path + '.js'
});
stream.emit('data', compiled);
}
} else {
var msg = 'Compile error:\n';
if (stderr.indexOf('UnsupportedClassVersionError') !== -1) {
msg += 'Make sure that you have Java version 8 or higher installed.\n';
}
stream.emit(
'error',
new gutil.PluginError('metal-tools-soy', msg + stderr)
);
}
stream.emit('end');
callback();
});
runSoyCompiler(files, options, this, callback);
}
);
}

function createCompiledGulpFile(file, outputDir) {
var realPath = path.join(outputDir, path.relative(process.cwd(), file.path) + '.js');
return new gutil.File({
base: file.base,
contents: fs.readFileSync(realPath),
cwd: file.cwd,
path: file.path + '.js'
});
}

function extractParams() {
return through.obj(function(file, encoding, callback) {
try {
Expand Down Expand Up @@ -208,3 +197,25 @@ function replaceTemplateRequires() {
callback();
});
}

function runSoyCompiler(files, options, stream, callback) {
var outputDir = '/tmp/metal-tools-soy';
var args = buildSoyCompilerArgs(files, outputDir, options.soyDeps);

var cp = childProcess.spawn('java', args, {cwd: process.cwd()});
var stderr = '';
cp.stderr.on('data', function(data) {
stderr += data;
});
cp.on('exit', function(code) {
if (code === 0) {
for (var i = 0; i < files.length; i++) {
stream.emit('data', createCompiledGulpFile(files[i], outputDir));
}
} else {
stream.emit('error', buildSoyCompilerError(stderr));
}
stream.emit('end');
callback();
});
}

0 comments on commit 44dba5d

Please sign in to comment.