embark/lib/pipeline/pipeline.js

267 lines
16 KiB
JavaScript
Raw Normal View History

const fs = require('../core/fs.js');
const async = require('async');
2018-05-16 20:41:15 +00:00
const ProcessLauncher = require('../process/processLauncher');
const utils = require('../utils/utils.js');
const constants = require('../constants');
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");
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.events = options.events;
this.logger = options.logger;
2018-05-10 15:14:25 +00:00
this.normalizeInput = options.normalizeInput;
this.plugins = options.plugins;
2018-05-10 14:48:06 +00:00
this.pipelinePlugins = this.plugins.getPluginsFor('pipeline');
2017-03-30 11:12:39 +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 = {};
let placeholderPage;
2018-05-08 13:36:50 +00:00
async.waterfall([
function createPlaceholderPage(next){
let html = `
<!doctype html>
<html>
<head>
<title>${__('Embark is building, please wait...')}</title>
<meta http-equiv="refresh" content="4">
<style>
body{background:#f2f2f5;background-size:cover;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif}.blobs{-webkit-filter:url("#goo");filter:url("#goo");position:absolute;top:0;left:-2.5%;bottom:0;right:0}@-webkit-keyframes blob-left-top-anim{0%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}33%{-webkit-transform:scale(.9) translate(-95px,0);transform:scale(.9) translate(-95px,0)}62%{-webkit-transform:scale(.7) translate(-95px,-95px);transform:scale(.7) translate(-95px,-95px)}94%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}}@keyframes blob-left-top-anim{0%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}33%{-webkit-transform:scale(.9) translate(-95px,0);transform:scale(.9) translate(-95px,0)}62%{-webkit-transform:scale(.7) translate(-95px,-95px);transform:scale(.7) translate(-95px,-95px)}94%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}}@-webkit-keyframes blob-right-top-anim{0%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}33%{-webkit-transform:scale(.9) translate(95px,0);transform:scale(.9) translate(95px,0)}64%{-webkit-transform:scale(.7) translate(95px,-95px);transform:scale(.7) translate(95px,-95px)}96%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}}@keyframes blob-right-top-anim{0%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}33%{-webkit-transform:scale(.9) translate(95px,0);transform:scale(.9) translate(95px,0)}64%{-webkit-transform:scale(.7) translate(95px,-95px);transform:scale(.7) translate(95px,-95px)}96%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}}@-webkit-keyframes blob-left-bottom-anim{0%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}33%{-webkit-transform:scale(.9) translate(-95px,0);transform:scale(.9) translate(-95px,0)}66%{-webkit-transform:scale(.7) translate(-95px,95px);transform:scale(.7) translate(-95px,95px)}98%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}}@keyframes blob-left-bottom-anim{0%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}33%{-webkit-transform:scale(.9) translate(-95px,0);transform:scale(.9) translate(-95px,0)}66%{-webkit-transform:scale(.7) translate(-95px,95px);transform:scale(.7) translate(-95px,95px)}98%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}}@-webkit-keyframes blob-right-bottom-anim{0%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}33%{-webkit-transform:scale(.9) translate(95px,0);transform:scale(.9) translate(95px,0)}68%{-webkit-transform:scale(.7) translate(95px,95px);transform:scale(.7) translate(95px,95px)}100%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}}@keyframes blob-right-bottom-anim{0%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}33%{-webkit-transform:scale(.9) translate(95px,0);transform:scale(.9) translate(95px,0)}68%{-webkit-transform:scale(.7) translate(95px,95px);transform:scale(.7) translate(95px,95px)}100%{-webkit-transform:scale(1.1) translate(0,0);transform:scale(1.1) translate(0,0)}}.blob{position:absolute;background-color:#363763;background-image:url('data:image/svg+xml;charset=UTF-8,<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg"><path d="M20 17H4l8-10z" fill-rule="evenodd" opacity=".05"/></svg>');left:50%;top:50%;width:150px;height:150px;line-height:150px;text-align:center;color:white;font-size:40px;border-radius:100%;margin-top:-50px;margin-left:-50px;-webkit-animation:blob-left-top-anim cubic-bezier(.77,0,.175,1) 4s infinite;animation:blob-left-top-anim cubic-bezier(.77,0,.175,1) 4s infinite}.blob:before{position:absolute;width:100%;height:100%;background-repeat:no-repeat;background-image:url('data:image/svg+xml;charset=UTF-8,<svg width="117" height="27" xmlns="http://www.w3.org/2000/
</style>
</head>
<body>
<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
<div class="blob"></div>
<div class="blob"></div>
</div>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="goo">
<feGaussianBlur in="SourceGraphic" stdDeviation="10" result="blur" />
<feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 18 -7" result="goo" />
<feBlend in="SourceGraphic" in2="goo" />
</filter>
</defs>
</svg>
<h1 class="loading-msg">${__('Embark is building, please wait...')}</h1>
</body>
</html>`;
fs.writeFile(self.buildDir + 'index.html', html, next);
},
2018-05-08 13:36:50 +00:00
function buildTheContracts(next) {
2018-05-16 16:48:17 +00:00
self.buildContracts(next);
2018-05-08 13:36:50 +00:00
},
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');
self.plugins.getPluginsProperty('imports', 'imports').forEach(function (importObject) {
2018-05-08 13:36:50 +00:00
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) {
2018-05-16 16:48:17 +00:00
self.events.request('contracts:list', (contracts) => {
async.each(contracts, (contract, eachCb) => {
self.events.request('code-generator:contract', contract.className, (contractCode) => {
let filePath = fs.dappPath(".embark", contract.className + '.js');
importsList["Embark/contracts/" + contract.className] = filePath;
fs.writeFile(filePath, contractCode, eachCb);
});
}, next);
});
2018-05-08 13:36:50 +00:00
},
function assetFileWrite(next) {
2018-05-10 14:43:10 +00:00
async.eachOf(self.assetFiles, function (files, targetFile, cb) {
async.map(files,
2018-05-08 13:36:50 +00:00
function (file, fileCb) {
self.logger.trace("reading " + file.filename);
2018-05-08 13:36:50 +00:00
2018-05-10 14:48:06 +00:00
// Not a JS file
2018-05-08 13:36:50 +00:00
if (file.filename.indexOf('.js') < 0) {
return file.content(function (fileContent) {
self.runPlugins(file, fileContent, fileCb);
});
}
2018-05-08 13:36:50 +00:00
// JS files
async.waterfall([
function runWebpack(next) {
2018-05-16 20:41:15 +00:00
const webpackProcess = new ProcessLauncher({
modulePath: utils.joinPath(__dirname, 'webpackProcess.js'),
logger: self.logger,
events: self.events,
2018-05-16 20:41:15 +00:00
normalizeInput: self.normalizeInput
});
webpackProcess.send({action: constants.pipeline.init, options: {}});
webpackProcess.send({action: constants.pipeline.build, file, importsList});
2018-05-18 18:25:20 +00:00
webpackProcess.once('result', constants.pipeline.built, (msg) => {
2018-05-16 20:41:15 +00:00
webpackProcess.disconnect();
return next(msg.error);
});
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) {
self.logger.error(err);
2018-05-08 13:36:50 +00:00
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) {
2018-05-08 21:49:46 +00:00
self.logger.error(__('errors found while generating') + ' ' + targetFile);
2018-05-08 13:36:50 +00:00
}
let dir = targetFile.split('/').slice(0, -1).join('/');
self.logger.trace("creating dir " + self.buildDir + dir);
2018-05-08 13:36:50 +00:00
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.logger.info("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");
2018-05-08 21:49:46 +00:00
self.logger.info(__("writing file") + " " + (self.buildDir + targetFile).bold.dim);
if(new RegExp(/^index.html?/i).test(targetFile)){
targetFile = targetFile.replace('index', 'index-temp');
placeholderPage = targetFile;
}
2018-05-08 13:36:50 +00:00
fs.writeFile(self.buildDir + targetFile, content, cb);
}
);
},
next);
},
function removePlaceholderPage(next){
let placeholderFile = self.buildDir + placeholderPage;
fs.access(self.buildDir + placeholderPage, (err) => {
if (err) return next(); // index-temp doesn't exist, do nothing
// rename index-temp.htm/l to index.htm/l, effectively replacing our placeholder page
// with the contents of the built index.html page
fs.move(placeholderFile, placeholderFile.replace('index-temp', 'index'), {overwrite: true}, next);
});
2018-05-08 13:36:50 +00:00
}
], 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-10 14:48:06 +00:00
if (self.pipelinePlugins.length <= 0) {
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-10 14:48:06 +00:00
async.eachSeries(self.pipelinePlugins,
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) {
self.logger.error(err.message);
2018-02-28 23:09:10 +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
}
);
}
buildContracts(cb) {
2018-01-10 15:43:25 +00:00
const self = this;
async.waterfall([
function makeDirectory(next) {
fs.mkdirp(fs.dappPath(self.buildDir, 'contracts'), (err, _result) => {
2018-05-16 19:18:12 +00:00
next(err);
});
},
function getContracts(next) {
self.events.request('contracts:list', (contracts) => {
next(null, contracts);
});
},
function writeContractsJSON(contracts, next) {
2018-05-16 16:48:17 +00:00
async.each(contracts, (contract, eachCb) => {
fs.writeJson(fs.dappPath(self.buildDir, 'contracts', contract.className + ".json"), contract, {spaces: 2}, eachCb);
2018-05-16 19:18:12 +00:00
}, () => { next(); });
}
], cb);
2016-08-21 16:02:02 +00:00
}
2018-05-15 22:21:00 +00:00
buildWeb3JS(cb) {
const self = this;
async.waterfall([
function makeDirectory(next) {
fs.mkdirp(fs.dappPath(".embark"), (err, _result) => {
next(err);
2018-01-10 15:43:25 +00:00
});
},
function getWeb3Code(next) {
self.events.request('code-generator:web3js', next);
},
2018-05-15 22:21:00 +00:00
function writeFile(code, next) {
fs.writeFile(fs.dappPath(".embark", 'web3_instance.js'), code, next);
2018-01-10 15:43:25 +00:00
}
], cb);
2017-12-12 19:45:20 +00:00
}
2018-05-15 22:21:00 +00:00
2017-03-30 11:12:39 +00:00
}
2016-08-21 16:02:02 +00:00
module.exports = Pipeline;