feat(@embark/generator): transpile embarkjs.js to be used by node

This commit is contained in:
Jonathan Rainville 2019-04-12 12:15:26 -04:00 committed by Michael Bradley
parent 79d96c3b4c
commit ae88cc612d
4 changed files with 54 additions and 19 deletions

View File

@ -91,6 +91,7 @@
"storage": "storage.json", "storage": "storage.json",
"communication": "communication.json", "communication": "communication.json",
"embarkjs": "embarkjs.js", "embarkjs": "embarkjs.js",
"embarkjsnode": "embarkjs.node.js",
"contractsJs": "contracts", "contractsJs": "contracts",
"symlinkDir": "modules" "symlinkDir": "modules"
}, },

View File

@ -1,6 +1,8 @@
const ws = new WebSocket(`${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.hostname}:${location.port}`); if (typeof WebSocket !== 'undefined') {
ws.addEventListener('message', (evt) => { const ws = new WebSocket(`${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.hostname}:${location.port}`);
if(evt.data === 'outputDone') { ws.addEventListener('message', (evt) => {
location.reload(true); if (evt.data === 'outputDone') {
} location.reload(true);
}); }
});
}

View File

@ -1,5 +1,6 @@
let async = require('async'); let async = require('async');
import {joinPath} from 'embark-utils'; import {joinPath} from 'embark-utils';
import { transform } from "@babel/core";
const constants = require('../../constants'); const constants = require('../../constants');
const path = require('path'); const path = require('path');
@ -153,14 +154,16 @@ class CodeGenerator {
}); });
} }
generateContractConfig(contractConfig, callback) { generateContractConfig(contractConfig, callback = () => {}) {
this.dappConfigs.blockchain = { this.dappConfigs.blockchain = {
dappConnection: contractConfig.dappConnection, dappConnection: contractConfig.dappConnection,
dappAutoEnable: contractConfig.dappAutoEnable, dappAutoEnable: contractConfig.dappAutoEnable,
warnIfMetamask: this.blockchainConfig.isDev, warnIfMetamask: this.blockchainConfig.isDev,
blockchainClient: this.blockchainConfig.ethereumClientName blockchainClient: this.blockchainConfig.ethereumClientName
}; };
this.generateArtifact(this.dappConfigs.blockchain, constants.dappArtifacts.blockchain, constants.dappArtifacts.dir, callback); this.generateArtifact(this.dappConfigs.blockchain, constants.dappArtifacts.blockchain, constants.dappArtifacts.dir, (err, path, _updated) => {
callback(err, path);
});
} }
generateStorageConfig(storageConfig) { generateStorageConfig(storageConfig) {
@ -192,15 +195,17 @@ class CodeGenerator {
}, },
(needsUpdate, next) => { (needsUpdate, next) => {
if (!needsUpdate) { if (!needsUpdate) {
return next(); return next(null, false);
} }
this.fs.writeFile(filePath, artifactInput, next); this.fs.writeFile(filePath, artifactInput, (err) => {
next(err, true);
});
} }
], (err) => { ], (err, updated) => {
if (err) { if (err) {
this.logger.error(err.message || err); this.logger.error(err.message || err);
} }
cb(err, filePath); cb(err, filePath, updated);
}); });
} }
@ -332,9 +337,8 @@ class CodeGenerator {
this.logger.error(__('Error creating a symlink to EmbarkJS')); this.logger.error(__('Error creating a symlink to EmbarkJS'));
return next(err); return next(err);
} }
embarkjsCode += `\nconst EmbarkJS = require("${symlinkDest}").default;`; embarkjsCode += `\nconst EmbarkJS = require("${symlinkDest}").default || require("${symlinkDest}");`;
embarkjsCode += "\nexport default EmbarkJS;"; embarkjsCode += "\nglobal.EmbarkJS = EmbarkJS;";
embarkjsCode += "\nglobal.EmbarkJS = EmbarkJS";
next(); next();
}); });
}, },
@ -346,12 +350,38 @@ class CodeGenerator {
code += self.generateStorageInitialization(true); code += self.generateStorageInitialization(true);
code += self.generateNamesInitialization(true); code += self.generateNamesInitialization(true);
code += self.getReloadPageCode(); code += self.getReloadPageCode();
code += "\nexport default EmbarkJS;";
code += "\nif (typeof module !== 'undefined' && module.exports) {" +
"\n\tmodule.exports = EmbarkJS;" +
"\n}";
code += '\n/* eslint-enable */'; code += '\n/* eslint-enable */';
next(); next();
}, },
function writeFile(next) { function writeFile(next) {
self.generateArtifact(code, constants.dappArtifacts.embarkjs, '', next); self.generateArtifact(code, constants.dappArtifacts.embarkjs, '', next);
},
function transformCode(artifactPath, updated, next) {
if (!updated) {
return next();
}
transform(code, {
cwd: self.fs.embarkPath(),
"presets": [
[
"@babel/preset-env", {
"targets": {
"node": "8.11.3"
}
}
]
]
}, (err, result) => {
if (err) {
return next(err);
}
self.generateArtifact(result.code, constants.dappArtifacts.embarkjsnode, '', next);
});
} }
], function(_err, _result) { ], function(_err, _result) {
cb(); cb();
@ -359,7 +389,7 @@ class CodeGenerator {
} }
getReloadPageCode() { getReloadPageCode() {
return this.env === 'development' ? this.fs.readFileSync(require('path').join(__dirname,'/code/reload-on-change.js'), 'utf8') : ''; return this.env === 'development' ? this.fs.readFileSync(require('path').join(__dirname, '/code/reload-on-change.js'), 'utf8') : '';
} }
getEmbarkJsProviderCode() { getEmbarkJsProviderCode() {
@ -396,7 +426,9 @@ class CodeGenerator {
contractCode += "export default " + contractName + ";\n"; contractCode += "export default " + contractName + ";\n";
this.generateArtifact(contractCode, contractName + '.js', constants.dappArtifacts.contractsJs, cb); this.generateArtifact(contractCode, contractName + '.js', constants.dappArtifacts.contractsJs, (err, path, _updated) => {
cb(err, path);
});
} }
generateSymlink(target, name, callback) { generateSymlink(target, name, callback) {

View File

@ -128,7 +128,7 @@ Blockchain.doConnect = function(connectionList, opts, doneCb) {
const connectWeb3 = async (next) => { const connectWeb3 = async (next) => {
const connectionString = 'web3://'; const connectionString = 'web3://';
if (window.ethereum) { if (typeof window !== 'undefined' && window.ethereum) {
try { try {
if (Blockchain.autoEnable) { if (Blockchain.autoEnable) {
await ethereum.enable(); await ethereum.enable();
@ -212,7 +212,7 @@ Blockchain.doConnect = function(connectionList, opts, doneCb) {
}; };
Blockchain.enableEthereum = function() { Blockchain.enableEthereum = function() {
if (window.ethereum) { if (typeof window !== 'undefined' && window.ethereum) {
return ethereum.enable().then((accounts) => { return ethereum.enable().then((accounts) => {
this.blockchainConnector.setProvider(ethereum); this.blockchainConnector.setProvider(ethereum);
this.blockchainConnector.setDefaultAccount(accounts[0]); this.blockchainConnector.setDefaultAccount(accounts[0]);