embark-area-51/lib/modules/library_manager/index.js

77 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-12-30 23:25:59 +00:00
var Npm = require('./npm.js');
class LibraryManager {
constructor(embark) {
2018-07-20 18:28:46 +00:00
this.embark = embark;
this.config = embark.config;
this.contractsConfig = this.config.contractsConfig;
2018-01-10 16:15:32 +00:00
this.storageConfig = this.config.storageConfig;
this.determineVersions();
this.registerCommands();
2017-12-30 23:12:16 +00:00
this.listenToCommandsToGetVersions();
this.listenToCommandsToGetLibrary();
}
determineVersions() {
this.versions = {};
let solcVersionInConfig = this.contractsConfig.versions.solc;
let web3VersionInConfig = this.contractsConfig.versions["web3"];
2018-01-10 16:15:32 +00:00
let ipfsApiVersion = this.storageConfig.versions["ipfs-api"];
this.versions['solc'] = solcVersionInConfig;
this.versions['web3'] = web3VersionInConfig;
this.versions['ipfs-api'] = ipfsApiVersion;
Object.keys(this.versions).forEach(versionKey => {
const newVersion = this.versions[versionKey].trim();
if (newVersion !== this.versions[versionKey]) {
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}"`}));
this.versions[versionKey] = newVersion;
}
});
}
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'));
}
2018-08-08 12:42:45 +00:00
};
});
}
2017-12-30 23:12:16 +00:00
listenToCommandsToGetVersions() {
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() {
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
});
this.embark.events.setCommandHandler('version:getPackagePath', (libName, version, cb) => {
cb(null, Npm.getPackagePath(libName, version));
});
2017-12-30 23:12:16 +00:00
}
}
module.exports = LibraryManager;