refactor(@embark/utils): move proposeAlternative, toposort into embark-utils package

This commit is contained in:
Michael Bradley, Jr 2019-04-29 16:49:28 -05:00 committed by Michael Bradley
parent 60ff097406
commit 65f3a270c0
5 changed files with 24 additions and 25 deletions

View File

@ -6,6 +6,7 @@ const clipboardy = require('clipboardy');
const {canonicalHost, defaultCorsHost, defaultHost, dockerHostSwap, isDocker} = require('./host'); const {canonicalHost, defaultCorsHost, defaultHost, dockerHostSwap, isDocker} = require('./host');
const {findNextPort} = require('./network'); const {findNextPort} = require('./network');
const logUtils = require('./log-utils'); const logUtils = require('./log-utils');
const toposortGraph = require('./toposort');
function checkIsAvailable(url, callback) { function checkIsAvailable(url, callback) {
const protocol = url.split(':')[0]; const protocol = url.split(':')[0];
@ -95,6 +96,18 @@ function copyToClipboard(text) {
clipboardy.writeSync(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 = { const Utils = {
joinPath: function() { joinPath: function() {
@ -119,7 +132,9 @@ const Utils = {
escapeHtml: logUtils.escapeHtml, escapeHtml: logUtils.escapeHtml,
normalizeInput: logUtils.normalizeInput, normalizeInput: logUtils.normalizeInput,
LogHandler: require('./logHandler'), LogHandler: require('./logHandler'),
AddressUtils: require('./addressUtils') AddressUtils: require('./addressUtils'),
proposeAlternative,
toposort
}; };
module.exports = Utils; module.exports = Utils;

View File

@ -406,13 +406,13 @@ class Cmd {
let suggestion; let suggestion;
if (cmd === 'compile') { if (cmd === 'compile') {
// we bypass `utils.proposeAlternative()` here as `build` isn't // we bypass `proposeAlternative()` here as `build` isn't
// similar enough // similar enough
suggestion = 'build --contracts'; suggestion = 'build --contracts';
} else { } 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']; 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) { if (suggestion) {
console.log((__('did you mean') + ' "%s"?').green, suggestion); console.log((__('did you mean') + ' "%s"?').green, suggestion);

View File

@ -1,8 +1,8 @@
let async = require('async'); let async = require('async');
const cloneDeep = require('clone-deep'); const cloneDeep = require('clone-deep');
const path = require('path');
const utils = require('../../utils/utils.js');
const constants = require('embark-core/constants'); const constants = require('embark-core/constants');
const path = require('path');
const {proposeAlternative, toposort} = require('embark-utils');
// TODO: create a contract object // TODO: create a contract object
@ -423,7 +423,7 @@ class ContractsManager {
className: className, className: className,
parentContractName: parentContractName parentContractName: parentContractName
})); }));
let suggestion = utils.proposeAlternative(parentContractName, dictionary, [className, parentContractName]); let suggestion = proposeAlternative(parentContractName, dictionary, [className, parentContractName]);
if (suggestion) { if (suggestion) {
self.logger.warn(__('did you mean "%s"?', suggestion)); self.logger.warn(__('did you mean "%s"?', suggestion));
} }
@ -467,7 +467,7 @@ class ContractsManager {
if (contract.code === undefined && !contract.abiDefinition) { if (contract.code === undefined && !contract.abiDefinition) {
self.logger.error(__("%s has no code associated", className)); self.logger.error(__("%s has no code associated", className));
let suggestion = utils.proposeAlternative(className, dictionary, [className]); let suggestion = proposeAlternative(className, dictionary, [className]);
if (suggestion) { if (suggestion) {
self.logger.warn(__('did you mean "%s"?', suggestion)); self.logger.warn(__('did you mean "%s"?', suggestion));
} }
@ -614,7 +614,7 @@ class ContractsManager {
let orderedDependencies; let orderedDependencies;
try { 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) { } catch (e) {
this.logger.error((__("Error: ") + e.message).red); 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); this.logger.error(__("there are two or more contracts that depend on each other in a cyclic manner").bold.red);

View File

@ -1,6 +1,5 @@
let http = require('follow-redirects').http; let http = require('follow-redirects').http;
let https = require('follow-redirects').https; let https = require('follow-redirects').https;
let toposortGraph = require('./toposort.js');
import {canonicalHost, normalizeInput} from 'embark-utils'; import {canonicalHost, normalizeInput} from 'embark-utils';
const balanceRegex = /([0-9]+) ?([a-zA-Z]*)/; 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) { function getExternalContractUrl(file,providerUrl) {
const constants = require('embark-core/constants'); const constants = require('embark-core/constants');
let url; let url;
@ -498,10 +488,6 @@ function getWindowSize() {
return {width: 240, height: 75}; return {width: 240, height: 75};
} }
function toposort(graph) {
return toposortGraph(graph);
}
function isConstructor(obj) { function isConstructor(obj) {
return !!obj.prototype && !!obj.prototype.constructor.name; return !!obj.prototype && !!obj.prototype.constructor.name;
} }
@ -544,7 +530,6 @@ module.exports = {
downloadFile, downloadFile,
extractTar, extractTar,
extractZip, extractZip,
proposeAlternative,
getExternalContractUrl, getExternalContractUrl,
toChecksumAddress, toChecksumAddress,
sha3, sha3,
@ -564,7 +549,6 @@ module.exports = {
fuzzySearch, fuzzySearch,
jsonFunctionReplacer, jsonFunctionReplacer,
getWindowSize, getWindowSize,
toposort,
isEs6Module, isEs6Module,
urlJoin urlJoin
}; };