embark-area-51/lib/pipeline.js

99 lines
3.0 KiB
JavaScript
Raw Normal View History

2016-09-25 01:23:57 +00:00
/*jshint esversion: 6, loopfunc: true */
2016-08-21 16:02:02 +00:00
var fs = require('fs');
var mkdirp = require('mkdirp');
var Pipeline = function(options) {
2016-08-22 03:40:05 +00:00
this.buildDir = options.buildDir;
this.contractsFiles = options.contractsFiles;
this.assetFiles = options.assetFiles;
2016-09-17 03:56:25 +00:00
this.logger = options.logger;
2016-12-10 15:20:04 +00:00
this.plugins = options.plugins;
2016-08-21 16:02:02 +00:00
};
Pipeline.prototype.build = function(abi) {
2016-09-17 03:56:25 +00:00
var self = this;
2016-08-22 03:40:05 +00:00
for(var targetFile in this.assetFiles) {
2016-08-21 16:02:02 +00:00
2016-12-10 15:20:04 +00:00
// TODO: run the plugin here instead, for each file
2016-09-25 01:23:57 +00:00
var content = this.assetFiles[targetFile].map(file => {
2016-09-17 03:56:25 +00:00
self.logger.info("reading " + file.filename);
var pipelinePlugins = this.plugins.getPluginsFor('pipeline');
2016-08-22 03:40:05 +00:00
if (file.filename === 'embark.js') {
return file.content + "\n" + abi;
2017-01-26 11:34:00 +00:00
} else if (file.filename === 'embark-plugins.js') {
var filesFromPlugins = [];
var filePlugins = self.plugins.getPluginsFor('pipelineFiles');
if (filePlugins.length > 0) {
filePlugins.forEach(function(plugin) {
try {
var fileObjects = plugin.runFilePipeline();
for (var i=0; i < fileObjects.length; i++) {
var fileObject = fileObjects[i];
console.debug(JSON.stringify(fileObject));
filesFromPlugins.push(fileObject);
}
}
catch(err) {
self.logger.error(err.message);
}
});
}
var fileContents = filesFromPlugins.map(function(file) {
if (pipelinePlugins.length > 0) {
pipelinePlugins.forEach(function(plugin) {
console.log(plugin.name + ": trying " + file.filename);
try {
if (file.options && file.options.skipPipeline) {
console.log("skipping");
return;
}
file.content = plugin.runPipeline({targetFile: file.filename, source: file.content});
}
catch(err) {
self.logger.error(err.message);
}
});
}
return file.content;
});
return fileContents.join('\n');
} else if (['web3.js', 'ipfs.js', 'ipfs-api.js', 'orbit.js'].indexOf(file.filename) >= 0) {
return file.content;
2016-08-21 16:02:02 +00:00
} else {
if (pipelinePlugins.length > 0) {
pipelinePlugins.forEach(function(plugin) {
2017-01-26 11:34:00 +00:00
try {
file.content = plugin.runPipeline({targetFile: file.filename, source: file.content});
}
catch(err) {
self.logger.error(err.message);
}
});
}
2016-08-22 03:40:05 +00:00
return file.content;
2016-08-21 16:02:02 +00:00
}
}).join("\n");
var dir = targetFile.split('/').slice(0, -1).join('/');
2016-09-17 03:56:25 +00:00
self.logger.info("creating dir " + this.buildDir + dir);
2016-08-22 03:40:05 +00:00
mkdirp.sync(this.buildDir + dir);
2016-08-21 16:02:02 +00:00
self.logger.info("writing file " + this.buildDir + targetFile);
2016-08-22 03:40:05 +00:00
fs.writeFileSync(this.buildDir + targetFile, content);
2016-08-21 16:02:02 +00:00
}
};
module.exports = Pipeline;