From 3aca2fe17b1b88d77364c8a04f0551305cb3237a Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Tue, 21 May 2019 15:30:33 +0200 Subject: [PATCH] refactor(@embark/code-runner): move code-runner into its own package --- packages/embark-code-runner/.npmrc | 4 + packages/embark-code-runner/README.md | 6 ++ packages/embark-code-runner/package.json | 74 +++++++++++++++++++ .../src}/fs.js | 0 .../src}/index.ts | 10 ++- .../src}/vm.ts | 7 +- packages/embark-code-runner/tsconfig.json | 4 + packages/embark-code-runner/tslint.json | 3 + packages/embark-utils/index.d.ts | 1 + packages/embark-utils/src/index.js | 8 ++ packages/embark/package.json | 1 + packages/embark/src/lib/core/engine.js | 2 +- packages/embark/src/lib/core/plugin.js | 6 +- packages/embark/src/lib/utils/utils.js | 11 +-- packages/embark/src/test/vm.js | 2 +- packages/embark/src/test/vm_fs.js | 3 +- 16 files changed, 117 insertions(+), 25 deletions(-) create mode 100644 packages/embark-code-runner/.npmrc create mode 100644 packages/embark-code-runner/README.md create mode 100644 packages/embark-code-runner/package.json rename packages/{embark/src/lib/modules/codeRunner => embark-code-runner/src}/fs.js (100%) rename packages/{embark/src/lib/modules/codeRunner => embark-code-runner/src}/index.ts (98%) rename packages/{embark/src/lib/modules/codeRunner => embark-code-runner/src}/vm.ts (97%) create mode 100644 packages/embark-code-runner/tsconfig.json create mode 100644 packages/embark-code-runner/tslint.json diff --git a/packages/embark-code-runner/.npmrc b/packages/embark-code-runner/.npmrc new file mode 100644 index 000000000..e031d3432 --- /dev/null +++ b/packages/embark-code-runner/.npmrc @@ -0,0 +1,4 @@ +engine-strict = true +package-lock = false +save-exact = true +scripts-prepend-node-path = true diff --git a/packages/embark-code-runner/README.md b/packages/embark-code-runner/README.md new file mode 100644 index 000000000..2ca57eddd --- /dev/null +++ b/packages/embark-code-runner/README.md @@ -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). diff --git a/packages/embark-code-runner/package.json b/packages/embark-code-runner/package.json new file mode 100644 index 000000000..f23fd0ba6 --- /dev/null +++ b/packages/embark-code-runner/package.json @@ -0,0 +1,74 @@ +{ + "name": "embark-code-runner", + "version": "4.1.0-beta.1", + "author": "Iuri Matias ", + "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" + } +} diff --git a/packages/embark/src/lib/modules/codeRunner/fs.js b/packages/embark-code-runner/src/fs.js similarity index 100% rename from packages/embark/src/lib/modules/codeRunner/fs.js rename to packages/embark-code-runner/src/fs.js diff --git a/packages/embark/src/lib/modules/codeRunner/index.ts b/packages/embark-code-runner/src/index.ts similarity index 98% rename from packages/embark/src/lib/modules/codeRunner/index.ts rename to packages/embark-code-runner/src/index.ts index a47fbd19a..d5fdc1d7b 100644 --- a/packages/embark/src/lib/modules/codeRunner/index.ts +++ b/packages/embark-code-runner/src/index.ts @@ -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) { }); } } - -module.exports = CodeRunner; diff --git a/packages/embark/src/lib/modules/codeRunner/vm.ts b/packages/embark-code-runner/src/vm.ts similarity index 97% rename from packages/embark/src/lib/modules/codeRunner/vm.ts rename to packages/embark-code-runner/src/vm.ts index 36465e034..af1d4c0ca 100644 --- a/packages/embark/src/lib/modules/codeRunner/vm.ts +++ b/packages/embark-code-runner/src/vm.ts @@ -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 { diff --git a/packages/embark-code-runner/tsconfig.json b/packages/embark-code-runner/tsconfig.json new file mode 100644 index 000000000..52d43eaaa --- /dev/null +++ b/packages/embark-code-runner/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src/**/*"] +} diff --git a/packages/embark-code-runner/tslint.json b/packages/embark-code-runner/tslint.json new file mode 100644 index 000000000..0946f2096 --- /dev/null +++ b/packages/embark-code-runner/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tslint.json" +} diff --git a/packages/embark-utils/index.d.ts b/packages/embark-utils/index.d.ts index a7f081c42..6a1d36759 100644 --- a/packages/embark-utils/index.d.ts +++ b/packages/embark-utils/index.d.ts @@ -16,6 +16,7 @@ declare module "embark-utils" { function embarkPath(...names: string[]): string; function exit(code?: any): void; function findNextPort(port: number): Promise; + 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; diff --git a/packages/embark-utils/src/index.js b/packages/embark-utils/src/index.js index ce584d2ed..2b4ead20b 100644 --- a/packages/embark-utils/src/index.js +++ b/packages/embark-utils/src/index.js @@ -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, diff --git a/packages/embark/package.json b/packages/embark/package.json index da14d1a9e..baadc799d 100644 --- a/packages/embark/package.json +++ b/packages/embark/package.json @@ -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", diff --git a/packages/embark/src/lib/core/engine.js b/packages/embark/src/lib/core/engine.js index 29cc4eecd..026a5cd1d 100644 --- a/packages/embark/src/lib/core/engine.js +++ b/packages/embark/src/lib/core/engine.js @@ -181,7 +181,7 @@ class Engine { } codeRunnerService(_options) { - this.registerModule('codeRunner', { + this.registerModulePackage('embark-code-runner', { ipc: this.ipc }); } diff --git a/packages/embark/src/lib/core/plugin.js b/packages/embark/src/lib/core/plugin.js index 2579fde83..56012dd8d 100644 --- a/packages/embark/src/lib/core/plugin.js +++ b/packages/embark/src/lib/core/plugin.js @@ -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; } diff --git a/packages/embark/src/lib/utils/utils.js b/packages/embark/src/lib/utils/utils.js index 3c0386463..440774915 100644 --- a/packages/embark/src/lib/utils/utils.js +++ b/packages/embark/src/lib/utils/utils.js @@ -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 }; diff --git a/packages/embark/src/test/vm.js b/packages/embark/src/test/vm.js index f2dc37da4..4a59813d4 100644 --- a/packages/embark/src/test/vm.js +++ b/packages/embark/src/test/vm.js @@ -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 () { diff --git a/packages/embark/src/test/vm_fs.js b/packages/embark/src/test/vm_fs.js index 05aad3780..db40e46f6 100644 --- a/packages/embark/src/test/vm_fs.js +++ b/packages/embark/src/test/vm_fs.js @@ -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',