From 661ce74877e7c724bb0c26351658d4b6559a0725 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Wed, 1 May 2019 16:00:04 -0400 Subject: [PATCH] chore: move console to module --- packages/embark-console/.npmrc | 4 + packages/embark-console/README.md | 6 ++ packages/embark-console/package.json | 85 +++++++++++++++++++ .../src/lib}/index.ts | 37 ++++---- .../src/lib}/suggestions.ts | 8 +- .../src/test/console.js | 39 ++++++--- .../suggestions.json | 0 packages/embark-console/tsconfig.json | 4 + packages/embark-console/tslint.json | 3 + packages/embark-core/constants.json | 3 + packages/embark-utils/package.json | 5 +- packages/embark-utils/src/index.js | 14 +++ packages/embark/package.json | 2 +- packages/embark/src/lib/core/engine.js | 2 +- packages/embark/src/lib/core/env.js | 5 -- packages/embark/src/lib/utils/utils.js | 15 ---- yarn.lock | 7 +- 17 files changed, 177 insertions(+), 62 deletions(-) create mode 100644 packages/embark-console/.npmrc create mode 100644 packages/embark-console/README.md create mode 100644 packages/embark-console/package.json rename packages/{embark/src/lib/modules/console => embark-console/src/lib}/index.ts (87%) rename packages/{embark/src/lib/modules/console => embark-console/src/lib}/suggestions.ts (93%) rename packages/{embark => embark-console}/src/test/console.js (60%) rename packages/{embark/src/lib/modules/console => embark-console}/suggestions.json (100%) create mode 100644 packages/embark-console/tsconfig.json create mode 100644 packages/embark-console/tslint.json diff --git a/packages/embark-console/.npmrc b/packages/embark-console/.npmrc new file mode 100644 index 000000000..e031d3432 --- /dev/null +++ b/packages/embark-console/.npmrc @@ -0,0 +1,4 @@ +engine-strict = true +package-lock = false +save-exact = true +scripts-prepend-node-path = true diff --git a/packages/embark-console/README.md b/packages/embark-console/README.md new file mode 100644 index 000000000..45fcb313f --- /dev/null +++ b/packages/embark-console/README.md @@ -0,0 +1,6 @@ +# `embark-console` + +> Console component 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-console/package.json b/packages/embark-console/package.json new file mode 100644 index 000000000..7482f02ee --- /dev/null +++ b/packages/embark-console/package.json @@ -0,0 +1,85 @@ +{ + "name": "embark-console", + "version": "4.1.0-beta.0", + "author": "Iuri Matias ", + "contributors": [], + "description": "Console component for Embark", + "homepage": "https://github.com/embark-framework/embark/tree/master/packages/embark-console#readme", + "bugs": "https://github.com/embark-framework/embark/issues", + "keywords": [ + "blockchain", + "dapps", + "ethereum", + "ipfs", + "serverless", + "solc", + "solidity" + ], + "files": [ + "dist/lib" + ], + "license": "MIT", + "repository": { + "directory": "packages/embark-console", + "type": "git", + "url": "https://github.com/embark-framework/embark.git" + }, + "main": "./dist/lib/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 test package", + "reset": "npx rimraf dist embark-*.tgz package", + "start": "npm run watch", + "test": "nyc --reporter=html --reporter=json mocha \"dist/test/**/*.js\" --exit --no-timeouts --require source-map-support/register", + "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", + "chalk": "2.4.2", + "embark-core": "^4.1.0-beta.0", + "embark-utils": "^4.1.0-beta.0", + "i18n": "0.8.3", + "json-stringify-safe": "5.0.1" + }, + "devDependencies": { + "@types/embark": "^4.0.0", + "@types/json-stringify-safe": "5.0.0", + "@babel/cli": "7.2.3", + "@babel/core": "7.2.2", + "cross-env": "5.2.0", + "embark-logger": "^4.1.0-beta.0", + "eslint": "5.7.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", + "tslint": "5.16.0", + "typescript": "3.4.5" + }, + "engines": { + "node": ">=8.12.0", + "npm": ">=6.4.1", + "yarn": ">=1.12.3" + }, + "nyc": { + "exclude": [ + "**/node_modules/**", + "coverage/**", + "dist/test/**" + ] + } +} diff --git a/packages/embark/src/lib/modules/console/index.ts b/packages/embark-console/src/lib/index.ts similarity index 87% rename from packages/embark/src/lib/modules/console/index.ts rename to packages/embark-console/src/lib/index.ts index e27e6a899..918f1b9ce 100644 --- a/packages/embark/src/lib/modules/console/index.ts +++ b/packages/embark-console/src/lib/index.ts @@ -1,14 +1,11 @@ -/*globals __*/ -import "colors"; -const env = require("../../core/env"); -const utils = require("../../utils/utils"); -const {escapeHtml, exit} = require("embark-utils"); -import { Callback } from "embark"; -import constants from "embark-core/constants.json"; -const stringify = require("json-stringify-safe"); import { waterfall } from "async"; -import { Embark, Events } from "embark"; -import {__} from "i18n"; +import chalk from "chalk"; +import { Callback, Embark, Events } /* supplied by @types/embark in packages/embark-typings */ from "embark"; +import constants from "embark-core/constants.json"; +import { escapeHtml, exit, jsonFunctionReplacer } from "embark-utils"; +import { __ } from "i18n"; +import stringify from "json-stringify-safe"; +import { dirname } from "path"; import Suggestions from "./suggestions"; type MatchFunction = (cmd: string) => boolean; @@ -91,7 +88,7 @@ class Console { } private cmdHistorySize() { - return env.anchoredValue(env.CMD_HISTORY_SIZE); + return parseInt(process.env.CMD_HISTORY_SIZE || constants.console.commandHistorySize, 10); } private registerApi() { @@ -103,7 +100,7 @@ class Console { } let response = result; if (typeof result !== "string") { - response = stringify(result, utils.jsonFunctionReplacer, 2); + response = stringify(result, jsonFunctionReplacer, 2); } else { // Avoid HTML injection in the Cockpit response = escapeHtml(response); @@ -125,21 +122,21 @@ class Console { __("possible commands are:"), // TODO: only if the blockchain is actually active! // will need to pass te current embark state here - "ipfs".cyan + " - " + __("instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)"), - "swarm".cyan + " - " + __("instantiated swarm-api object configured to the current environment (available if swarm is enabled)"), - "web3".cyan + " - " + __("instantiated web3.js object configured to the current environment"), - "EmbarkJS".cyan + " - " + __("EmbarkJS static functions for Storage, Messages, Names, etc."), - "log [process] on/off".cyan + " - " + __("Activate or deactivate the logs of a sub-process. Options: blockchain, ipfs, webserver"), + chalk.cyan("ipfs") + " - " + __("instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)"), + chalk.cyan("swarm") + " - " + __("instantiated swarm-api object configured to the current environment (available if swarm is enabled)"), + chalk.cyan("web3") + " - " + __("instantiated web3.js object configured to the current environment"), + chalk.cyan("EmbarkJS") + " - " + __("EmbarkJS static functions for Storage, Messages, Names, etc."), + chalk.cyan("log [process] on/off") + " - " + __("Activate or deactivate the logs of a sub-process. Options: blockchain, ipfs, webserver"), ]; helpDescriptions.forEach((helpDescription) => { let matches = [] as string[]; if (Array.isArray(helpDescription.matches)) { matches = helpDescription.matches as string[]; } - helpText.push(`${(helpDescription.usage || matches.join("/")).cyan} - ${helpDescription.description}`); + helpText.push(`${chalk.cyan(helpDescription.usage || matches.join("/"))} - ${helpDescription.description}`); }); // Add end commands - helpText.push("quit".cyan + " - " + __("to immediatly exit (alias: exit)"), + helpText.push(chalk.cyan("quit") + " - " + __("to immediatly exit (alias: exit)"), "", __("The web3 object and the interfaces for the deployed contracts and their methods are also available")); return helpText.join("\n"); @@ -297,7 +294,7 @@ class Console { this.ipc.client.emit("console:history:save", cmd); } - if (this.fs.existsSync(utils.dirname(this.cmdHistoryFile))) { + if (this.fs.existsSync(dirname(this.cmdHistoryFile))) { this.fs.writeFileSync( this.cmdHistoryFile, history diff --git a/packages/embark/src/lib/modules/console/suggestions.ts b/packages/embark-console/src/lib/suggestions.ts similarity index 93% rename from packages/embark/src/lib/modules/console/suggestions.ts rename to packages/embark-console/src/lib/suggestions.ts index 44cef6261..44737feba 100644 --- a/packages/embark/src/lib/modules/console/suggestions.ts +++ b/packages/embark-console/src/lib/suggestions.ts @@ -1,6 +1,6 @@ -const fuzzySearch = require("../../utils/utils").fuzzySearch; - -import { Embark, Events } from "embark"; +import { Embark, Events } /* supplied by @types/embark in packages/embark-typings */ from "embark"; +import { fuzzySearch } from "embark-utils"; +import { suggestions as defaultSuggestions } from "../../suggestions.json"; interface ContractsManager { [key: string]: any; @@ -18,7 +18,7 @@ export default class Suggestions { private embark: Embark; private events: Events; private contracts: ContractsManager; - private static readonly DEFAULT_SUGGESTIONS = require("./suggestions.json").suggestions; + private static readonly DEFAULT_SUGGESTIONS = defaultSuggestions; private _suggestions: SuggestionsList = []; constructor(embark: Embark, options?: object) { diff --git a/packages/embark/src/test/console.js b/packages/embark-console/src/test/console.js similarity index 60% rename from packages/embark/src/test/console.js rename to packages/embark-console/src/test/console.js index fba20b32d..43cab290f 100644 --- a/packages/embark/src/test/console.js +++ b/packages/embark-console/src/test/console.js @@ -1,25 +1,38 @@ /*globals describe, it*/ -let Console = require('../lib/modules/console/'); -let Plugins = require('../lib/core/plugins.js'); -let IPC = require('../lib/core/ipc.js'); -let Logger = require('embark-logger'); -let assert = require('assert'); -let version = require('../../package.json').version; +import Console from '../lib'; +import Logger from 'embark-logger'; +import assert from 'assert'; +import { version } from '../../package.json'; +import * as i18n from "i18n"; + +i18n.configure({ + directory: undefined, + locales: [] +}); describe('embark.Console', function() { - let ipc = new IPC({ipcRole: 'none'}); - let events = {once: () => {}, setCommandHandler: () => {}, emit: () => {}, on: () => {}, request: () => {}}; let logger = new Logger({logLevel: 'error'}); - let plugins = new Plugins({plugins: {}, events, logger}); + let ipc = { + isServer: () => { return true; }, + broadcast: () => {}, + on: () => {}, + isClient: () => { return false; } + }; + let events = {once: () => {}, setCommandHandler: () => {}, emit: () => {}, on: () => {}, request: () => {}}; + let plugins = { + logger: logger, + createPlugin: () => { return {registerAPICall: () => {}}; }, + getPluginsProperty: () => { return []; } + }; let embarkObject = { registerAPICall: () => {}, events: events, logger: plugins.logger, fs: { - existsSync: () => { return false }, - dappPath: () => { return "ok" } + existsSync: () => { return false; }, + dappPath: () => { return "ok"; } }, - registerConsoleCommand: (cmd, opt) => {}, + registerConsoleCommand: (_cmd, _opt) => {}, embarkConfig: { options: { solc: { @@ -28,7 +41,7 @@ describe('embark.Console', function() { } } } - } + }; let console = new Console(embarkObject, {plugins, version, ipc, events, logger}); describe('#executeCmd', function() { diff --git a/packages/embark/src/lib/modules/console/suggestions.json b/packages/embark-console/suggestions.json similarity index 100% rename from packages/embark/src/lib/modules/console/suggestions.json rename to packages/embark-console/suggestions.json diff --git a/packages/embark-console/tsconfig.json b/packages/embark-console/tsconfig.json new file mode 100644 index 000000000..52d43eaaa --- /dev/null +++ b/packages/embark-console/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src/**/*"] +} diff --git a/packages/embark-console/tslint.json b/packages/embark-console/tslint.json new file mode 100644 index 000000000..0946f2096 --- /dev/null +++ b/packages/embark-console/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tslint.json" +} diff --git a/packages/embark-core/constants.json b/packages/embark-core/constants.json index c00927834..c054f39c8 100644 --- a/packages/embark-core/constants.json +++ b/packages/embark-core/constants.json @@ -1,5 +1,8 @@ { "httpContractsDirectory": ".embark/contracts/", + "console": { + "commandHistorySize": "20" + }, "contexts": { "simulator": "simulator", "blockchain": "blockchain", diff --git a/packages/embark-utils/package.json b/packages/embark-utils/package.json index 46ea48bda..2b310f8ea 100644 --- a/packages/embark-utils/package.json +++ b/packages/embark-utils/package.json @@ -51,14 +51,15 @@ "colors": "1.3.2", "ethereumjs-wallet": "0.6.0", "follow-redirects": "1.5.7", + "fuzzy": "0.1.3", "i18n": "0.8.3", "merge": "1.2.1", "multihashes": "0.4.14", "ora": "2.1.0", + "shelljs": "0.5.3", "web3": "1.0.0-beta.37", "web3-eth": "1.0.0-beta.37", - "web3-eth-abi": "1.0.0-beta.37", - "shelljs": "0.5.3" + "web3-eth-abi": "1.0.0-beta.37" }, "devDependencies": { "@babel/cli": "7.2.3", diff --git a/packages/embark-utils/src/index.js b/packages/embark-utils/src/index.js index 1e4aca061..d6f575ac7 100644 --- a/packages/embark-utils/src/index.js +++ b/packages/embark-utils/src/index.js @@ -165,12 +165,26 @@ function prepareContractsConfig(config) { return config; } +function jsonFunctionReplacer(_key, value) { + if (typeof value === 'function') { + return value.toString(); + } + + return value; +} + +function fuzzySearch(text, list, filter) { + const fuzzy = require('fuzzy'); + return fuzzy.filter(text, list, {extract: (filter || function () {})}); +} const Utils = { joinPath: function() { const path = require('path'); return path.join.apply(path.join, arguments); }, + jsonFunctionReplacer, + fuzzySearch, canonicalHost, copyToClipboard, deconstructUrl, diff --git a/packages/embark/package.json b/packages/embark/package.json index 497962f8a..f3613fc1a 100644 --- a/packages/embark/package.json +++ b/packages/embark/package.json @@ -94,6 +94,7 @@ "embark-authenticator": "^4.1.0-beta.0", "embark-api": "^4.1.0-beta.0", "embark-compiler": "^4.0.0", + "embark-console": "^4.1.0-beta.0", "embark-contracts-manager": "^4.1.0-beta.0", "embark-core": "^4.1.0-beta.0", "embark-ens": "^4.1.0-beta.0", @@ -125,7 +126,6 @@ "flatted": "0.2.3", "follow-redirects": "1.5.7", "fs-extra": "7.0.1", - "fuzzy": "0.1.3", "ganache-cli": "6.2.3", "glob": "7.1.3", "globule": "1.2.1", diff --git a/packages/embark/src/lib/core/engine.js b/packages/embark/src/lib/core/engine.js index afb67a423..2a6e9ea75 100644 --- a/packages/embark/src/lib/core/engine.js +++ b/packages/embark/src/lib/core/engine.js @@ -170,7 +170,7 @@ class Engine { } console(_options) { - this.registerModule('console', { + this.registerModulePackage('embark-console', { events: this.events, plugins: this.plugins, version: this.version, diff --git a/packages/embark/src/lib/core/env.js b/packages/embark/src/lib/core/env.js index 54a126363..da8639ef8 100644 --- a/packages/embark/src/lib/core/env.js +++ b/packages/embark/src/lib/core/env.js @@ -34,10 +34,6 @@ const DAPP_PATH = 'DAPP_PATH'; const DEFAULT_DAPP_PATH = anchoredValue(PWD); anchoredValue(DAPP_PATH, DEFAULT_DAPP_PATH); -const CMD_HISTORY_SIZE = 'CMD_HISTORY_SIZE'; -const DEFAULT_CMD_HISTORY_SIZE = 20; -anchoredValue(CMD_HISTORY_SIZE, DEFAULT_CMD_HISTORY_SIZE); - const DIAGRAM_PATH = 'DIAGRAM_PATH'; const DEFAULT_DIAGRAM_PATH = joinPath(anchoredValue(DAPP_PATH), 'diagram.svg'); anchoredValue(DIAGRAM_PATH, DEFAULT_DIAGRAM_PATH); @@ -74,7 +70,6 @@ module.exports = { anchoredValue, PWD, DAPP_PATH, - CMD_HISTORY_SIZE, DIAGRAM_PATH, EMBARK_PATH, PKG_PATH diff --git a/packages/embark/src/lib/utils/utils.js b/packages/embark/src/lib/utils/utils.js index 1846472f4..46d8b2141 100644 --- a/packages/embark/src/lib/utils/utils.js +++ b/packages/embark/src/lib/utils/utils.js @@ -394,19 +394,6 @@ function fileTreeSort(nodes){ return folders.concat(files); } -function fuzzySearch(text, list, filter) { - const fuzzy = require('fuzzy'); - return fuzzy.filter(text, list, {extract: (filter || function () {})}); -} - -function jsonFunctionReplacer(_key, value) { - if (typeof value === 'function') { - return value.toString(); - } - - return value; -} - function getWindowSize() { const windowSize = require('window-size'); if (windowSize) { @@ -467,8 +454,6 @@ module.exports = { interceptLogs, errorMessage, fileTreeSort, - fuzzySearch, - jsonFunctionReplacer, getWindowSize, isEs6Module, urlJoin diff --git a/yarn.lock b/yarn.lock index ef5cad16f..36bea2fd0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2803,6 +2803,11 @@ resolved "https://registry.yarnpkg.com/@types/i18n/-/i18n-0.8.3.tgz#f602164f2fae486ea87590f6be5d6dd5db1664e6" integrity sha512-JyNZyqamS3jfJ/qTXkAIAtBKlapN5OrEVosdde6LT41Gf209NzXppDVoNkn1pABGZbSnT8dgEgiuTFBIgxweIQ== +"@types/json-stringify-safe@5.0.0": + version "5.0.0" + resolved "https://registry.yarnpkg.com/@types/json-stringify-safe/-/json-stringify-safe-5.0.0.tgz#df34d054419d39323a3730966bacba02ac5e474e" + integrity sha512-UUA1sH0RSRROdInuDOA1yoRzbi5xVFD1RHCoOvNRPTNwR8zBkJ/84PZ6NhKVDtKp0FTeIccJCdQz1X2aJPr4uw== + "@types/lockfile@^1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@types/lockfile/-/lockfile-1.0.0.tgz#76a7c19c50fe8ee2b1666d653ff5d557c30fe0ff" @@ -11897,7 +11902,7 @@ json-stable-stringify@^1.0.1: dependencies: jsonify "~0.0.0" -json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1: +json-stringify-safe@5.0.1, json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=