Skip to content

Commit

Permalink
v2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dherault committed Mar 26, 2016
1 parent 65f3438 commit 1a33b4a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-offline",
"version": "2.0.1",
"version": "2.1.0",
"description": "A Serverless plugin to emulate AWS APIG and Lambda offline.",
"main": "src/index.js",
"scripts": {},
Expand Down
47 changes: 30 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = S => {
{
option: 'prefix',
shortcut: 'p',
description: 'Add a prefix to every path, to send your requests to http://localhost:3000/prefix/[your_path] instead.'
description: 'Adds a prefix to every path, to send your requests to http://localhost:3000/prefix/[your_path] instead.'
},
{
option: 'port',
Expand Down Expand Up @@ -88,6 +88,7 @@ module.exports = S => {
// Load everything!
this.project = S.getProject();

this.envVars = {};
process.env.IS_OFFLINE = true;

this._setOptions();
Expand Down Expand Up @@ -121,7 +122,7 @@ module.exports = S => {
const stageVariables = stages[this.options.stage];
this.options.region = userOptions.region || Object.keys(stageVariables.regions)[0];

// Not really an option, but conviennient for latter use
// Not options, but conviennient for latter use
this.velocityContextOptions = {
stageVariables,
stage: this.options.stage,
Expand Down Expand Up @@ -169,6 +170,18 @@ module.exports = S => {

if (fun.runtime !== 'nodejs') return;

let populatedFun;
try {
populatedFun = fun.toObjectPopulated({
stage: this.options.stage,
region: this.options.region,
});
}
catch(err) {
serverlessLog(`Error while populating function '${fun.name}' with stage '${this.options.stage}' and region '${this.options.region}':`);
this._logAndExit(err.stack);
}

const funName = fun.name;
const handlerParts = fun.handler.split('/').pop().split('.');
const handlerPath = fun.getRootPath(handlerParts[0]);
Expand All @@ -178,22 +191,10 @@ module.exports = S => {
serverlessLog(`Routes for ${fun.name}:`);

// Add a route for each endpoint
fun.endpoints.forEach(ep => {
populatedFun.endpoints.forEach(endpoint => {

let endpoint;
let firstCall = true;

try {
endpoint = ep.toObjectPopulated({
stage: this.options.stage,
region: this.options.region,
});
}
catch(err) {
serverlessLog(`Error while populating endpoint '${ep.method} ${ep.path}' with stage '${this.options.stage}' and region '${this.options.region}':`);
this._logAndExit(err.stack);
}

const epath = endpoint.path;
const method = endpoint.method.toUpperCase();
const requestTemplates = endpoint.requestTemplates;
Expand Down Expand Up @@ -224,16 +225,28 @@ module.exports = S => {
// Holds the response to do async op
const response = reply.response().hold();

// Sets env variables
// Clears old vars
for (let key in this.envVars) {
delete process.env[key];
}

// Declare new ones
this.envVars = populatedFun.environment;
for (let key in this.envVars) {
process.env[key] = this.envVars[key];
}

// First we try to load the handler
let handler;
try {
if (!this.options.skipCacheInvalidation) {
debugLog('Invalidating cache...');

Object.keys(require.cache).forEach(key => {
for (let key in require.cache) {
// Require cache invalidation, brutal and fragile. Might cause errors, if so, please submit issue.
if (!key.match('node_modules')) delete require.cache[key];
});
}
}

debugLog(`Loading handler... (${handlerPath})`);
Expand Down

0 comments on commit 1a33b4a

Please sign in to comment.