refactor(@embarkjs/web3): make web3 connector an internal plugin like the other embarkjs-* packages

Effectively deprecate the `embarkjs-connector-web3` package but don't introduce
a breaking change by simply not loading the plugin if it's specified in a
DApp's `embark.json`. If the deprecated plugin is specified, display a message
indicating the plugin was ignored and suggesting it be removed from the
project's `embark.json` and `package.json`.
This commit is contained in:
Michael Bradley, Jr 2019-06-12 15:26:59 -05:00 committed by Michael Bradley
parent 71cb161525
commit d684b9af0f
29 changed files with 424 additions and 27 deletions

View File

@ -14,7 +14,6 @@
"ipfs-api": "17.2.4"
},
"plugins": {
"embarkjs-connector-web3": {}
},
"options": {
"solc": {

View File

@ -35,7 +35,6 @@
"devDependencies": {
"embark": "^4.1.0-beta.3",
"embark-reset": "^4.1.0-beta.3",
"embarkjs-connector-web3": "^4.1.0-beta.3",
"npm-run-all": "4.1.5",
"rimraf": "2.6.3"
}

View File

@ -14,7 +14,6 @@
"ipfs-api": "17.2.4"
},
"plugins": {
"embarkjs-connector-web3": {}
},
"options": {
"solc": {

View File

@ -36,7 +36,6 @@
"bootstrap": "3.3.7",
"embark": "^4.1.0-beta.3",
"embark-reset": "^4.1.0-beta.3",
"embarkjs-connector-web3": "^4.1.0-beta.3",
"npm-run-all": "4.1.5",
"react": "16.7.0",
"react-bootstrap": "0.32.4",

View File

@ -13,7 +13,6 @@
"solc": "0.5.0"
},
"plugins": {
"embarkjs-connector-web3": {}
},
"options": {
"solc": {

View File

@ -31,7 +31,6 @@
"devDependencies": {
"embark": "^4.1.0-beta.3",
"embark-reset": "^4.1.0-beta.3",
"embarkjs-connector-web3": "^4.1.0-beta.3",
"npm-run-all": "4.1.5",
"rimraf": "2.6.3"
}

View File

@ -20,8 +20,7 @@
"ipfs-api": "17.2.7"
},
"plugins": {
"embark-dapp-test-service": {},
"embarkjs-connector-web3": {}
"embark-dapp-test-service": {}
},
"options": {
"solc": {

View File

@ -7,7 +7,6 @@
"embark-dapp-test-service": "^4.0.0",
"embark-i18n": "^4.1.0-beta.3",
"embark-reset": "^4.1.0-beta.3",
"embarkjs-connector-web3": "^4.1.0-beta.3",
"fs-extra": "7.0.1",
"haml": "0.4.3",
"jquery": "3.3.1",

View File

@ -16,6 +16,5 @@
"solc": "0.4.24"
},
"plugins": {
"embarkjs-connector-web3": {}
}
}

View File

@ -2,8 +2,7 @@
"description": "Test DApp for integration testing purposes",
"devDependencies": {
"embark": "^4.1.0-beta.3",
"embark-reset": "^4.1.0-beta.3",
"embarkjs-connector-web3": "^4.1.0-beta.3"
"embark-reset": "^4.1.0-beta.3"
},
"name": "embark-dapp-test-contracts",
"private": true,

View File

@ -79,14 +79,9 @@
"nohoist": [
"embark/embark-test-contract-0",
"embark/embark-test-contract-1",
"embark-dapp-template-boilerplate/embarkjs-connector-web3",
"embark-dapp-template-demo/bootstrap",
"embark-dapp-template-demo/embarkjs-connector-web3",
"embark-dapp-template-simple/embarkjs-connector-web3",
"embark-dapp-test-app/embark-dapp-test-service",
"embark-dapp-test-app/embarkjs-connector-web3",
"embark-dapp-test-app/zeppelin-solidity",
"embark-dapp-test-contracts/embarkjs-connector-web3"
"embark-dapp-test-app/zeppelin-solidity"
]
}
}

View File

@ -313,7 +313,7 @@ class CodeGenerator {
const self = this;
let embarkjsCode = '';
let code = "/* eslint-disable */";
const deps = ['ens', 'ipfs', 'swarm', 'whisper'];
const deps = ['web3', 'ens', 'ipfs', 'swarm', 'whisper'];
async.waterfall([
// TODO: here due to a race condition when running embark build

View File

@ -0,0 +1,6 @@
# `embark-web3`
> Module to add Web3 support to Embark
Visit [embark.status.im](https://embark.status.im/) to get started with
[Embark](https://github.com/embark-framework/embark).

View File

@ -0,0 +1,69 @@
{
"name": "embark-web3",
"version": "4.1.0-beta.3",
"author": "Iuri Matias <iuri.matias@gmail.com>",
"contributors": [],
"description": "Module to add Web3 support to Embark",
"homepage": "https://github.com/embark-framework/embark/tree/master/packages/embark-web3#readme",
"bugs": "https://github.com/embark-framework/embark/issues",
"keywords": [
"blockchain",
"dapps",
"ethereum",
"ipfs",
"serverless",
"solc",
"solidity"
],
"files": [
"dist"
],
"license": "MIT",
"repository": {
"directory": "packages/embark-web3",
"type": "git",
"url": "https://github.com/embark-framework/embark.git"
},
"main": "./dist/index.js",
"scripts": {
"build": "cross-env BABEL_ENV=node babel src --extensions \".js\" --out-dir dist --root-mode upward --source-maps",
"ci": "npm run qa",
"clean": "npm run reset",
"lint": "npm-run-all lint:*",
"lint:js": "eslint src/",
"// lint:ts": "tslint -c tslint.json \"src/**/*.ts\"",
"package": "npm pack",
"// qa": "npm-run-all lint typecheck build package",
"qa": "npm-run-all lint build package",
"reset": "npx rimraf dist embark-*.tgz package",
"start": "npm run watch",
"// typecheck": "tsc",
"watch": "run-p watch:*",
"watch:build": "npm run build -- --verbose --watch",
"// watch:typecheck": "npm run typecheck -- --preserveWatchOutput --watch"
},
"eslintConfig": {
"extends": "../../.eslintrc.json"
},
"dependencies": {
"@babel/runtime-corejs2": "7.3.1",
"embark-core": "^4.1.0-beta.3",
"embark-utils": "^4.1.0-beta.3",
"embarkjs-web3": "^4.1.0-beta.3"
},
"devDependencies": {
"@babel/cli": "7.2.3",
"@babel/core": "7.2.2",
"cross-env": "5.2.0",
"eslint": "5.7.0",
"npm-run-all": "4.1.5",
"rimraf": "2.6.3",
"tslint": "5.16.0",
"typescript": "3.4.5"
},
"engines": {
"node": ">=8.12.0 <12.0.0",
"npm": ">=6.4.1",
"yarn": ">=1.12.3"
}
}

View File

@ -0,0 +1,123 @@
/* global __dirname module process require */
const { dappPath, embarkPath, normalizePath, toForwardSlashes } = require('embark-utils');
const constants = require('embark-core/constants');
const path = require('path');
class EmbarkWeb3 {
constructor(embark, _options) {
this.embark = embark;
this.logger = embark.logger;
this.events = embark.events;
this.fs = embark.fs;
this.config = embark.config;
this.modulesPath = dappPath(embark.config.embarkConfig.generationDir, constants.dappArtifacts.symlinkDir);
this.addWeb3ToEmbarkJS();
}
async addWeb3ToEmbarkJS() {
let blockchainConnectorReady = false;
await this.whenRuncodeReady();
const web3LocationPromise = this.getWeb3Location();
this.events.setCommandHandler('blockchain:connector:ready', (cb) => {
if (blockchainConnectorReady) {
return cb();
}
this.events.once("blockchain:connector:ready", () => {
cb();
});
});
web3LocationPromise.then((_web3Location) => {
blockchainConnectorReady = true;
this.events.emit('blockchain:connector:ready');
});
let web3Location = await web3LocationPromise;
web3Location = normalizePath(web3Location, true);
await this.registerVar('__Web3', require(web3Location));
const symlinkLocation = await this.generateSymlink(web3Location);
let code = `\nconst Web3 = global.__Web3 || require('${symlinkLocation}');`;
code += `\nglobal.Web3 = Web3;`;
let linkedModulePath = path.join(this.modulesPath, 'embarkjs-web3');
if (process.platform === 'win32') linkedModulePath = linkedModulePath.replace(/\\/g, '\\\\');
code += `\n
const __embarkWeb3 = require('${linkedModulePath}');
EmbarkJS.Blockchain.registerProvider('web3', __embarkWeb3.default || __embarkWeb3);
EmbarkJS.Blockchain.setProvider('web3', {});
`;
const configPath = toForwardSlashes(dappPath(this.config.embarkConfig.generationDir, constants.dappArtifacts.dir, constants.dappArtifacts.blockchain));
code += `\nif (!global.__Web3) {`; // Only connect when in the Dapp
code += `\n const web3ConnectionConfig = require('${configPath}');`;
code += `\n EmbarkJS.Blockchain.connect(web3ConnectionConfig, (err) => {if (err) { console.error(err); } });`;
code += `\n}`;
this.embark.addCodeToEmbarkJS(code);
code = "EmbarkJS.Blockchain.setProvider('web3', {web3});";
const shouldInit = (_config) => {
return true;
};
this.embark.addConsoleProviderInit('blockchain', code, shouldInit);
}
whenRuncodeReady() {
return new Promise((resolve) => {
this.events.on('runcode:ready', () => {
resolve();
});
});
}
getWeb3Location() {
return new Promise((resolve, reject) => {
this.events.request("version:get:web3", (web3Version) => {
if (web3Version === "1.0.0-beta") {
const nodePath = embarkPath('node_modules');
const web3Path = require.resolve("web3", {paths: [nodePath]});
return resolve(web3Path);
}
this.events.request("version:getPackageLocation", "web3", web3Version, (err, location) => {
if (err) {
return reject(err);
}
const locationPath = embarkPath(location);
resolve(locationPath);
});
});
});
}
generateSymlink(location) {
return new Promise((resolve, reject) => {
this.events.request('code-generator:symlink:generate', location, 'web3', (err, symlinkDest) => {
if (err) {
return reject(err);
}
resolve(symlinkDest);
});
});
}
registerVar(name, code) {
return new Promise((resolve) => {
this.events.emit('runcode:register', name, code, () => {
resolve();
});
});
}
}
module.exports = EmbarkWeb3;

View File

@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"]
}

View File

@ -0,0 +1,3 @@
{
"extends": "../../tslint.json"
}

View File

@ -125,12 +125,14 @@
"embark-utils": "^4.1.0-beta.3",
"embark-vyper": "^4.1.0-beta.3",
"embark-watcher": "^4.1.0-beta.3",
"embark-web3": "^4.1.0-beta.3",
"embark-webserver": "^4.1.0-beta.3",
"embark-whisper": "^4.1.0-beta.3",
"embarkjs": "^4.1.0-beta.3",
"embarkjs-ens": "^4.1.0-beta.3",
"embarkjs-ipfs": "^4.1.0-beta.3",
"embarkjs-swarm": "^4.1.0-beta.3",
"embarkjs-web3": "^4.1.0-beta.3",
"embarkjs-whisper": "^4.1.0-beta.3",
"eth-ens-namehash": "2.0.8",
"ethereumjs-tx": "1.3.7",

View File

@ -128,13 +128,13 @@ class EmbarkController {
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
}
engine.startService("web3");
engine.startService("processManager");
engine.startService("coreProcess");
engine.startService("blockchainListener");
engine.startService("serviceMonitor");
engine.startService("libraryManager");
engine.startService("codeRunner");
engine.startService("web3");
engine.startService("pipeline");
engine.startService("deployment");
engine.startService("storage");
@ -229,13 +229,13 @@ class EmbarkController {
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
}
if (!options.onlyCompile) engine.startService("web3");
engine.startService("processManager");
engine.startService("libraryManager");
engine.startService("codeRunner");
engine.startService("pipeline");
engine.startService("codeGenerator");
if (!options.onlyCompile) {
engine.startService("web3");
engine.startService("deployment", {onlyCompile: options.onlyCompile});
engine.startService("storage");
} else {
@ -514,10 +514,10 @@ class EmbarkController {
if (pluginList.length > 0) {
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
}
engine.startService("web3");
engine.startService("processManager");
engine.startService("libraryManager");
engine.startService("codeRunner");
engine.startService("web3");
engine.startService("deployment", {onlyCompile: true});
callback();
@ -581,11 +581,11 @@ class EmbarkController {
},
function startServices(callback) {
engine.startService("web3");
engine.startService("processManager");
engine.startService("serviceMonitor");
engine.startService("libraryManager");
engine.startService("codeRunner");
engine.startService("web3");
engine.startService("pipeline");
engine.startService("deployment");
engine.startService("storage");
@ -670,10 +670,10 @@ class EmbarkController {
engine.init({}, callback);
},
function startServices(callback) {
engine.startService("web3", {wait: true, node: options.node});
engine.startService("processManager");
engine.startService("libraryManager");
engine.startService("codeRunner");
engine.startService("web3", {wait: true, node: options.node});
engine.startService("deployment", {
trackContracts: false,
compileOnceOnly: true,

View File

@ -312,6 +312,7 @@ class Engine {
wait: options.wait
});
this.registerModulePackage('embark-web3');
this.registerModulePackage('embark-whisper');
}

View File

@ -17,11 +17,20 @@ var Plugins = function(options) {
this.version = options.version;
};
Plugins.deprecated = {
'embarkjs-connector-web3': '4.1.0'
};
Plugins.prototype.loadPlugins = function() {
for (let pluginName in this.pluginList) {
let pluginConfig = this.pluginList[pluginName];
Object.entries(Plugins.deprecated).forEach(([pluginName, embarkVersion]) => {
if (this.pluginList[pluginName]) {
delete this.pluginList[pluginName];
this.logger.warn(`${pluginName} plugin was not loaded because it has been deprecated as of embark v${embarkVersion}, please remove it from this project's embark.json and package.json`);
}
});
Object.entries(this.pluginList).forEach(([pluginName, pluginConfig]) => {
this.loadPlugin(pluginName, pluginConfig);
}
});
};
Plugins.prototype.listPlugins = function() {

View File

@ -0,0 +1,46 @@
/* global module require */
const cloneDeep = require('lodash.clonedeep');
module.exports = (api) => {
const env = api.env();
const base = {};
const browser = cloneDeep(base);
Object.assign(browser, {
ignore: [
'src/embarkjs.js',
'src/node/index.js'
]
});
const node = cloneDeep(base);
Object.assign(node, {
ignore: [
'src/browser.js'
]
});
const nodeAsync = cloneDeep(base);
Object.assign(nodeAsync, {
ignore: [
'src/node/index.js'
]
});
const nodeTest = cloneDeep(base);
switch (env) {
case 'browser':
return browser;
case 'node':
return node;
case 'node:async':
return nodeAsync;
case 'node:test':
return nodeTest;
default:
return base;
}
};

1
packages/embarkjs-web3/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build-test

View File

@ -0,0 +1,4 @@
engine-strict = true
package-lock = false
save-exact = true
scripts-prepend-node-path = true

View File

@ -0,0 +1,6 @@
# `embarkjs-web3`
> embarkjs plugin to interact with web3
Visit [embark.status.im](https://embark.status.im/) to get started with
[Embark](https://github.com/embark-framework/embark).

View File

@ -0,0 +1,74 @@
{
"name": "embarkjs-web3",
"version": "4.1.0-beta.3",
"author": "Iuri Matias <iuri.matias@gmail.com>",
"contributors": [],
"description": "embarkjs plugin to interact with web3",
"homepage": "https://github.com/embark-framework/embark/tree/master/packages/embarkjs-web3#readme",
"bugs": "https://github.com/embark-framework/embark/issues",
"keywords": [
"blockchain",
"dapps",
"ethereum",
"ipfs",
"serverless",
"solc",
"solidity"
],
"license": "MIT",
"repository": {
"directory": "packages/embarkjs-web3",
"type": "git",
"url": "https://github.com/embark-framework/embark.git"
},
"main": "dist/node/index.js",
"browser": {
"./dist/node/index.js": "./dist/browser/index.js"
},
"browserslist": [
"last 1 version",
"not dead",
"> 0.2%"
],
"files": [
"dist"
],
"scripts": {
"build": "npm-run-all build:**",
"build:browser": "cross-env BABEL_ENV=browser babel src --extensions \".js\" --out-dir dist/browser --root-mode upward",
"build:node": "cross-env BABEL_ENV=node babel src --extensions \".js\" --out-dir dist --root-mode upward --source-maps",
"build:node:async": "cross-env BABEL_ENV=node:async babel src/node --extensions \".js\" --out-dir dist --root-mode upward --source-maps",
"// build:node:test": "cross-env BABEL_ENV=node:test babel test --extensions \".js\" --out-dir build-test --root-mode upward --source-maps",
"ci": "npm run qa",
"clean": "npm run reset",
"package": "npm pack",
"// qa": "npm-run-all build test package",
"qa": "npm-run-all build package",
"reset": "npx rimraf .nyc_output build-test coverage dist embarkjs-*.tgz package",
"start": "npm run watch",
"// test": "nyc --reporter=html --reporter=json mocha \"build-test/**/*.js\" --exit --no-timeouts --require source-map-support/register",
"watch": "run-p \"build:** -- --verbose --watch\""
},
"dependencies": {
"@babel/runtime-corejs2": "7.3.1",
"web3": "1.0.0-beta.37"
},
"devDependencies": {
"@babel/cli": "7.2.3",
"@babel/core": "7.2.2",
"ajv": "6.5.2",
"chai": "4.2.0",
"cross-env": "5.2.0",
"lodash.clonedeep": "4.5.0",
"mocha": "5.2.0",
"npm-run-all": "4.1.5",
"nyc": "13.1.0",
"rimraf": "2.6.3",
"source-map-support": "0.5.9"
},
"engines": {
"node": ">=8.12.0 <12.0.0",
"npm": ">=6.4.1",
"yarn": ">=1.12.3"
}
}

View File

@ -0,0 +1 @@
export {default} from './index';

View File

@ -0,0 +1,62 @@
/* global global require */
const Web3 = global.Web3 || require('web3');
const __embarkWeb3 = {};
__embarkWeb3.init = function(config) {
global.web3 = config.web3 || global.web3;
// Check if the global web3 object uses the old web3 (0.x)
if (global.web3 && typeof global.web3.version !== 'string') {
// If so, use a new instance using 1.0, but use its provider
this.web3 = new Web3(global.web3.currentProvider);
} else {
this.web3 = global.web3 || new Web3();
}
global.web3 = this.web3;
};
__embarkWeb3.getInstance = function () {
return this.web3;
};
__embarkWeb3.getAccounts = function () {
return this.web3.eth.getAccounts(...arguments);
};
__embarkWeb3.getNewProvider = function (providerName, ...args) {
return new Web3.providers[providerName](...args);
};
__embarkWeb3.setProvider = function (provider) {
return this.web3.setProvider(provider);
};
__embarkWeb3.getCurrentProvider = function () {
return this.web3.currentProvider;
};
__embarkWeb3.getDefaultAccount = function () {
return this.web3.eth.defaultAccount;
};
__embarkWeb3.setDefaultAccount = function (account) {
this.web3.eth.defaultAccount = account;
};
__embarkWeb3.newContract = function (options) {
return new this.web3.eth.Contract(options.abi, options.address);
};
__embarkWeb3.send = function () {
return this.web3.eth.sendTransaction(...arguments);
};
__embarkWeb3.toWei = function () {
return this.web3.toWei(...arguments);
};
__embarkWeb3.getNetworkId = function () {
return this.web3.eth.net.getId();
};
export default __embarkWeb3;

View File

@ -0,0 +1 @@
module.exports = require('../embarkjs').default;