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');
|
2017-07-05 12:35:51 +00:00
|
|
|
let async = require('async');
|
2017-12-14 00:49:05 +00:00
|
|
|
//var Plugins = require('../core/plugins.js');
|
2017-12-12 17:20:57 +00:00
|
|
|
var utils = require('../utils/utils.js');
|
2017-12-14 00:49:05 +00:00
|
|
|
//var Npm = require('../pipeline/npm.js');
|
|
|
|
//let currentWeb3Version = require('../../package.json').dependencies.web3.replace("^","");
|
2017-12-12 17:20:57 +00:00
|
|
|
const webpack = require("webpack");
|
2016-08-21 16:02:02 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Pipeline {
|
2017-01-15 19:30:41 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
constructor(options) {
|
|
|
|
this.buildDir = options.buildDir;
|
|
|
|
this.contractsFiles = options.contractsFiles;
|
|
|
|
this.assetFiles = options.assetFiles;
|
2017-12-12 17:20:57 +00:00
|
|
|
this.events = options.events;
|
2017-03-30 11:12:39 +00:00
|
|
|
this.logger = options.logger;
|
|
|
|
this.plugins = options.plugins;
|
|
|
|
}
|
2017-01-15 19:30:41 +00:00
|
|
|
|
2017-07-05 12:35:51 +00:00
|
|
|
build(abi, contractsJSON, path, callback) {
|
2017-03-30 11:12:39 +00:00
|
|
|
let self = this;
|
2017-06-27 22:18:29 +00:00
|
|
|
|
|
|
|
this.buildContracts(contractsJSON);
|
|
|
|
|
2017-12-12 21:10:12 +00:00
|
|
|
self.buildWeb3JS(function() {
|
|
|
|
|
2017-07-05 12:35:51 +00:00
|
|
|
// limit:1 due to issues when downloading required files such as web3.js
|
2017-12-12 21:10:12 +00:00
|
|
|
async.eachOfLimit(self.assetFiles, 1, function (files, targetFile, cb) {
|
2017-12-12 17:20:57 +00:00
|
|
|
// limit:1 due to issues when downloading required files such as web3.js
|
|
|
|
async.mapLimit(files, 1,
|
|
|
|
function(file, fileCb) {
|
|
|
|
self.logger.trace("reading " + file.filename);
|
|
|
|
|
|
|
|
if (file.filename.indexOf('.js') >= 0) {
|
2017-12-12 19:37:47 +00:00
|
|
|
let importsList = {};
|
2017-12-12 17:20:57 +00:00
|
|
|
|
2017-12-13 14:01:53 +00:00
|
|
|
//importsList["Embark/EmbarkJS"] = fs.embarkPath("js/embark.js");
|
|
|
|
importsList["Embark/EmbarkJS"] = utils.joinPath(fs.dappPath(), ".embark", 'embark.js');
|
2017-12-12 21:10:12 +00:00
|
|
|
importsList["Embark/web3"] = utils.joinPath(fs.dappPath(), ".embark", 'web3_instance.js');
|
2017-12-14 00:51:34 +00:00
|
|
|
//importsList["Embark/libs/IpfsApi"] = fs.embarkPath('js/ipfs.js');
|
2017-12-12 17:20:57 +00:00
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function findImports(next) {
|
|
|
|
webpack({
|
|
|
|
entry: utils.joinPath(fs.dappPath(), file.filename),
|
|
|
|
output: {
|
|
|
|
libraryTarget: 'umd',
|
|
|
|
path: utils.joinPath(fs.dappPath(), '.embark'),
|
|
|
|
filename: file.filename
|
|
|
|
},
|
|
|
|
externals: function(context, request, callback) {
|
|
|
|
if (request === utils.joinPath(fs.dappPath(), file.filename)) {
|
|
|
|
callback();
|
|
|
|
} else {
|
2017-12-12 19:37:47 +00:00
|
|
|
if (request.indexOf('Embark/contracts/') === 0) {
|
2017-12-12 17:20:57 +00:00
|
|
|
let contractName = request.split('/')[2];
|
|
|
|
let contractCode = self.buildContractJS(contractName);
|
|
|
|
let filePath = utils.joinPath(fs.dappPath(), ".embark", contractName + '.js');
|
|
|
|
fs.writeFileSync(filePath, contractCode);
|
|
|
|
importsList[request] = filePath;
|
|
|
|
}
|
|
|
|
callback(null, "amd " + Math.random());
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 00:49:05 +00:00
|
|
|
}).run((_err, _stats) => {
|
2017-12-12 17:20:57 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function runWebpack(next) {
|
|
|
|
webpack({
|
|
|
|
entry: utils.joinPath(fs.dappPath(), file.filename),
|
|
|
|
output: {
|
|
|
|
libraryTarget: 'umd',
|
|
|
|
path: utils.joinPath(fs.dappPath(), '.embark'),
|
|
|
|
filename: file.filename
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
alias: importsList
|
|
|
|
},
|
|
|
|
externals: function(context, request, callback) {
|
|
|
|
if (request === "Embark/contracts/all") {
|
|
|
|
return callback(null, fs.readFileSync(utils.joinPath(fs.dappPath(), '.embark', 'embark.js')));
|
2017-12-13 14:01:53 +00:00
|
|
|
// should be .toString() ?
|
2017-12-12 17:20:57 +00:00
|
|
|
}
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
2017-12-16 17:01:45 +00:00
|
|
|
use: [{loader: "style-loader"}, {loader: "css-loader"}]
|
2017-12-14 00:24:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
2017-12-16 17:01:45 +00:00
|
|
|
use: [{loader: "style-loader"}, {loader: "css-loader"}]
|
2017-12-14 00:24:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
|
|
|
|
loader: 'url-loader?limit=100000'
|
2017-12-12 17:20:57 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2017-12-14 00:49:05 +00:00
|
|
|
}).run((_err, _stats) => {
|
2017-12-12 17:20:57 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:49:05 +00:00
|
|
|
], function(_err, _result) {
|
2018-01-03 18:44:35 +00:00
|
|
|
if (!fs.existsSync('./.embark/' + file.filename)) {
|
|
|
|
self.logger.error("coudln't find file: " + file.filename);
|
|
|
|
return fileCb("couldn't find file: " + file.filename);
|
|
|
|
}
|
2017-12-12 17:20:57 +00:00
|
|
|
let fileContent = fs.readFileSync('./.embark/' + file.filename).toString();
|
|
|
|
fileCb(null, {content: fileContent, filename: file.filename, path: file.path, modified: true});
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
file.content(function(fileContent) {
|
|
|
|
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, modified: true});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
function (err, contentFiles) {
|
2018-01-03 18:44:35 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.warn('errors found while generating ' + targetFile);
|
|
|
|
}
|
2017-12-12 17:20:57 +00:00
|
|
|
let dir = targetFile.split('/').slice(0, -1).join('/');
|
|
|
|
self.logger.trace("creating dir " + self.buildDir + dir);
|
|
|
|
fs.mkdirpSync(self.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);
|
2017-01-29 05:58:06 +00:00
|
|
|
|
2017-12-12 17:20:57 +00:00
|
|
|
fs.copySync(self.buildDir + targetDir + filename, file.path, {overwrite: true});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
let content = contentFiles.map(function (file) {
|
|
|
|
if (file === undefined) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return file.content;
|
|
|
|
}).join("\n");
|
|
|
|
|
|
|
|
self.logger.info("writing file " + (self.buildDir + targetFile).bold.dim);
|
|
|
|
fs.writeFileSync(self.buildDir + targetFile, content);
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function (_err, _results) {
|
|
|
|
callback();
|
|
|
|
});
|
2017-12-12 21:10:12 +00:00
|
|
|
});
|
2017-12-12 17:20:57 +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
|
|
|
}
|
2017-06-28 00:27:24 +00:00
|
|
|
|
|
|
|
buildContractJS(contractName) {
|
|
|
|
let contractJSON = fs.readFileSync('dist/contracts/' + contractName + '.json').toString();
|
|
|
|
|
|
|
|
let contractCode = "";
|
2017-12-12 18:51:14 +00:00
|
|
|
contractCode += "import web3 from 'Embark/web3';\n";
|
2017-12-12 17:20:57 +00:00
|
|
|
contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n";
|
2017-07-02 02:04:29 +00:00
|
|
|
contractCode += "let " + contractName + "JSONConfig = " + contractJSON + ";\n";
|
|
|
|
contractCode += "let " + contractName + " = new EmbarkJS.Contract(" + contractName + "JSONConfig);\n";
|
2017-12-12 17:20:57 +00:00
|
|
|
|
|
|
|
contractCode += "\n__embarkContext.execWhenReady(function() {\n";
|
|
|
|
contractCode += "\n" + contractName + ".setProvider(web3.currentProvider);\n";
|
|
|
|
contractCode += "\n});\n";
|
|
|
|
|
|
|
|
contractCode += "export default " + contractName + ";\n";
|
2017-06-28 00:27:24 +00:00
|
|
|
|
|
|
|
return contractCode;
|
|
|
|
}
|
2017-12-12 19:45:20 +00:00
|
|
|
|
|
|
|
buildWeb3JS(cb) {
|
|
|
|
this.events.request('provider-code', function(providerCode) {
|
|
|
|
let code = "";
|
|
|
|
code += "\nimport Web3 from '" + utils.joinPath(fs.embarkPath("js/web3-1.0.min.js")) + "'\n";
|
|
|
|
|
|
|
|
code += "\n if (typeof web3 !== 'undefined') {";
|
|
|
|
code += "\n } else {";
|
|
|
|
code += "\n var web3 = new Web3();\n";
|
|
|
|
code += "\n }";
|
|
|
|
|
|
|
|
code += providerCode;
|
|
|
|
code += "\nglobal.__embarkContext = __mainContext.__loadManagerInstance;\n";
|
|
|
|
code += "\nwindow.web3 = web3;\n";
|
|
|
|
code += "\nexport default web3;\n";
|
|
|
|
let filePath = utils.joinPath(fs.dappPath(), ".embark", 'web3_instance.js');
|
2017-12-12 21:10:12 +00:00
|
|
|
fs.mkdirpSync(utils.joinPath(fs.dappPath(), ".embark"));
|
2017-12-12 19:45:20 +00:00
|
|
|
fs.writeFileSync(filePath, code);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2016-08-21 16:02:02 +00:00
|
|
|
|
|
|
|
module.exports = Pipeline;
|