Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2043 from trufflesuite/truffle-core-housekeeping
Browse files Browse the repository at this point in the history
Internal improvement: Update truffle-core and remove some unneeded code
  • Loading branch information
eggplantzzz committed Jun 10, 2019
2 parents 4efb890 + 8e204e5 commit 1a88bf9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 115 deletions.
5 changes: 1 addition & 4 deletions packages/truffle-core/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ const Command = require("./lib/command");

const command = new Command(require("./lib/commands"));

// Hack to suppress web3 MaxListenersExceededWarning
// This should be removed when issue is resolved upstream:
// https://github.com/ethereum/web3.js/issues/1648
const listeners = process.listeners("warning");
listeners.forEach(listener => process.removeListener("warning", listener));

let options = {
logger: console
};
let options = { logger: console };

const inputArguments = process.argv.slice(2);
const userWantsGeneralHelp =
Expand Down
210 changes: 102 additions & 108 deletions packages/truffle-core/lib/command.js
Original file line number Diff line number Diff line change
@@ -1,138 +1,132 @@
var TaskError = require("./errors/taskerror");
var yargs = require("yargs/yargs");
var _ = require("lodash");
const TaskError = require("./errors/taskerror");
const yargs = require("yargs/yargs");
const { bundled, core } = require("../lib/version").info();
var OS = require("os");
const OS = require("os");
const analytics = require("../lib/services/analytics");

function Command(commands) {
this.commands = commands;
class Command {
constructor(commands) {
this.commands = commands;

var args = yargs();
let args = yargs();

Object.keys(this.commands).forEach(function(command) {
args = args.command(commands[command]);
});
Object.keys(this.commands).forEach(function(command) {
args = args.command(commands[command]);
});

this.args = args;
}
this.args = args;
}

Command.prototype.getCommand = function(inputStrings, noAliases) {
var argv = this.args.parse(inputStrings);
getCommand(inputStrings, noAliases) {
const argv = this.args.parse(inputStrings);

if (argv._.length === 0) {
return null;
}
if (argv._.length === 0) {
return null;
}

var firstInputString = argv._[0];
var chosenCommand = null;

// If the command wasn't specified directly, go through a process
// for inferring the command.
if (this.commands[firstInputString]) {
chosenCommand = firstInputString;
} else if (noAliases !== true) {
var currentLength = 1;
var availableCommandNames = Object.keys(this.commands);

// Loop through each letter of the input until we find a command
// that uniquely matches.
while (currentLength <= firstInputString.length) {
// Gather all possible commands that match with the current length
var possibleCommands = availableCommandNames.filter(function(
possibleCommand
) {
return (
possibleCommand.substring(0, currentLength) ===
firstInputString.substring(0, currentLength)
const firstInputString = argv._[0];
let chosenCommand = null;

// If the command wasn't specified directly, go through a process
// for inferring the command.
if (this.commands[firstInputString]) {
chosenCommand = firstInputString;
} else if (noAliases !== true) {
let currentLength = 1;
const availableCommandNames = Object.keys(this.commands);

// Loop through each letter of the input until we find a command
// that uniquely matches.
while (currentLength <= firstInputString.length) {
// Gather all possible commands that match with the current length
const possibleCommands = availableCommandNames.filter(
possibleCommand => {
return (
possibleCommand.substring(0, currentLength) ===
firstInputString.substring(0, currentLength)
);
}
);
});

// Did we find only one command that matches? If so, use that one.
if (possibleCommands.length === 1) {
chosenCommand = possibleCommands[0];
break;
// Did we find only one command that matches? If so, use that one.
if (possibleCommands.length === 1) {
chosenCommand = possibleCommands[0];
break;
}

currentLength += 1;
}
}

currentLength += 1;
if (chosenCommand == null) {
return null;
}
}

if (chosenCommand == null) {
return null;
}
const command = this.commands[chosenCommand];

var command = this.commands[chosenCommand];
return {
name: chosenCommand,
argv,
command
};
}

return {
name: chosenCommand,
argv: argv,
command: command
};
};
run(inputStrings, options, callback) {
const result = this.getCommand(inputStrings, options.noAliases);

Command.prototype.run = function(inputStrings, options, callback) {
if (typeof options === "function") {
callback = options;
options = {};
}
if (result == null) {
return callback(
new TaskError(
"Cannot find command based on input: " + JSON.stringify(inputStrings)
)
);
}

const result = this.getCommand(inputStrings, options.noAliases);
const argv = result.argv;

if (result == null) {
return callback(
new TaskError(
"Cannot find command based on input: " + JSON.stringify(inputStrings)
)
);
}
// Remove the task name itself.
if (argv._) argv._.shift();

var argv = result.argv;
// We don't need this.
delete argv["$0"];

// Remove the task name itself.
if (argv._) {
argv._.shift();
}
// Some options might throw if options is a Config object. If so, let's ignore those options.
const clone = {};
Object.keys(options).forEach(key => {
try {
clone[key] = options[key];
} catch (e) {
// Do nothing with values that throw.
}
});

// We don't need this.
delete argv["$0"];
const newOptions = Object.assign({}, clone, argv);

// Some options might throw if options is a Config object. If so, let's ignore those options.
var clone = {};
Object.keys(options).forEach(function(key) {
try {
clone[key] = options[key];
} catch (e) {
// Do nothing with values that throw.
result.command.run(newOptions, callback);
analytics.send({
command: result.name ? result.name : "other",
args: result.argv._,
version: bundled || "(unbundled) " + core
});
} catch (err) {
callback(err);
}
});

options = _.extend(clone, argv);
}

try {
result.command.run(options, callback);
analytics.send({
command: result.name ? result.name : "other",
args: result.argv._,
version: bundled || "(unbundled) " + core
});
} catch (err) {
callback(err);
displayGeneralHelp() {
this.args
.usage(
"Truffle v" +
(bundled || core) +
" - a development framework for Ethereum" +
OS.EOL +
OS.EOL +
"Usage: truffle <command> [options]"
)
.epilog("See more at http://truffleframework.com/docs")
.showHelp();
}
};

Command.prototype.displayGeneralHelp = function() {
this.args
.usage(
"Truffle v" +
(bundled || core) +
" - a development framework for Ethereum" +
OS.EOL +
OS.EOL +
"Usage: truffle <command> [options]"
)
.epilog("See more at http://truffleframework.com/docs")
.showHelp();
};
}

module.exports = Command;
4 changes: 2 additions & 2 deletions packages/truffle-core/lib/version.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const pkg = require("../package.json");
const solcpkg = require("solc/package.json");
const Web3 = require("web3");

const info = () => {
let bundleVersion;
Expand Down Expand Up @@ -45,7 +44,8 @@ const logSolidity = (logger = console, versionInformation, config) => {
};

const logWeb3 = (logger = console) => {
logger.log(`Web3.js v${Web3.version}`);
const web3Version = pkg.dependencies.web3;
logger.log(`Web3.js v${web3Version}`);
};

const logAll = (logger = console, config) => {
Expand Down
1 change: 0 additions & 1 deletion packages/truffle-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"fs-extra": "6.0.1",
"ganache-core": "2.5.5",
"hdkey": "^1.1.0",
"lodash": "4.17.11",
"mkdirp": "^0.5.1",
"mocha": "5.2.0",
"node-dir": "0.1.17",
Expand Down

0 comments on commit 1a88bf9

Please sign in to comment.