Use resolve-from instead of absolute paths.

This commit is contained in:
Brian Neisler 2017-03-29 13:41:22 -07:00
parent 59c8c41057
commit ad5aa1ea97
2 changed files with 8 additions and 12 deletions

View File

@ -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);
}

View File

@ -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",