2018-10-13 23:12:35 +00:00
let utils = require ( '../../utils/utils' ) ;
class Suggestions {
2018-10-14 16:47:29 +00:00
constructor ( embark , options ) {
2018-10-16 01:54:32 +00:00
this . embark = embark ;
2018-10-16 01:42:57 +00:00
this . events = embark . events ;
2018-10-13 23:12:35 +00:00
this . plugins = options . plugins ;
this . registerApi ( ) ;
2018-10-16 01:42:57 +00:00
this . listenToEvents ( ) ;
this . contracts = { }
2018-10-13 23:12:35 +00:00
}
registerApi ( ) {
2018-10-16 01:54:32 +00:00
this . embark . registerAPICall ( 'post' , '/embark-api/suggestions' , ( req , res ) => {
2018-10-13 23:12:35 +00:00
let suggestions = this . getSuggestions ( req . body . command )
res . send ( { result : suggestions } )
} ) ;
}
2018-10-16 01:42:57 +00:00
listenToEvents ( ) {
this . events . on ( "deploy:contract:deployed" , ( contract ) => {
2018-10-14 16:47:29 +00:00
this . contracts [ contract . className ] = contract
2018-10-16 01:42:57 +00:00
} )
}
2018-10-14 16:47:29 +00:00
2018-10-13 23:12:35 +00:00
getSuggestions ( cmd ) {
2018-10-17 17:22:08 +00:00
if ( ! cmd ) return [ ]
2018-10-16 01:42:57 +00:00
cmd = cmd . toLowerCase ( )
2018-10-16 12:44:50 +00:00
let suggestions = [ ]
2018-10-14 16:47:29 +00:00
2018-10-16 12:01:55 +00:00
suggestions . push ( { value : 'web3.eth' , command _type : "web3 object" , description : "module for interacting with the Ethereum network" } )
suggestions . push ( { value : 'web3.net' , command _type : "web3 object" , description : "module for interacting with network properties" } )
suggestions . push ( { value : 'web3.shh' , command _type : "web3 object" , description : "module for interacting with the whisper protocol" } )
suggestions . push ( { value : 'web3.bzz' , command _type : "web3 object" , description : "module for interacting with the swarm network" } )
suggestions . push ( { value : 'web3.eth.getAccounts()' , command _type : "web3 object" , description : "get list of accounts" } )
2018-10-14 16:47:29 +00:00
2018-10-16 01:42:57 +00:00
for ( let contractName in this . contracts ) {
2018-10-14 16:47:29 +00:00
let contract = this . contracts [ contractName ]
2018-10-16 12:01:55 +00:00
suggestions . push ( { value : contract . className , command _type : "web3 object" , description : "contract deployed at " + contract . deployedAddress } ) ;
2018-10-14 16:47:29 +00:00
2018-10-16 12:01:55 +00:00
suggestions . push ( { value : "profile " + contract . className , command _type : "embark command" , description : "profile " + contract . className + " contract" } ) ;
2018-10-16 01:42:57 +00:00
}
2018-10-14 16:47:29 +00:00
2018-10-16 01:42:57 +00:00
// TODO: make this registered through an API instead
2018-10-14 16:47:29 +00:00
2018-10-16 12:01:55 +00:00
suggestions . push ( { value : 'help' , command _type : "embark command" , description : "displays quick list of some of the available embark commands" } ) ;
suggestions . push ( { value : 'versions' , command _type : "embark command" , description : "display versions in use for libraries and tools like web3 and solc" } ) ;
suggestions . push ( { value : 'ipfs' , command _type : "javascript object" , description : "instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)" } ) ;
suggestions . push ( { value : 'swarm' , command _type : "javascript object" , description : "instantiated swarm-api object configured to the current environment (available if swarm is enabled)" } ) ;
suggestions . push ( { value : 'web3' , command _type : "javascript object" , description : "instantiated web3.js object configured to the current environment" } ) ;
suggestions . push ( { value : 'EmbarkJS' , command _type : "javascript object" , description : "EmbarkJS static functions for Storage, Messages, Names, etc." } ) ;
2018-10-14 16:47:29 +00:00
2018-10-16 01:42:57 +00:00
// sort first the ones that match the command at the beginning of the string, then prefer smaller commands first
2018-10-16 12:01:55 +00:00
return utils . fuzzySearch ( cmd , suggestions , ( result ) => { return result . value + " " + result . description } ) . map ( ( x ) => x . original ) . sort ( ( x , y ) => {
2018-10-16 01:42:57 +00:00
let diff = x . value . indexOf ( cmd ) - y . value . indexOf ( cmd )
if ( diff !== 0 ) return diff ;
return x . value . length - y . value . length ;
} ) ;
2018-10-13 23:12:35 +00:00
}
}
module . exports = Suggestions ;