embark-area-51/lib/pipeline/pipeline.js

111 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-09-25 01:23:57 +00:00
/*jshint esversion: 6, loopfunc: true */
2017-03-29 17:50:05 +00:00
let fs = require('../core/fs.js');
2016-08-21 16:02:02 +00:00
2017-03-30 11:12:39 +00:00
class Pipeline {
2017-03-30 11:12:39 +00:00
constructor(options) {
this.buildDir = options.buildDir;
this.contractsFiles = options.contractsFiles;
this.assetFiles = options.assetFiles;
this.logger = options.logger;
this.plugins = options.plugins;
}
2017-04-04 10:37:50 +00:00
build(abi, contractsJSON, path) {
2017-03-30 11:12:39 +00:00
let self = this;
this.buildContracts(contractsJSON);
2017-03-30 11:12:39 +00:00
for (let targetFile in this.assetFiles) {
2016-08-21 16:02:02 +00:00
2017-03-30 11:12:39 +00:00
let contentFiles = this.assetFiles[targetFile].map(file => {
self.logger.trace("reading " + file.filename);
2016-08-21 16:02:02 +00:00
2017-03-30 11:12:39 +00:00
let pipelinePlugins = this.plugins.getPluginsFor('pipeline');
2017-01-29 05:58:06 +00:00
if (file.filename[0] === '$') {
let contractName = file.filename.substr(1);
return {content: this.buildContractJS(contractName), filename: contractName + ".js", path: file.path, modified: true};
}
else if (file.filename === 'embark.js') {
2017-03-30 11:12:39 +00:00
return {content: file.content + "\n" + abi, filename: file.filename, path: file.path, modified: true};
} else if (file.filename === 'abi.js') {
return {content: abi, filename: file.filename, path: file.path, modified: true};
} else if (['web3.js', 'ipfs.js', 'ipfs-api.js', 'orbit.js'].indexOf(file.filename) >= 0) {
file.modified = true;
return file;
} else {
2017-01-29 05:58:06 +00:00
2017-03-30 11:12:39 +00:00
if (pipelinePlugins.length > 0) {
pipelinePlugins.forEach(function (plugin) {
try {
if (file.options && file.options.skipPipeline) {
return;
}
file.content = plugin.runPipeline({targetFile: file.filename, source: file.content});
file.modified = true;
}
catch (err) {
self.logger.error(err.message);
}
});
}
2017-01-29 05:58:06 +00:00
2017-03-30 11:12:39 +00:00
return file;
}
2017-01-29 05:58:06 +00:00
});
2017-03-30 11:12:39 +00:00
let dir = targetFile.split('/').slice(0, -1).join('/');
self.logger.trace("creating dir " + this.buildDir + dir);
fs.mkdirpSync(this.buildDir + dir);
// if it's a directory
if (targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1) {
let targetDir = targetFile;
if (targetDir.slice(-1) !== '/') {
targetDir = targetDir + '/';
}
contentFiles.map(function (file) {
let filename = file.filename.replace('app/', '');
filename = filename.replace(targetDir, '');
self.logger.info("writing file " + (self.buildDir + targetDir + filename).bold.dim);
fs.copySync(self.buildDir + targetDir + filename, file.path, {overwrite: true});
});
} else {
let content = contentFiles.map(function (file) {
return file.content;
}).join("\n");
self.logger.info("writing file " + (this.buildDir + targetFile).bold.dim);
fs.writeFileSync(this.buildDir + targetFile, content);
}
2017-01-29 05:58:06 +00:00
}
2017-04-04 10:37:50 +00:00
}
buildContracts(contractsJSON) {
fs.mkdirpSync(this.buildDir + 'contracts');
for (let className in contractsJSON) {
let contract = contractsJSON[className];
fs.writeJSONSync(this.buildDir + 'contracts/' + className + ".json", contract, {spaces: 2});
}
2016-08-21 16:02:02 +00:00
}
buildContractJS(contractName) {
let contractJSON = fs.readFileSync('dist/contracts/' + contractName + '.json').toString();
let contractCode = "";
contractCode += contractName + "JSONConfig = " + contractJSON + ";\n";
contractCode += contractName + " = new EmbarkJS.Contract(" + contractName + "JSONConfig);\n";
return contractCode;
}
2017-03-30 11:12:39 +00:00
}
2016-08-21 16:02:02 +00:00
module.exports = Pipeline;