This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2043 from trufflesuite/truffle-core-housekeeping
Internal improvement: Update truffle-core and remove some unneeded code
- Loading branch information
Showing
4 changed files
with
105 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters