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
|
|
|
|
2017-01-29 05:58:06 +00:00
|
|
|
var contentFiles = this.assetFiles[targetFile].map(file => {
|
2017-02-06 12:03:14 +00:00
|
|
|
self.logger.trace("reading " + file.filename);
|
2017-01-15 19:30:41 +00:00
|
|
|
|
|
|
|
var pipelinePlugins = this.plugins.getPluginsFor('pipeline');
|
|
|
|
|
2016-08-22 03:40:05 +00:00
|
|
|
if (file.filename === 'embark.js') {
|
2017-01-29 05:58:06 +00:00
|
|
|
return {content: file.content + "\n" + abi, filename: file.filename, path: file.path, modified: true};
|
2017-02-16 00:27:23 +00:00
|
|
|
} else if (file.filename === 'abi.js') {
|
|
|
|
return {content: abi, filename: file.filename, path: file.path, modified: true};
|
2017-01-15 19:30:41 +00:00
|
|
|
} else if (['web3.js', 'ipfs.js', 'ipfs-api.js', 'orbit.js'].indexOf(file.filename) >= 0) {
|
2017-01-29 05:58:06 +00:00
|
|
|
file.modified = true;
|
|
|
|
return file;
|
2016-08-21 16:02:02 +00:00
|
|
|
} else {
|
2017-01-15 19:30:41 +00:00
|
|
|
|
|
|
|
if (pipelinePlugins.length > 0) {
|
|
|
|
pipelinePlugins.forEach(function(plugin) {
|
2017-01-26 11:34:00 +00:00
|
|
|
try {
|
2017-02-03 11:30:08 +00:00
|
|
|
if (file.options && file.options.skipPipeline) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-26 11:34:00 +00:00
|
|
|
file.content = plugin.runPipeline({targetFile: file.filename, source: file.content});
|
2017-01-29 05:58:06 +00:00
|
|
|
file.modified = true;
|
2017-01-26 11:34:00 +00:00
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
self.logger.error(err.message);
|
|
|
|
}
|
2017-01-15 19:30:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-29 05:58:06 +00:00
|
|
|
return file;
|
2016-08-21 16:02:02 +00:00
|
|
|
}
|
2017-01-29 05:58:06 +00:00
|
|
|
});
|
2016-08-21 16:02:02 +00:00
|
|
|
|
|
|
|
var dir = targetFile.split('/').slice(0, -1).join('/');
|
2017-02-16 00:32:29 +00:00
|
|
|
self.logger.trace("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
|
|
|
|
2017-01-29 05:58:06 +00:00
|
|
|
// if it's a directory
|
|
|
|
if (targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1) {
|
|
|
|
var targetDir = targetFile;
|
|
|
|
|
|
|
|
if (targetDir.slice(-1) !== '/') {
|
|
|
|
targetDir = targetDir + '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
contentFiles.map(function(file) {
|
|
|
|
var filename = file.filename.replace('app/', '');
|
|
|
|
filename = filename.replace(targetDir, '');
|
2017-02-16 01:56:18 +00:00
|
|
|
self.logger.info("writing file " + (self.buildDir + targetDir + filename).bold.dim);
|
2017-01-29 05:58:06 +00:00
|
|
|
|
2017-02-03 11:30:08 +00:00
|
|
|
fs.writeFileSync(self.buildDir + targetDir + filename, fs.readFileSync(file.path));
|
2017-01-29 05:58:06 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var content = contentFiles.map(function(file) {
|
|
|
|
return file.content;
|
|
|
|
}).join("\n");
|
|
|
|
|
2017-02-16 01:56:18 +00:00
|
|
|
self.logger.info("writing file " + (this.buildDir + targetFile).bold.dim);
|
2017-01-29 05:58:06 +00:00
|
|
|
fs.writeFileSync(this.buildDir + targetFile, content);
|
|
|
|
}
|
2016-08-21 16:02:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Pipeline;
|
|
|
|
|