refactor(@embark/code-runner): move code-runner into its own package

This commit is contained in:
Pascal Precht 2019-05-21 15:30:33 +02:00 committed by Pascal Precht
parent 038928f8a5
commit 3aca2fe17b
16 changed files with 117 additions and 25 deletions

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 @@
# `embark-code-runner`
> Code execution VM for 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,74 @@
{
"name": "embark-code-runner",
"version": "4.1.0-beta.1",
"author": "Iuri Matias <iuri.matias@gmail.com>",
"contributors": [],
"description": "Code execution VM for Embark",
"homepage": "https://github.com/embark-framework/embark/tree/master/packages/embark-code-runner#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-code-runner",
"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,.ts\" --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",
"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",
"async": "2.6.1",
"colors": "1.3.2",
"embark-utils": "^4.1.0-beta.1",
"embarkjs": "^4.1.0-beta.1",
"fs-extra": "7.0.1",
"parse-json": "4.0.0",
"vm2": "3.6.4",
"web3": "1.0.0-beta.37"
},
"devDependencies": {
"@babel/cli": "7.2.3",
"@babel/core": "7.2.2",
"@types/async": "2.0.50",
"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",
"npm": ">=6.4.1",
"yarn": ">=1.12.3"
}
}

View File

@ -1,7 +1,11 @@
import * as fs from "./fs";
import VM from "./vm";
const fs = require("./fs");
export { fs, VM };
import { Callback, Embark, Events, Logger } /* supplied by @types/embark in packages/embark-typings */ from "embark";
import Web3 from "web3";
const EmbarkJS = require("embarkjs");
export enum ProviderEventType {
@ -9,7 +13,7 @@ export enum ProviderEventType {
ProviderSet = "providerSet",
}
class CodeRunner {
export default class CodeRunner {
private ready: boolean = false;
private blockchainConnected: boolean = false;
private logger: Logger;
@ -158,5 +162,3 @@ private resetEmbarkJS(cb: Callback<null>) {
});
}
}
module.exports = CodeRunner;

View File

@ -1,12 +1,9 @@
import { each } from "async";
import { Callback, Logger } /* supplied by @types/embark in packages/embark-typings */ from "embark";
import { compact, dappPath, isEs6Module, recursiveMerge } from "embark-utils";
import * as path from "path";
import { NodeVM, NodeVMOptions } from "vm2";
import { compact, dappPath, recursiveMerge } from "embark-utils";
const path = require("path");
const { isEs6Module } = require("../../utils/utils");
const WEB3_INVALID_RESPONSE_ERROR: string = "Invalid JSON RPC response";
interface Command {

View File

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

View File

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

View File

@ -16,6 +16,7 @@ declare module "embark-utils" {
function embarkPath(...names: string[]): string;
function exit(code?: any): void;
function findNextPort(port: number): Promise<number>;
function isEs6Module(module: any): boolean;
function jsonFunctionReplacer(key: any, value: any): any;
function fuzzySearch(text: string, list: any, filter: any): any;
function getExternalContractUrl(file: string, provideUrl: string): string;

View File

@ -259,6 +259,13 @@ function errorMessage(e) {
return e;
}
function isConstructor(obj) {
return !!obj.prototype && !!obj.prototype.constructor.name;
}
function isEs6Module(module) {
return (typeof module === 'function' && isConstructor(module)) || (typeof module === 'object' && typeof module.default === 'function' && module.__esModule);
}
const Utils = {
anchoredValue,
@ -286,6 +293,7 @@ const Utils = {
getAddressToContract,
getTransactionParams,
isDocker,
isEs6Module,
checkIsAvailable,
File,
findNextPort,

View File

@ -97,6 +97,7 @@
"embark-blockchain-listener": "^4.1.0-beta.1",
"embark-blockchain-process": "^4.1.0-beta.1",
"embark-code-generator": "^4.1.0-beta.1",
"embark-code-runner": "^4.1.0-beta.1",
"embark-compiler": "^4.1.0-beta.1",
"embark-console": "^4.1.0-beta.1",
"embark-console-listener": "^4.1.0-beta.1",

View File

@ -181,7 +181,7 @@ class Engine {
}
codeRunnerService(_options) {
this.registerModule('codeRunner', {
this.registerModulePackage('embark-code-runner', {
ipc: this.ipc
});
}

View File

@ -1,6 +1,6 @@
const utils = require('../utils/utils.js');
import { __ } from 'embark-i18n';
import { dappPath, embarkPath, joinPath } from 'embark-utils';
import { dappPath, embarkPath, isEs6Module, joinPath } from 'embark-utils';
const constants = require('embark-core/constants');
const fs = require('fs-extra');
const deepEqual = require('deep-equal');
@ -98,7 +98,7 @@ Plugin.prototype.loadPlugin = function() {
if (this.shouldInterceptLogs) {
this.setUpLogger();
}
if (utils.isEs6Module(this.pluginModule)) {
if (isEs6Module(this.pluginModule)) {
if (this.pluginModule.default) {
this.pluginModule = this.pluginModule.default;
}
@ -108,7 +108,7 @@ Plugin.prototype.loadPlugin = function() {
};
Plugin.prototype.loadInternalPlugin = function() {
if (utils.isEs6Module(this.pluginModule)) {
if (isEs6Module(this.pluginModule)) {
if (this.pluginModule.default) {
this.pluginModule = this.pluginModule.default;
}

View File

@ -154,14 +154,6 @@ function getWindowSize() {
return {width: 240, height: 75};
}
function isConstructor(obj) {
return !!obj.prototype && !!obj.prototype.constructor.name;
}
function isEs6Module(module) {
return (typeof module === 'function' && isConstructor(module)) || (typeof module === 'object' && typeof module.default === 'function' && module.__esModule);
}
module.exports = {
dirname,
filesMatchingPattern,
@ -178,6 +170,5 @@ module.exports = {
extractZip,
normalizeInput,
interceptLogs,
getWindowSize,
isEs6Module
getWindowSize
};

View File

@ -1,6 +1,6 @@
/*globals describe, it*/
const TestLogger = require('../lib/utils/test_logger');
const VM = require('../lib/modules/codeRunner/vm').default;
import { VM } from 'embark-code-runner';
const {expect} = require('chai');
describe('embark.vm', function () {

View File

@ -1,10 +1,10 @@
/*global after, before, describe, it, require, process*/
import { fs } from 'embark-code-runner';
const { embarkPath } = require('embark-utils');
const {assert} = require('chai');
const os = require('os');
const path = require('path');
const underlyingFs = require('fs-extra');
const fs = require('../lib/modules/codeRunner/fs');
describe('fs', () => {
let fsMethods = {};
@ -36,6 +36,7 @@ describe('fs', () => {
const helperFunctions = [
'dappPath',
'default', // not a helper function but a property on `fs` due to how it's exported
'diagramPath',
'embarkPath',
'ipcPath',