2017-12-30 23:25:59 +00:00
var Npm = require ( './npm.js' ) ;
2017-12-30 20:52:51 +00:00
class LibraryManager {
2018-07-20 18:14:52 +00:00
constructor ( embark ) {
2018-07-20 18:28:46 +00:00
this . embark = embark ;
2018-07-20 18:14:52 +00:00
this . config = embark . config ;
2017-12-30 20:52:51 +00:00
this . contractsConfig = this . config . contractsConfig ;
2018-01-10 16:15:32 +00:00
this . storageConfig = this . config . storageConfig ;
2017-12-30 20:52:51 +00:00
2017-12-30 21:48:53 +00:00
this . determineVersions ( ) ;
2017-12-30 20:52:51 +00:00
this . registerCommands ( ) ;
2018-08-10 10:31:10 +00:00
this . registerAPICommands ( ) ;
2017-12-30 23:12:16 +00:00
this . listenToCommandsToGetVersions ( ) ;
this . listenToCommandsToGetLibrary ( ) ;
2017-12-30 21:48:53 +00:00
}
determineVersions ( ) {
this . versions = { } ;
let solcVersionInConfig = this . contractsConfig . versions . solc ;
2018-04-20 23:45:57 +00:00
let web3VersionInConfig = this . contractsConfig . versions [ "web3" ] ;
2018-01-10 16:15:32 +00:00
let ipfsApiVersion = this . storageConfig . versions [ "ipfs-api" ] ;
2017-12-30 21:48:53 +00:00
this . versions [ 'solc' ] = solcVersionInConfig ;
this . versions [ 'web3' ] = web3VersionInConfig ;
this . versions [ 'ipfs-api' ] = ipfsApiVersion ;
2018-04-16 15:38:46 +00:00
Object . keys ( this . versions ) . forEach ( versionKey => {
const newVersion = this . versions [ versionKey ] . trim ( ) ;
if ( newVersion !== this . versions [ versionKey ] ) {
2018-05-25 07:13:57 +00:00
this . embark . logger . warn ( _ _ ( 'There is a space in the version of {{versionKey}}. We corrected it for you ({{correction}}).' , { versionKey : versionKey , correction : ` " ${ this . versions [ versionKey ] } " => " ${ newVersion } " ` } ) ) ;
2018-04-16 15:38:46 +00:00
this . versions [ versionKey ] = newVersion ;
}
} ) ;
2017-12-30 20:52:51 +00:00
}
registerCommands ( ) {
const self = this ;
this . embark . registerConsoleCommand ( ( cmd , _options ) => {
2018-08-08 12:42:45 +00:00
return {
match : ( ) => cmd === "versions" || cmd === _ _ ( 'versions' ) ,
process : ( callback ) => {
let text = [ _ _ ( 'versions in use' ) + ':' ] ;
for ( let lib in self . versions ) {
text . push ( lib + ": " + self . versions [ lib ] ) ;
}
callback ( null , text . join ( '\n' ) ) ;
2017-12-30 21:48:53 +00:00
}
2018-08-08 12:42:45 +00:00
} ;
2017-12-30 20:52:51 +00:00
} ) ;
}
2018-08-10 10:31:10 +00:00
registerAPICommands ( ) {
const self = this ;
self . embark . registerAPICall (
'get' ,
'/embark-api/versions' ,
( req , res ) => {
const versions = Object . keys ( self . versions ) . map ( ( name ) => ( { value : self . versions [ name ] , name } ) ) ;
res . send ( versions ) ;
}
) ;
}
2017-12-30 23:12:16 +00:00
listenToCommandsToGetVersions ( ) {
2017-12-30 21:48:53 +00:00
const self = this ;
for ( let libName in this . versions ) {
let lib = self . versions [ libName ] ;
this . embark . events . setCommandHandler ( 'version:get:' + libName , ( cb ) => {
cb ( lib ) ;
} ) ;
}
}
2017-12-30 23:12:16 +00:00
listenToCommandsToGetLibrary ( ) {
2018-06-14 08:09:02 +00:00
let npm = new Npm ( { logger : this . embark . logger } ) ;
2017-12-31 00:34:15 +00:00
this . embark . events . setCommandHandler ( 'version:getPackageLocation' , ( libName , version , cb ) => {
2018-04-20 23:52:51 +00:00
npm . getPackageVersion ( libName , version , cb ) ;
2017-12-30 23:12:16 +00:00
} ) ;
2018-06-13 13:16:54 +00:00
this . embark . events . setCommandHandler ( 'version:getPackagePath' , ( libName , version , cb ) => {
2018-06-14 08:09:02 +00:00
cb ( null , Npm . getPackagePath ( libName , version ) ) ;
2018-06-13 13:16:54 +00:00
} ) ;
2017-12-30 23:12:16 +00:00
}
2017-12-30 20:52:51 +00:00
}
module . exports = LibraryManager ;