Skip to content

Commit

Permalink
Merge pull request serverless#3373 from serverless/add-serverless-alp…
Browse files Browse the repository at this point in the history
…ha-autoloading

Add serverless-alpha autoloading
  • Loading branch information
eahefnawy authored Mar 29, 2017
2 parents 03bc013 + 4ee198c commit 2d2c254
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class Serverless {
this.version = Version;

this.yamlParser = new YamlParser(this);
this.pluginManager = new PluginManager(this);
this.utils = new Utils(this);
this.service = new Service(this);
this.variables = new Variables(this);
this.pluginManager = new PluginManager(this);

// use the servicePath from the options or try to find it in the CWD
configObject.servicePath = configObject.servicePath || this.utils.findServicePath();
Expand All @@ -38,11 +38,11 @@ class Serverless {
this.classes = {};
this.classes.CLI = CLI;
this.classes.YamlParser = YamlParser;
this.classes.PluginManager = PluginManager;
this.classes.Utils = Utils;
this.classes.Service = Service;
this.classes.Variables = Variables;
this.classes.Error = ServerlessError;
this.classes.PluginManager = PluginManager;

this.serverlessDirPath = path.join(os.homedir(), '.serverless');
}
Expand Down
28 changes: 28 additions & 0 deletions lib/classes/PluginManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const os = require('os');
const Module = require('module');
const BbPromise = require('bluebird');
const _ = require('lodash');
Expand All @@ -15,6 +16,33 @@ class PluginManager {
this.plugins = [];
this.commands = {};
this.hooks = {};

this.autoloadServerlessAlphaPlugin();
}

autoloadServerlessAlphaPlugin() {
// get the path to the global node_modules dir
// see: https://docs.npmjs.com/files/folders
let prefixPath;
let globalNodeModulesPath;
if (os.platform === 'win32') {
const nodeExecPath = path.join(path.sep, 'node.exe');
prefixPath = process.execPath.replace(nodeExecPath, '');
globalNodeModulesPath = path.join(prefixPath, 'node_modules');
} else {
const nodeExecPath = path.join(path.sep, 'bin', 'node');
prefixPath = process.execPath.replace(nodeExecPath, '');
globalNodeModulesPath = path.join(prefixPath, 'lib', 'node_modules');
}

const serverlessAlphaPluginPath = path
.join(path.join(globalNodeModulesPath, 'serverless-alpha'));

if (this.serverless.utils.dirExistsSync(serverlessAlphaPluginPath)) {
// eslint-disable-next-line
const ServerlessAlpha = require(serverlessAlphaPluginPath);
this.addPlugin(ServerlessAlpha);
}
}

setCliOptions(options) {
Expand Down
61 changes: 61 additions & 0 deletions lib/classes/PluginManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,67 @@ describe('PluginManager', () => {
});
});

describe('#loadServerlessAlphaPlugin()', () => {
let processExecPath;
let tmpDirPath;
let globalNodeModulesPath;
let prefixPath;

beforeEach(() => {
// create global node_modules dir
// see: https://docs.npmjs.com/files/folders
let nodeExecPath;
if (os.platform === 'win32') {
nodeExecPath = path.join(path.sep, 'node.exe');
prefixPath = process.execPath.replace(nodeExecPath, '');
globalNodeModulesPath = path.join(prefixPath, 'node_modules');
} else {
nodeExecPath = path.join(path.sep, 'bin', 'node');
prefixPath = process.execPath.replace(nodeExecPath, '');
globalNodeModulesPath = path.join(prefixPath, 'lib', 'node_modules');
}

// create a new tmpDir for the homeDir path
tmpDirPath = testUtils.getTmpDirPath();
fse.mkdirsSync(tmpDirPath);

const tmpExecPath = path.join(tmpDirPath, prefixPath);
fse.mkdirsSync(tmpExecPath);
fse.mkdirsSync(path.join(tmpDirPath, globalNodeModulesPath));

// save the process.execPath so that we can reset this later on
processExecPath = process.execPath;
process.execPath = tmpExecPath;
});

afterEach(() => {
// recover the process.execPath
process.execPath = processExecPath;
});

it('should auto load the serverless-alpha plugin if globally installed', () => {
// add a mock for the serverless-alpha plugin in the tmpDir
class ServerlessAlpha {}

const globalAlphaPluginPath = path.join(tmpDirPath,
globalNodeModulesPath, 'serverless-alpha');

fse.mkdirsSync(globalAlphaPluginPath);
testUtils.installPlugin(globalAlphaPluginPath, ServerlessAlpha);

pluginManager.autoloadServerlessAlphaPlugin();

expect(pluginManager.plugins.length).to.equal(1);
expect(pluginManager.plugins[0].constructor.name).to.equal('ServerlessAlpha');
});

it('should not load the serverless-alpha plugin if not globally installed', () => {
pluginManager.autoloadServerlessAlphaPlugin();

expect(pluginManager.plugins.length).to.equal(0);
});
});

describe('#setCliOptions()', () => {
it('should set the cliOptions object', () => {
const options = { foo: 'bar' };
Expand Down

0 comments on commit 2d2c254

Please sign in to comment.