refactor(@embark/storage): wrap require.resolve in try/catch

Wrap `require.resolve` in try/catch instead of testing the path with
`embark.fs.access`.
This commit is contained in:
Michael Bradley, Jr 2019-07-05 16:11:51 -05:00 committed by Michael Bradley
parent 13e926659f
commit bd602b45ee
1 changed files with 63 additions and 62 deletions

View File

@ -99,83 +99,84 @@ class StorageProcessesLauncher {
if (self.processes[storageName]) { if (self.processes[storageName]) {
return callback(__('Storage process already started')); return callback(__('Storage process already started'));
} }
const filePath = require.resolve(`embark-${storageName}/process`, {paths: [embarkPath('node_modules')]});
this.embark.fs.access(filePath, (err) => {
if (err) {
return callback(__('No process file for this storage type (%s) exists. Please start the process locally.', storageName));
}
let cmd = (storageName === 'swarm' ? (self.storageConfig.swarmPath || 'swarm') : 'ipfs'); let filePath;
try {
filePath = require.resolve(`embark-${storageName}/process`, {paths: [embarkPath('node_modules')]});
} catch (e) {
return callback(__('No process file for this storage type (%s) exists. Please start the process locally.', storageName));
}
const program = shellJs.which(cmd); let cmd = (storageName === 'swarm' ? (self.storageConfig.swarmPath || 'swarm') : 'ipfs');
if (!program) {
self.logger.warn(__('{{storageName}} is not installed or your configuration is not right', {storageName}).yellow);
self.logger.info(__('You can install and get more information here: ').yellow + References[storageName].underline);
return callback(__('%s not installed', storageName));
}
self.logger.info(__(`Starting %s process`, storageName).cyan); const program = shellJs.which(cmd);
self.processes[storageName] = new ProcessLauncher({ if (!program) {
modulePath: filePath, self.logger.warn(__('{{storageName}} is not installed or your configuration is not right', {storageName}).yellow);
name: storageName, self.logger.info(__('You can install and get more information here: ').yellow + References[storageName].underline);
logger: self.logger, return callback(__('%s not installed', storageName));
events: self.events, }
embark: self.embark,
silent: self.logger.logLevel !== 'trace', self.logger.info(__(`Starting %s process`, storageName).cyan);
exitCallback: self.processExited.bind(this, storageName) self.processes[storageName] = new ProcessLauncher({
}); modulePath: filePath,
this.events.request("blockchain:object", (blockchain) => { name: storageName,
blockchain.onReady(() => { logger: self.logger,
blockchain.determineDefaultAccount((err, defaultAccount) => { events: self.events,
if (err) { embark: self.embark,
return callback(err); silent: self.logger.logLevel !== 'trace',
exitCallback: self.processExited.bind(this, storageName)
});
this.events.request("blockchain:object", (blockchain) => {
blockchain.onReady(() => {
blockchain.determineDefaultAccount((err, defaultAccount) => {
if (err) {
return callback(err);
}
self.processes[storageName].send({
action: constants.storage.init, options: {
storageConfig: self.storageConfig,
blockchainConfig: self.blockchainConfig,
cors: self.buildCors(),
defaultAccount: defaultAccount
} }
self.processes[storageName].send({
action: constants.storage.init, options: {
storageConfig: self.storageConfig,
blockchainConfig: self.blockchainConfig,
cors: self.buildCors(),
defaultAccount: defaultAccount
}
});
}); });
}); });
}); });
});
self.processes[storageName].on('result', constants.storage.initiated, (msg) => { self.processes[storageName].on('result', constants.storage.initiated, (msg) => {
if (msg.error) { if (msg.error) {
self.processes[storageName].disconnect(); self.processes[storageName].disconnect();
delete self.processes[storageName]; delete self.processes[storageName];
return callback(msg.error); return callback(msg.error);
} }
self.logger.info(__(`${storageName} process started`).cyan); self.logger.info(__(`${storageName} process started`).cyan);
callback(); callback();
}); });
self.processes[storageName].on('result', constants.storage.restart, (_msg) => { self.processes[storageName].on('result', constants.storage.restart, (_msg) => {
self.restartCalled = true; self.restartCalled = true;
self.logger.info(__(`Restarting ${storageName} process...`).cyan); self.logger.info(__(`Restarting ${storageName} process...`).cyan);
self.processes[storageName].kill(); self.processes[storageName].kill();
delete this.processes[storageName]; delete this.processes[storageName];
}); });
self.processes[storageName].on('result', constants.storage.exit, (_msg) => { self.processes[storageName].on('result', constants.storage.exit, (_msg) => {
self.processes[storageName].kill(); self.processes[storageName].kill();
delete this.processes[storageName]; delete this.processes[storageName];
this.events.emit(constants.storage.exit); this.events.emit(constants.storage.exit);
}); });
self.events.on('logs:swarm:enable', () => { self.events.on('logs:swarm:enable', () => {
self.processes[storageName].silent = false; self.processes[storageName].silent = false;
}); });
self.events.on('logs:swarm:disable', () => {
self.processes[storageName].silent = true;
});
self.events.on('logs:swarm:disable', () => {
self.processes[storageName].silent = true;
}); });
} }
stopProcess(storageName, cb) { stopProcess(storageName, cb) {
if(this.processes[storageName]) { if(this.processes[storageName]) {
this.manualExit = true; this.manualExit = true;