Skip to content

Commit

Permalink
refactor: removed instances keyword and replaces this.S to this.serve…
Browse files Browse the repository at this point in the history
…rless
  • Loading branch information
eahefnawy committed May 24, 2016
1 parent 249c243 commit de86121
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 164 deletions.
2 changes: 1 addition & 1 deletion bin/serverless
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const serverless = new Serverless({

// check if it's a test (e.g. an integration test) and modify the serverless instance accordingly
if (process.argv.indexOf('--serverless-integration-test') > -1) {
serverless.instances.pluginManager.addPlugin(TestsPlugin);
serverless.pluginManager.addPlugin(TestsPlugin);
}

serverless.run();
20 changes: 9 additions & 11 deletions lib/Serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class Serverless {

this.version = Version;

this.instances = {};
this.instances.config = new Config(this, config);
this.instances.yamlParser = new YamlParser(this);
this.instances.pluginManager = new PluginManager(this);
this.instances.utils = new Utils(this);
this.instances.service = new Service(this);
this.config = new Config(this, config);
this.yamlParser = new YamlParser(this);
this.pluginManager = new PluginManager(this);
this.utils = new Utils(this);
this.service = new Service(this);
this.cli = new CLI(this, configObj.interactive);

this.classes = {};
this.classes.CLI = CLI;
Expand All @@ -33,16 +33,14 @@ class Serverless {
this.classes.Service = Service;
this.classes.Error = SError;

this.instances.pluginManager.loadAllPlugins();
this.pluginManager.loadAllPlugins();

this.instances.cli = new CLI(this, configObj.interactive);

this.inputToBeProcessed = this.instances.cli.processInput();
this.inputToBeProcessed = this.cli.processInput();
}

run() {
if (this.inputToBeProcessed.commands.length) {
this.instances.pluginManager.run(this.inputToBeProcessed.commands,
this.pluginManager.run(this.inputToBeProcessed.commands,
this.inputToBeProcessed.options);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/classes/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const path = require('path');

class Config {

constructor(S, config) {
this.S = S;
constructor(serverless, config) {
this.serverless = serverless;
this.serverlessPath = path.join(__dirname, '..');

if (config) this.update(config);
Expand Down
9 changes: 3 additions & 6 deletions lib/classes/PluginManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const Utils = require('./Utils');
const path = require('path');
const has = require('lodash').has;
const forEach = require('lodash').forEach;
Expand Down Expand Up @@ -44,9 +43,7 @@ class PluginManager {

// check if a Promise is returned
if (returnValue && returnValue.then instanceof Function) {
returnValue.then((value) => {
return value;
});
returnValue.then((value) => value);
}
});
}
Expand All @@ -58,10 +55,10 @@ class PluginManager {
}

loadCorePlugins() {
const utils = new Utils();
const pluginsDirectoryPath = path.join(__dirname, '../plugins');

const corePlugins = utils.readFileSync(path.join(pluginsDirectoryPath, 'Plugins.json')).plugins;
const corePlugins = this.serverless.utils
.readFileSync(path.join(pluginsDirectoryPath, 'Plugins.json')).plugins;

corePlugins.forEach((corePlugin) => {
const Plugin = require(path.join(pluginsDirectoryPath, corePlugin));
Expand Down
14 changes: 6 additions & 8 deletions lib/classes/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const BbPromise = require('bluebird');

class Service {

constructor(S, data) {
this.S = S;
constructor(serverless, data) {
this.serverless = serverless;

// Default properties
this.service = null;
Expand All @@ -27,13 +27,13 @@ class Service {
load(opts) {
const that = this;
const options = opts || {};
const servicePath = that.S.instances.config.servicePath;
const servicePath = that.serverless.config.servicePath;

if (!servicePath) {
throw new Error('ServicePath is not configured.');
}

return that.S.instances.yamlParser
return that.serverless.yamlParser
.parse(path.join(servicePath, 'serverless.yaml'))
.then((serverlessYaml) => {
that.service = serverlessYaml.service;
Expand All @@ -43,15 +43,13 @@ class Service {
that.resources = serverlessYaml.resources;
that.functions = serverlessYaml.functions;
})
.then(() => {
return that.S.instances.yamlParser.parse(path.join(servicePath, 'serverless.env.yaml'));
})
.then(() => that.serverless.yamlParser
.parse(path.join(servicePath, 'serverless.env.yaml')))
.then((serverlessEnvYaml) => {
that.environment = serverlessEnvYaml;
BbPromise.resolve(that);
})
.then(() => {

// Validate: Check stage exists
if (options.stage) this.getStage(options.stage);

Expand Down
4 changes: 2 additions & 2 deletions lib/classes/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const fse = BbPromise.promisifyAll(require('fs-extra'));

class Utils {

constructor(S) {
this.S = S;
constructor(serverless) {
this.serverless = serverless;
}

dirExistsSync(dirPath) {
Expand Down
6 changes: 3 additions & 3 deletions lib/classes/YamlParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const resolve = require('json-refs').resolveRefs;

class YamlParser {

constructor(S) {
this.S = S;
constructor(serverless) {
this.serverless = serverless;
}

parse(yamlFilePath) {
Expand All @@ -16,7 +16,7 @@ class YamlParser {
parentDir = parentDir.join('/');
process.chdir(parentDir);

const root = YAML.load(this.S.instances.utils.readFileSync(yamlFilePath).toString());
const root = YAML.load(this.serverless.utils.readFileSync(yamlFilePath).toString());
const options = {
filter: ['relative', 'remote'],
loaderOptions: {
Expand Down
1 change: 0 additions & 1 deletion lib/plugins/create.js

This file was deleted.

42 changes: 21 additions & 21 deletions lib/plugins/create/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class Create {
}

prompt() {
if (this.serverless.instances.config.interactive && !this.options.noGreeting) {
this.serverless.instances.cli.asciiGreeting();
if (this.serverless.config.interactive && !this.options.noGreeting) {
this.serverless.cli.asciiGreeting();
}

console.log('');
this.serverless.instances.cli.log('Creating new Serverless service...');
this.serverless.cli.log('Creating new Serverless service...');

return BbPromise.resolve();
}
Expand All @@ -62,17 +62,17 @@ class Create {
throw new this.serverless.classes.Error('Service names can only be alphanumeric and -');
}

this.serverless.instances.config
this.serverless.config
.update({ servicePath: path.join(process.cwd(), this.options.name) });
return BbPromise.resolve();
}

parse() {
const allPromises = [];
allPromises.push(this.serverless.instances.yamlParser.parse(path.join(this.serverless
.instances.config.serverlessPath, 'templates', 'serverless.yaml')));
allPromises.push(this.serverless.instances.utils.readFile(path.join(this.serverless
.instances.config.serverlessPath, 'templates', 'nodejs', 'package.json')));
allPromises.push(this.serverless.yamlParser.parse(path.join(this.serverless
.config.serverlessPath, 'templates', 'serverless.yaml')));
allPromises.push(this.serverless.utils.readFile(path.join(this.serverless
.config.serverlessPath, 'templates', 'nodejs', 'package.json')));
return BbPromise.all(allPromises);
}

Expand All @@ -96,27 +96,27 @@ class Create {
vars: {},
};

this.serverless.instances.utils.writeFileSync(path.join(this.serverless
.instances.config.servicePath, 'serverless.yaml'), serverlessYaml);
this.serverless.instances.utils.writeFileSync(path.join(this.serverless
.instances.config.servicePath, 'package.json'), packageJson);
this.serverless.instances.utils.writeFileSync(path.join(this.serverless
.instances.config.servicePath, 'serverless.env.yaml'), serverlessEnvYaml);
this.serverless.utils.writeFileSync(path.join(this.serverless
.config.servicePath, 'serverless.yaml'), serverlessYaml);
this.serverless.utils.writeFileSync(path.join(this.serverless
.config.servicePath, 'package.json'), packageJson);
this.serverless.utils.writeFileSync(path.join(this.serverless
.config.servicePath, 'serverless.env.yaml'), serverlessEnvYaml);

// copy handler.js
fse.copySync(path.join(this.serverless.instances.config.serverlessPath,
fse.copySync(path.join(this.serverless.config.serverlessPath,
'templates', 'nodejs', 'handler.js'), path.join(this.serverless
.instances.config.servicePath, 'handler.js'));
.config.servicePath, 'handler.js'));

return BbPromise.resolve();
}

finish() {
this.serverless.instances.cli.log(`Successfully created service "${this.options.name}"`);
this.serverless.instances.cli.log(' |- serverless.yaml');
this.serverless.instances.cli.log(' |- serverless.env.yaml');
this.serverless.instances.cli.log(' |- handler.js');
this.serverless.instances.cli.log(' |- package.json');
this.serverless.cli.log(`Successfully created service "${this.options.name}"`);
this.serverless.cli.log(' |- serverless.yaml');
this.serverless.cli.log(' |- serverless.env.yaml');
this.serverless.cli.log(' |- handler.js');
this.serverless.cli.log(' |- package.json');
return BbPromise.resolve();
}
}
Expand Down
34 changes: 17 additions & 17 deletions lib/plugins/create/tests/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ describe('Create', () => {
});

it('should NOT generate greeting if not interactive', () => {
const greetingStub = sinon.stub(create.serverless.instances.cli, 'asciiGreeting');
const greetingStub = sinon.stub(create.serverless.cli, 'asciiGreeting');
return create.prompt().then(() => {
expect(greetingStub.notCalled).to.be.equal(true);
create.serverless.instances.cli.asciiGreeting.restore();
create.serverless.cli.asciiGreeting.restore();
});
});

it('should generate greeting if interactive', () => {
create.serverless.instances.config.interactive = true;
const greetingStub = sinon.stub(create.serverless.instances.cli, 'asciiGreeting');
create.serverless.config.interactive = true;
const greetingStub = sinon.stub(create.serverless.cli, 'asciiGreeting');
return create.prompt().then(() => {
expect(greetingStub.calledOnce).to.be.equal(true);
create.serverless.instances.cli.asciiGreeting.restore();
create.serverless.instances.config.interactive = false;
create.serverless.cli.asciiGreeting.restore();
create.serverless.config.interactive = false;
});
});
});
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('Create', () => {
create.options.name = 'valid-service-name';
create.options.stage = 'dev';
create.options.region = 'aws_useast1';
create.validate().then(() => expect(create.serverless.instances.config.servicePath)
create.validate().then(() => expect(create.serverless.config.servicePath)
.to.be.equal(path.join(process.cwd(), create.options.name)));
});
});
Expand All @@ -112,21 +112,21 @@ describe('Create', () => {
fakeYaml = { service: '' };
fakeJson = { name: '' };
tmpDir = path.join(os.tmpdir(), (new Date).getTime().toString(), create.options.name);
create.serverless.instances.config.servicePath = tmpDir;
create.serverless.config.servicePath = tmpDir;
});

it('should generate handler.js', () => {
create.scaffold(fakeYaml, fakeJson).then(() => {
expect(create.serverless.instances.utils.fileExistsSync(path.join(tmpDir, 'handler.js')))
expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, 'handler.js')))
.to.be.equal(true);
});
});

it('should generate serverless.yaml and set correct service name', () => {
create.scaffold(fakeYaml, fakeJson).then(() => {
expect(create.serverless.instances.utils
expect(create.serverless.utils
.fileExistsSync(path.join(tmpDir, 'serverless.yaml'))).to.be.equal(true);
create.serverless.instances.yamlParser
create.serverless.yamlParser
.parse(path.join(tmpDir, 'serverless.yaml')).then((serverlessYaml) => {
expect(serverlessYaml.service).to.be.equal('new-service');
});
Expand All @@ -135,19 +135,19 @@ describe('Create', () => {

it('should generate package.json and set correct package name', () => {
create.scaffold(fakeYaml, fakeJson).then(() => {
expect(create.serverless.instances.utils
expect(create.serverless.utils
.fileExistsSync(path.join(tmpDir, 'package.json'))).to.be.equal(true);
const packageJson = create.serverless.instances.utils
const packageJson = create.serverless.utils
.readFileSync(path.join(tmpDir, 'package.json'));
expect(packageJson.name).to.be.equal('new-service');
});
});

it('should generate serverless.env.yaml and set correct stage and region', () => {
create.scaffold(fakeYaml, fakeJson).then(() => {
expect(create.serverless.instances.utils
expect(create.serverless.utils
.fileExistsSync(path.join(tmpDir, 'serverless.env.yaml'))).to.be.equal(true);
create.serverless.instances.yamlParser
create.serverless.yamlParser
.parse(path.join(tmpDir, 'serverless.env.yaml')).then((serverlessEnvYaml) => {
expect(typeof serverlessEnvYaml.stages.dev.regions.aws_useast1).to.be.equal('object');
});
Expand All @@ -157,10 +157,10 @@ describe('Create', () => {

describe('#finish()', () => {
it('should log 5 messages', () => {
const logStub = sinon.stub(create.serverless.instances.cli, 'log');
const logStub = sinon.stub(create.serverless.cli, 'log');
create.finish();
expect(logStub.callCount).to.be.equal(5);
create.serverless.instances.cli.log.restore();
create.serverless.cli.log.restore();
});
});
});
12 changes: 6 additions & 6 deletions lib/plugins/deploy/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const BbPromise = require('bluebird');
const Zip = require('node-zip');

class Deploy {
constructor(S) {
this.S = S;
constructor(serverless) {
this.serverless = serverless;
this.commands = {
deploy: {
usage: 'deploy lambda zip.',
Expand All @@ -30,11 +30,11 @@ class Deploy {
deploy(options) {
this.options = options;
const allPromises = [];
this.S.instances.service.getAllFunctions().forEach((f) => {
const fConfig = this.S.instances.service.getFunction(f);
this.serverless.service.getAllFunctions().forEach((f) => {
const fConfig = this.serverless.service.getFunction(f);

const configParams = {
FunctionName: `${this.S.instances.service.service}-${f}`,
FunctionName: `${this.serverless.service.service}-${f}`,
Description: fConfig.description,
Handler: fConfig.handler,
MemorySize: fConfig.memory_size,
Expand All @@ -43,7 +43,7 @@ class Deploy {

allPromises.push(this.Lambda.updateFunctionConfigurationPromised(configParams));
const codeParams = {
FunctionName: `${this.S.instances.service.service}-${f}`,
FunctionName: `${this.serverless.service.service}-${f}`,
ZipFile: new Zip(),
};
allPromises.push(this.Lambda.updateFunctionCodePromised(codeParams));
Expand Down
Loading

0 comments on commit de86121

Please sign in to comment.