From 2ddf2ca28144eb7fc63664022b33f01018e12cc5 Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Thu, 3 Sep 2020 10:55:37 +0200 Subject: [PATCH] refactor: Refactor to async/await --- lib/Serverless.js | 52 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/lib/Serverless.js b/lib/Serverless.js index c27ba0b8ce5..e747cfd9b34 100644 --- a/lib/Serverless.js +++ b/lib/Serverless.js @@ -116,7 +116,7 @@ class Serverless { }); }); } - eventuallyFallbackToLocal() { + async eventuallyFallbackToLocal() { if ( this.pluginManager.serverlessConfigFile && this.pluginManager.serverlessConfigFile.enableLocalInstallationFallback != null @@ -128,32 +128,34 @@ class Serverless { 'Remove this setting to clear this deprecation warning' ); } - return resolve(process.cwd(), 'serverless').then( - ({ realPath }) => { - if (realPath === __filename) { - this.isLocallyInstalled = true; - return null; - } - if ( - this.pluginManager.serverlessConfigFile && - this.pluginManager.serverlessConfigFile.enableLocalInstallationFallback != null && - !this.pluginManager.serverlessConfigFile.enableLocalInstallationFallback - ) { - return null; - } - this.cli.log('Running "serverless" installed locally (in service node_modules)'); - // TODO: Replace below fallback logic with more straightforward one at top of the CLI - // when we willl drop support for the "disableLocalInstallationFallback" setting - this.isOverridenByLocal = true; - const ServerlessLocal = require(realPath); - const serverlessLocal = new ServerlessLocal(); - this.invokedInstance = serverlessLocal; - return serverlessLocal.init(); - }, - error => { + const localServerlessPath = await (async () => { + try { + return (await resolve(process.cwd(), 'serverless')).realPath; + } catch (error) { if (!isModuleNotFoundError(error, 'serverless')) throw error; + return null; } - ); + })(); + if (!localServerlessPath) return; + if (localServerlessPath === __filename) { + this.isLocallyInstalled = true; + return; + } + if ( + this.pluginManager.serverlessConfigFile && + this.pluginManager.serverlessConfigFile.enableLocalInstallationFallback != null && + !this.pluginManager.serverlessConfigFile.enableLocalInstallationFallback + ) { + return; + } + this.cli.log('Running "serverless" installed locally (in service node_modules)'); + // TODO: Replace below fallback logic with more straightforward one at top of the CLI + // when we willl drop support for the "disableLocalInstallationFallback" setting + this.isOverridenByLocal = true; + const ServerlessLocal = require(localServerlessPath); + const serverlessLocal = new ServerlessLocal(); + this.invokedInstance = serverlessLocal; + await serverlessLocal.init(); } run() {