From ad5aa1ea97b56883eaef32a3bcc2f9598190403d Mon Sep 17 00:00:00 2001 From: Brian Neisler Date: Wed, 29 Mar 2017 13:41:22 -0700 Subject: [PATCH] Use resolve-from instead of absolute paths. --- lib/classes/PluginManager.js | 19 +++++++------------ package.json | 1 + 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/classes/PluginManager.js b/lib/classes/PluginManager.js index e7c0e817a..c114824b5 100644 --- a/lib/classes/PluginManager.js +++ b/lib/classes/PluginManager.js @@ -5,6 +5,7 @@ const os = require('os'); const Module = require('module'); const BbPromise = require('bluebird'); const childProcess = require('child_process'); +const resolveFrom = require('resolve-from'); const _ = require('lodash'); class PluginManager { @@ -21,11 +22,7 @@ class PluginManager { autoloadServerlessAlphaPlugin() { try { - const localPluginPath = path.join( - this.serverless.config.servicePath, - 'node_modules', - 'serverless-alpha' - ); + const localPluginPath = resolveFrom(this.serverless.config.servicePath, 'serverless-alpha'); const ServerlessAlpha = require(localPluginPath); //eslint-disable-line this.addPlugin(ServerlessAlpha); return; @@ -36,30 +33,28 @@ class PluginManager { let prefixPath; try { - prefixPath = childProcess.execSync('npm config get prefix --silent').toString(); + prefixPath = childProcess.execSync('npm config get prefix --silent').toString().trim(); } catch (e) { // serverless-alpha not found globally via npm prefixPath = false; } - prefixPath = prefixPath.trim(); - if (prefixPath && prefixPath.length) { // get the path to the global node_modules dir // see: https://docs.npmjs.com/files/folders let globalNodeModulesPath; if (os.platform === 'win32') { - globalNodeModulesPath = path.join(prefixPath, 'node_modules'); + globalNodeModulesPath = prefixPath; } else { - globalNodeModulesPath = path.join(prefixPath, 'lib', 'node_modules'); + globalNodeModulesPath = path.join(prefixPath, 'lib'); } - const serverlessAlphaPluginPath = path.join( + const serverlessAlphaPluginPath = resolveFrom( globalNodeModulesPath, 'serverless-alpha' ); - if (this.serverless.utils.dirExistsSync(serverlessAlphaPluginPath)) { + if (serverlessAlphaPluginPath) { const ServerlessAlpha = require(serverlessAlphaPluginPath); //eslint-disable-line this.addPlugin(ServerlessAlpha); } diff --git a/package.json b/package.json index c931725c9..c3dd54d43 100644 --- a/package.json +++ b/package.json @@ -100,6 +100,7 @@ "moment": "^2.13.0", "node-fetch": "^1.5.3", "replaceall": "^0.1.6", + "resolve-from": "^2.0.0", "semver": "^5.0.3", "semver-regex": "^1.0.0", "shelljs": "^0.6.0",