2018-05-07 18:30:30 +00:00
|
|
|
const fs = require('../core/fs.js');
|
|
|
|
const async = require('async');
|
|
|
|
const utils = require('../utils/utils.js');
|
2017-12-12 17:20:57 +00:00
|
|
|
const webpack = require("webpack");
|
2018-05-07 18:30:30 +00:00
|
|
|
const constants = require('../constants');
|
2018-05-07 20:33:30 +00:00
|
|
|
const File = require('../core/file');
|
2016-08-21 16:02:02 +00:00
|
|
|
|
2018-01-17 00:17:52 +00:00
|
|
|
require("babel-preset-react");
|
|
|
|
require("babel-preset-es2015");
|
|
|
|
require("babel-preset-es2016");
|
|
|
|
require("babel-preset-es2017");
|
|
|
|
|
2018-05-07 18:30:30 +00:00
|
|
|
let pipeline;
|
|
|
|
|
2018-05-07 20:33:30 +00:00
|
|
|
// Override process.chdir so that we have a partial-implementation PWD for Windows
|
|
|
|
const realChdir = process.chdir;
|
|
|
|
process.chdir = (...args) => {
|
|
|
|
if (!process.env.PWD) {
|
|
|
|
process.env.PWD = process.cwd();
|
|
|
|
}
|
|
|
|
realChdir(...args);
|
|
|
|
};
|
|
|
|
|
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;
|
2018-05-07 18:30:30 +00:00
|
|
|
this.pipelinePlugins = options.pipelinePlugins;
|
|
|
|
this.pluginImports = options.pluginImports;
|
2018-05-07 19:48:01 +00:00
|
|
|
this.web3Location = options.web3Location;
|
|
|
|
this.providerCode = options.providerCode;
|
2018-05-07 18:30:30 +00:00
|
|
|
|
|
|
|
this.interceptLogs();
|
|
|
|
}
|
|
|
|
|
|
|
|
interceptLogs() {
|
|
|
|
const context = {};
|
|
|
|
context.console = console;
|
|
|
|
|
2018-05-08 13:04:53 +00:00
|
|
|
context.console.log = this.log;
|
|
|
|
context.console.warn = this.log;
|
|
|
|
context.console.info = this.log;
|
|
|
|
context.console.debug = this.log;
|
|
|
|
context.console.trace = this.log;
|
|
|
|
context.console.dir = this.log;
|
2018-05-07 18:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log() {
|
|
|
|
process.send({result: constants.pipeline.log, message: arguments});
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
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;
|
2018-05-08 13:36:50 +00:00
|
|
|
const importsList = {};
|
2017-06-27 22:18:29 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
async.waterfall([
|
|
|
|
function buildTheContracts(next) {
|
|
|
|
self.buildContracts(contractsJSON, next);
|
|
|
|
},
|
|
|
|
function buildWeb3(next) {
|
|
|
|
self.buildWeb3JS(next);
|
|
|
|
},
|
|
|
|
function createImportList(next) {
|
|
|
|
importsList["Embark/EmbarkJS"] = fs.dappPath(".embark", 'embark.js');
|
|
|
|
importsList["Embark/web3"] = fs.dappPath(".embark", 'web3_instance.js');
|
2017-06-27 22:18:29 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
self.pluginImports.forEach(function (importObject) {
|
|
|
|
let [importName, importLocation] = importObject;
|
|
|
|
importsList[importName] = importLocation;
|
|
|
|
});
|
2017-12-12 21:10:12 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
next();
|
|
|
|
},
|
|
|
|
function writeContracts(next) {
|
|
|
|
async.each(Object.keys(contractsJSON), (contractName, eachCb) => {
|
2018-05-08 13:41:19 +00:00
|
|
|
self.buildContractJS(contractName, (err, contractCode) => {
|
|
|
|
let filePath = fs.dappPath(".embark", contractName + '.js');
|
|
|
|
importsList["Embark/contracts/" + contractName] = filePath;
|
|
|
|
fs.writeFile(filePath, contractCode, eachCb);
|
|
|
|
});
|
2018-05-08 13:36:50 +00:00
|
|
|
}, next);
|
|
|
|
},
|
|
|
|
function assetFileWrite(next) {
|
|
|
|
// limit:1 due to issues when downloading required files such as web3.js
|
|
|
|
async.eachOfLimit(self.assetFiles, 1, function (files, targetFile, cb) {
|
|
|
|
// limit:1 due to issues when downloading required files such as web3.js
|
|
|
|
async.mapLimit(files, 1,
|
|
|
|
function (file, fileCb) {
|
|
|
|
file = new File(file); // Re-instantiate a File as through the process, we lose its prototype
|
|
|
|
self.log("reading " + file.filename);
|
|
|
|
|
|
|
|
if (file.filename.indexOf('.js') < 0) {
|
|
|
|
return file.content(function (fileContent) {
|
|
|
|
self.runPlugins(file, fileContent, fileCb);
|
|
|
|
});
|
|
|
|
}
|
2018-04-09 20:29:49 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
// JS files
|
|
|
|
let realCwd;
|
2018-04-09 20:29:49 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
async.waterfall([
|
2018-04-09 20:29:49 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
function findImports(next) {
|
|
|
|
self.webpackRun(file.filename, {}, false, importsList, false, next);
|
|
|
|
},
|
2018-05-08 13:04:53 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
function changeCwd(next) {
|
|
|
|
realCwd = utils.pwd();
|
|
|
|
process.chdir(fs.embarkPath(''));
|
|
|
|
next();
|
|
|
|
},
|
2018-05-08 13:25:37 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
function runWebpack(next) {
|
|
|
|
self.webpackRun(file.filename, {}, true, importsList, true, next);
|
|
|
|
},
|
2018-05-08 13:25:37 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
function changeCwdBack(next) {
|
|
|
|
process.chdir(realCwd);
|
|
|
|
next();
|
|
|
|
},
|
2018-05-08 13:25:37 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
function checkFile(next) {
|
|
|
|
fs.access('./.embark/' + file.filename, (err) => {
|
|
|
|
if (err) {
|
|
|
|
self.log("couldn't find file: " + file.filename);
|
|
|
|
return next("couldn't find file: " + file.filename);
|
|
|
|
}
|
2018-05-08 13:25:37 +00:00
|
|
|
next();
|
2018-05-08 13:36:50 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function readFile(next) {
|
|
|
|
fs.readFile('./.embark/' + file.filename, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
next(null, data.toString());
|
|
|
|
});
|
|
|
|
},
|
2018-05-08 13:25:37 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
function runPluginsOnContent(fileContent, next) {
|
|
|
|
self.runPlugins(file, fileContent, next);
|
|
|
|
}
|
2018-05-08 13:25:37 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
], function (err, contentFile) {
|
2018-05-08 13:04:53 +00:00
|
|
|
if (err) {
|
2018-05-08 13:36:50 +00:00
|
|
|
process.chdir(realCwd);
|
|
|
|
self.log(err);
|
|
|
|
return fileCb(err);
|
2018-05-08 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
fileCb(null, contentFile);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function (err, contentFiles) {
|
|
|
|
if (err) {
|
|
|
|
self.log('errors found while generating ' + targetFile);
|
|
|
|
}
|
|
|
|
let dir = targetFile.split('/').slice(0, -1).join('/');
|
|
|
|
self.log("creating dir " + self.buildDir + dir);
|
|
|
|
fs.mkdirpSync(self.buildDir + dir);
|
2018-05-08 13:02:46 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
// if it's a directory
|
|
|
|
if (targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1) {
|
|
|
|
let targetDir = targetFile;
|
2018-05-08 13:02:46 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
if (targetDir.slice(-1) !== '/') {
|
|
|
|
targetDir = targetDir + '/';
|
2018-05-08 13:25:37 +00:00
|
|
|
}
|
2018-05-08 13:04:53 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
async.each(contentFiles, function (file, mapCb) {
|
|
|
|
let filename = file.filename.replace(file.basedir + '/', '');
|
|
|
|
self.log("writing file " + (self.buildDir + targetDir + filename).bold.dim);
|
2018-05-08 13:25:37 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
fs.copy(file.path, self.buildDir + targetDir + filename, {overwrite: true}, mapCb);
|
|
|
|
}, cb);
|
|
|
|
return;
|
2018-05-08 13:25:37 +00:00
|
|
|
}
|
2018-05-08 13:36:50 +00:00
|
|
|
|
|
|
|
let content = contentFiles.map(function (file) {
|
|
|
|
if (file === undefined) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return file.content;
|
|
|
|
}).join("\n");
|
|
|
|
|
|
|
|
self.log("writing file " + (self.buildDir + targetFile).bold.dim);
|
|
|
|
fs.writeFile(self.buildDir + targetFile, content, cb);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
next);
|
|
|
|
}
|
|
|
|
], callback);
|
2017-12-12 17:20:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 23:09:10 +00:00
|
|
|
runPlugins(file, fileContent, fileCb) {
|
|
|
|
const self = this;
|
2018-05-07 18:30:30 +00:00
|
|
|
if (self.pipelinePlugins.length <= 0) {
|
2018-05-08 13:04:53 +00:00
|
|
|
return fileCb(null, {
|
|
|
|
content: fileContent,
|
|
|
|
filename: file.filename,
|
|
|
|
path: file.path,
|
|
|
|
basedir: file.basedir,
|
|
|
|
modified: true
|
|
|
|
});
|
2018-02-28 23:09:10 +00:00
|
|
|
}
|
2018-05-07 18:30:30 +00:00
|
|
|
async.eachSeries(self.pipelinePlugins,
|
2018-05-08 13:04:53 +00:00
|
|
|
function (plugin, pluginCB) {
|
2018-02-28 23:09:10 +00:00
|
|
|
if (file.options && file.options.skipPipeline) {
|
|
|
|
return pluginCB();
|
|
|
|
}
|
|
|
|
|
|
|
|
fileContent = plugin.runPipeline({targetFile: file.filename, source: fileContent});
|
|
|
|
file.modified = true;
|
|
|
|
pluginCB();
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
2018-05-07 18:30:30 +00:00
|
|
|
self.log(err.message);
|
2018-02-28 23:09:10 +00:00
|
|
|
}
|
2018-05-08 13:04:53 +00:00
|
|
|
return fileCb(null, {
|
|
|
|
content: fileContent,
|
|
|
|
filename: file.filename,
|
|
|
|
path: file.path,
|
|
|
|
basedir: file.basedir,
|
|
|
|
modified: true
|
|
|
|
});
|
2018-02-28 23:09:10 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:04:08 +00:00
|
|
|
webpackRun(filename, options, includeModules, importsList, detectErrors, callback) {
|
2018-01-17 20:29:34 +00:00
|
|
|
let defaultOptions = {
|
2018-04-02 19:30:16 +00:00
|
|
|
entry: fs.dappPath(filename),
|
2018-01-17 20:29:34 +00:00
|
|
|
output: {
|
|
|
|
libraryTarget: 'umd',
|
2018-04-02 19:30:16 +00:00
|
|
|
path: fs.dappPath('.embark'),
|
2018-01-17 20:29:34 +00:00
|
|
|
filename: filename
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
alias: importsList,
|
|
|
|
modules: [
|
|
|
|
fs.embarkPath('node_modules'),
|
2018-04-02 19:30:16 +00:00
|
|
|
fs.dappPath('node_modules')
|
2018-01-17 20:29:34 +00:00
|
|
|
]
|
|
|
|
},
|
2018-05-08 13:04:53 +00:00
|
|
|
externals: function (context, request, callback) {
|
2018-01-17 20:29:34 +00:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-08 13:04:53 +00:00
|
|
|
let webpackOptions = utils.recursiveMerge(defaultOptions, options);
|
2018-01-17 20:29:34 +00:00
|
|
|
|
|
|
|
if (includeModules) {
|
|
|
|
webpackOptions.module = {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
|
|
|
use: [{loader: "style-loader"}, {loader: "css-loader"}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
|
|
|
use: [{loader: "style-loader"}, {loader: "css-loader"}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
|
|
|
|
loader: 'url-loader?limit=100000'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.js$/,
|
|
|
|
loader: "babel-loader",
|
|
|
|
exclude: /(node_modules|bower_components)/,
|
|
|
|
options: {
|
2018-02-23 23:24:46 +00:00
|
|
|
presets: ['babel-preset-es2016', 'babel-preset-es2017', 'babel-preset-react'].map(require.resolve),
|
2018-02-24 00:37:42 +00:00
|
|
|
plugins: ["babel-plugin-webpack-aliases"].map(require.resolve),
|
|
|
|
compact: false
|
2018-01-17 20:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
webpack(webpackOptions).run((_err, _stats) => {
|
2018-05-07 19:48:01 +00:00
|
|
|
if (_err) {
|
|
|
|
console.log('ERROS');
|
|
|
|
console.log(_err);
|
|
|
|
}
|
2018-02-23 19:04:08 +00:00
|
|
|
if (!detectErrors) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_stats.hasErrors()) {
|
|
|
|
return callback(_stats.toJson().errors.join("\n"));
|
|
|
|
}
|
2018-01-17 20:29:34 +00:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
buildContracts(contractsJSON, callback) {
|
|
|
|
fs.mkdirp(fs.dappPath(this.buildDir, 'contracts'), (err) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
async.each(Object.keys(contractsJSON), (className, eachCb) => {
|
|
|
|
let contract = contractsJSON[className];
|
|
|
|
fs.writeJson(fs.dappPath(this.buildDir, 'contracts', className + ".json"), contract, {spaces: 2}, eachCb);
|
|
|
|
}, callback);
|
|
|
|
});
|
2016-08-21 16:02:02 +00:00
|
|
|
}
|
2017-06-28 00:27:24 +00:00
|
|
|
|
2018-05-08 13:41:19 +00:00
|
|
|
buildContractJS(contractName, callback) {
|
|
|
|
fs.readFile(fs.dappPath(this.buildDir, 'contracts', contractName + '.json'), (err, contractJSON) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
contractJSON = contractJSON.toString();
|
2017-12-12 17:20:57 +00:00
|
|
|
|
2018-05-08 13:41:19 +00:00
|
|
|
let contractCode = "";
|
|
|
|
contractCode += "import web3 from 'Embark/web3';\n";
|
|
|
|
contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n";
|
|
|
|
contractCode += "let " + contractName + "JSONConfig = " + contractJSON + ";\n";
|
|
|
|
contractCode += "let " + contractName + " = new EmbarkJS.Contract(" + contractName + "JSONConfig);\n";
|
2017-12-12 17:20:57 +00:00
|
|
|
|
2018-05-08 13:41:19 +00:00
|
|
|
contractCode += "\n__embarkContext.execWhenReady(function() {\n";
|
|
|
|
contractCode += "\n" + contractName + ".setProvider(web3.currentProvider);\n";
|
|
|
|
contractCode += "\n});\n";
|
2017-06-28 00:27:24 +00:00
|
|
|
|
2018-05-08 13:41:19 +00:00
|
|
|
contractCode += "export default " + contractName + ";\n";
|
|
|
|
callback(null, contractCode);
|
|
|
|
});
|
2017-06-28 00:27:24 +00:00
|
|
|
}
|
2017-12-12 19:45:20 +00:00
|
|
|
|
|
|
|
buildWeb3JS(cb) {
|
2018-01-10 15:43:25 +00:00
|
|
|
const self = this;
|
|
|
|
let code = "";
|
|
|
|
|
|
|
|
async.waterfall([
|
2018-05-07 19:48:01 +00:00
|
|
|
function getImports(next) {
|
|
|
|
self.web3Location = self.web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes
|
|
|
|
code += "\nimport Web3 from '" + self.web3Location + "';\n";
|
2018-01-10 15:43:25 +00:00
|
|
|
|
|
|
|
code += "\n if (typeof web3 !== 'undefined') {";
|
|
|
|
code += "\n } else {";
|
|
|
|
code += "\n var web3 = new Web3();\n";
|
|
|
|
code += "\n }";
|
|
|
|
|
2018-05-07 19:48:01 +00:00
|
|
|
code += self.providerCode;
|
|
|
|
code += "\nglobal.__embarkContext = __mainContext.__loadManagerInstance;\n";
|
|
|
|
code += "\nwindow.web3 = web3;\n";
|
|
|
|
code += "\nexport default web3;\n";
|
|
|
|
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
function makeDirectory(next) {
|
|
|
|
fs.mkdirp(fs.dappPath(".embark"), (err, _result) => {
|
|
|
|
next(err);
|
2018-01-10 15:43:25 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function writeFile(next) {
|
2018-05-07 19:48:01 +00:00
|
|
|
fs.writeFile(fs.dappPath(".embark", 'web3_instance.js'), code, next);
|
2018-01-10 15:43:25 +00:00
|
|
|
}
|
2018-05-07 19:48:01 +00:00
|
|
|
], cb);
|
2017-12-12 19:45:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2016-08-21 16:02:02 +00:00
|
|
|
|
2018-05-07 18:30:30 +00:00
|
|
|
|
|
|
|
process.on('message', (msg) => {
|
|
|
|
if (msg.action === constants.pipeline.init) {
|
|
|
|
pipeline = new Pipeline(msg.options);
|
|
|
|
return process.send({result: constants.pipeline.initiated});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.action === constants.pipeline.build) {
|
|
|
|
return pipeline.build(msg.abi, msg.contractsJSON, msg.path, (err, result) => {
|
|
|
|
process.send({result: constants.pipeline.built, error: err, data: result});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
process.on('exit', () => {
|
|
|
|
process.exit(0);
|
|
|
|
});
|