From 65f3a270c021d0ff658c5c886bc1db8e5ce6bef2 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 29 Apr 2019 16:49:28 -0500 Subject: [PATCH] refactor(@embark/utils): move proposeAlternative, toposort into embark-utils package --- packages/embark-utils/src/index.js | 17 ++++++++++++++++- .../lib/utils => embark-utils/src}/toposort.js | 0 packages/embark/src/cmd/cmd.js | 6 +++--- .../src/lib/modules/contracts_manager/index.js | 10 +++++----- packages/embark/src/lib/utils/utils.js | 16 ---------------- 5 files changed, 24 insertions(+), 25 deletions(-) rename packages/{embark/src/lib/utils => embark-utils/src}/toposort.js (100%) diff --git a/packages/embark-utils/src/index.js b/packages/embark-utils/src/index.js index b941a2337..7d2bf8fea 100644 --- a/packages/embark-utils/src/index.js +++ b/packages/embark-utils/src/index.js @@ -6,6 +6,7 @@ const clipboardy = require('clipboardy'); const {canonicalHost, defaultCorsHost, defaultHost, dockerHostSwap, isDocker} = require('./host'); const {findNextPort} = require('./network'); const logUtils = require('./log-utils'); +const toposortGraph = require('./toposort'); function checkIsAvailable(url, callback) { const protocol = url.split(':')[0]; @@ -95,6 +96,18 @@ function copyToClipboard(text) { clipboardy.writeSync(text); } +function proposeAlternative(word, _dictionary, _exceptions) { + const propose = require('propose'); + let exceptions = _exceptions || []; + let dictionary = _dictionary.filter((entry) => { + return exceptions.indexOf(entry) < 0; + }); + return propose(word, dictionary, {threshold: 0.3}); +} + +function toposort(graph) { + return toposortGraph(graph); +} const Utils = { joinPath: function() { @@ -119,7 +132,9 @@ const Utils = { escapeHtml: logUtils.escapeHtml, normalizeInput: logUtils.normalizeInput, LogHandler: require('./logHandler'), - AddressUtils: require('./addressUtils') + AddressUtils: require('./addressUtils'), + proposeAlternative, + toposort }; module.exports = Utils; diff --git a/packages/embark/src/lib/utils/toposort.js b/packages/embark-utils/src/toposort.js similarity index 100% rename from packages/embark/src/lib/utils/toposort.js rename to packages/embark-utils/src/toposort.js diff --git a/packages/embark/src/cmd/cmd.js b/packages/embark/src/cmd/cmd.js index 863b4a901..61b267770 100644 --- a/packages/embark/src/cmd/cmd.js +++ b/packages/embark/src/cmd/cmd.js @@ -406,13 +406,13 @@ class Cmd { let suggestion; if (cmd === 'compile') { - // we bypass `utils.proposeAlternative()` here as `build` isn't + // we bypass `proposeAlternative()` here as `build` isn't // similar enough suggestion = 'build --contracts'; } else { - let utils = require('../lib/utils/utils.js'); + const {proposeAlternative} = require('embark-utils'); let dictionary = ['new', 'demo', 'build', 'run', 'blockchain', 'simulator', 'test', 'upload', 'version', 'console', 'eject-webpack', 'graph', 'help', 'reset']; - suggestion = utils.proposeAlternative(cmd, dictionary); + suggestion = proposeAlternative(cmd, dictionary); } if (suggestion) { console.log((__('did you mean') + ' "%s"?').green, suggestion); diff --git a/packages/embark/src/lib/modules/contracts_manager/index.js b/packages/embark/src/lib/modules/contracts_manager/index.js index d3f2254e8..ceaf2197e 100644 --- a/packages/embark/src/lib/modules/contracts_manager/index.js +++ b/packages/embark/src/lib/modules/contracts_manager/index.js @@ -1,8 +1,8 @@ let async = require('async'); const cloneDeep = require('clone-deep'); -const path = require('path'); -const utils = require('../../utils/utils.js'); const constants = require('embark-core/constants'); +const path = require('path'); +const {proposeAlternative, toposort} = require('embark-utils'); // TODO: create a contract object @@ -423,7 +423,7 @@ class ContractsManager { className: className, parentContractName: parentContractName })); - let suggestion = utils.proposeAlternative(parentContractName, dictionary, [className, parentContractName]); + let suggestion = proposeAlternative(parentContractName, dictionary, [className, parentContractName]); if (suggestion) { self.logger.warn(__('did you mean "%s"?', suggestion)); } @@ -467,7 +467,7 @@ class ContractsManager { if (contract.code === undefined && !contract.abiDefinition) { self.logger.error(__("%s has no code associated", className)); - let suggestion = utils.proposeAlternative(className, dictionary, [className]); + let suggestion = proposeAlternative(className, dictionary, [className]); if (suggestion) { self.logger.warn(__('did you mean "%s"?', suggestion)); } @@ -614,7 +614,7 @@ class ContractsManager { let orderedDependencies; try { - orderedDependencies = utils.toposort(converted_dependencies.filter((x) => x[0] !== x[1])).reverse(); + orderedDependencies = toposort(converted_dependencies.filter((x) => x[0] !== x[1])).reverse(); } catch (e) { this.logger.error((__("Error: ") + e.message).red); this.logger.error(__("there are two or more contracts that depend on each other in a cyclic manner").bold.red); diff --git a/packages/embark/src/lib/utils/utils.js b/packages/embark/src/lib/utils/utils.js index 6885bd107..39b37712f 100644 --- a/packages/embark/src/lib/utils/utils.js +++ b/packages/embark/src/lib/utils/utils.js @@ -1,6 +1,5 @@ let http = require('follow-redirects').http; let https = require('follow-redirects').https; -let toposortGraph = require('./toposort.js'); import {canonicalHost, normalizeInput} from 'embark-utils'; const balanceRegex = /([0-9]+) ?([a-zA-Z]*)/; @@ -178,15 +177,6 @@ function extractZip(filename, packageDirectory, opts, cb) { }); } -function proposeAlternative(word, _dictionary, _exceptions) { - const propose = require('propose'); - let exceptions = _exceptions || []; - let dictionary = _dictionary.filter((entry) => { - return exceptions.indexOf(entry) < 0; - }); - return propose(word, dictionary, {threshold: 0.3}); -} - function getExternalContractUrl(file,providerUrl) { const constants = require('embark-core/constants'); let url; @@ -498,10 +488,6 @@ function getWindowSize() { return {width: 240, height: 75}; } -function toposort(graph) { - return toposortGraph(graph); -} - function isConstructor(obj) { return !!obj.prototype && !!obj.prototype.constructor.name; } @@ -544,7 +530,6 @@ module.exports = { downloadFile, extractTar, extractZip, - proposeAlternative, getExternalContractUrl, toChecksumAddress, sha3, @@ -564,7 +549,6 @@ module.exports = { fuzzySearch, jsonFunctionReplacer, getWindowSize, - toposort, isEs6Module, urlJoin };