Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
mj1618 committed Jul 11, 2021
1 parent c945ad4 commit 9bd7b07
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 49 deletions.
103 changes: 67 additions & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { topicNameFromArn } from "./helpers";
import { spawn } from "child_process";
import { get, has } from "lodash/fp";

import { loadServerlessConfig } from "./sls-config-parser"
import { loadServerlessConfig } from "./sls-config-parser";

class ServerlessOfflineSns {
private config: any;
Expand Down Expand Up @@ -95,7 +95,7 @@ class ServerlessOfflineSns {
this.accountId = this.config.accountId || "123456789012";
const offlineConfig =
this.serverless.service.custom["serverless-offline"] || {};
this.servicesDirectory = this.config.servicesDirectory || ""
this.servicesDirectory = this.config.servicesDirectory || "";
this.location = process.cwd();
const locationRelativeToCwd =
this.options.location || this.config.location || offlineConfig.location;
Expand Down Expand Up @@ -204,37 +204,63 @@ class ServerlessOfflineSns {
this.debug("subscribing functions");
const subscribePromises: Array<Promise<any>> = [];
if (this.servicesDirectory) {
shell.cd(this.servicesDirectory);
for (const directory of shell.ls("-d", "*/")) {
shell.cd(directory);
const service = directory.split("/")[0];
const serverless = await loadServerlessConfig(shell.pwd(), this.debug);
this.debug("Processing subscriptions for ", service);
this.debug("shell.pwd()", shell.pwd());
this.debug("serverless functions", serverless.service.functions);
const subscriptions = this.getResourceSubscriptions(serverless);
subscriptions.forEach((subscription) =>
subscribePromises.push(this.subscribeFromResource(subscription, this.location))
shell.cd(this.servicesDirectory);
for (const directory of shell.ls("-d", "*/")) {
shell.cd(directory);
const service = directory.split("/")[0];
const serverless = await loadServerlessConfig(shell.pwd(), this.debug);
this.debug("Processing subscriptions for ", service);
this.debug("shell.pwd()", shell.pwd());
this.debug("serverless functions", serverless.service.functions);
const subscriptions = this.getResourceSubscriptions(serverless);
subscriptions.forEach((subscription) =>
subscribePromises.push(
this.subscribeFromResource(subscription, this.location)
)
);
Object.keys(serverless.service.functions).map((fnName) => {
const fn = serverless.service.functions[fnName];
subscribePromises.push(
Promise.all(
fn.events
.filter((event) => event.sns != null)
.map((event) => {
return this.subscribe(
serverless,
fnName,
event.sns,
shell.pwd()
);
})
)
Object.keys(serverless.service.functions).map(fnName => {
const fn = serverless.service.functions[fnName];
subscribePromises.push(Promise.all(fn.events.filter(event => event.sns != null).map(event => {
return this.subscribe(serverless, fnName, event.sns, shell.pwd());
})));
});
shell.cd("../");
}
);
});
shell.cd("../");
}
} else {
const subscriptions = this.getResourceSubscriptions(this.serverless);
subscriptions.forEach((subscription) =>
subscribePromises.push(this.subscribeFromResource(subscription, this.location))
const subscriptions = this.getResourceSubscriptions(this.serverless);
subscriptions.forEach((subscription) =>
subscribePromises.push(
this.subscribeFromResource(subscription, this.location)
)
Object.keys(this.serverless.service.functions).map(fnName => {
const fn = this.serverless.service.functions[fnName];
subscribePromises.push(Promise.all(fn.events.filter(event => event.sns != null).map(event => {
return this.subscribe(this.serverless, fnName, event.sns, this.location);
})));
});
);
Object.keys(this.serverless.service.functions).map((fnName) => {
const fn = this.serverless.service.functions[fnName];
subscribePromises.push(
Promise.all(
fn.events
.filter((event) => event.sns != null)
.map((event) => {
return this.subscribe(
this.serverless,
fnName,
event.sns,
this.location
);
})
)
);
});
}
await Promise.all(subscribePromises);
this.debug("subscribing queues");
Expand Down Expand Up @@ -278,7 +304,7 @@ class ServerlessOfflineSns {
const fn = serverless.service.functions[fnName];

if (!fn.runtime) {
fn.runtime = serverless.service.provider.runtime;
fn.runtime = serverless.service.provider.runtime;
}

let topicName = "";
Expand Down Expand Up @@ -308,7 +334,12 @@ class ServerlessOfflineSns {
this.log(`Creating topic: "${topicName}" for fn "${fnName}"`);
const data = await this.snsAdapter.createTopic(topicName);
this.debug("topic: " + JSON.stringify(data));
await this.snsAdapter.subscribe(fn, this.createHandler(fnName, fn, lambdasLocation), data.TopicArn, snsConfig);
await this.snsAdapter.subscribe(
fn,
this.createHandler(fnName, fn, lambdasLocation),
data.TopicArn,
snsConfig
);
}

public async subscribeQueue(queueUrl, snsConfig) {
Expand Down Expand Up @@ -345,9 +376,9 @@ class ServerlessOfflineSns {

public createHandler(fnName, fn, location) {
if (!fn.runtime || fn.runtime.startsWith("nodejs")) {
return this.createJavascriptHandler(fn, location);
return this.createJavascriptHandler(fn, location);
} else {
return () => this.createProxyHandler(fnName, fn, location);
return () => this.createProxyHandler(fnName, fn, location);
}
}

Expand Down Expand Up @@ -482,7 +513,7 @@ class ServerlessOfflineSns {
const handlerFnNameIndex = fn.handler.lastIndexOf(".");
const handlerPath = fn.handler.substring(0, handlerFnNameIndex);
const handlerFnName = fn.handler.substring(handlerFnNameIndex + 1);
const fullHandlerPath = resolve(location, handlerPath)
const fullHandlerPath = resolve(location, handlerPath);
this.debug("require(" + fullHandlerPath + ")[" + handlerFnName + "]");
const handler = require(fullHandlerPath)[handlerFnName];
return handler;
Expand Down Expand Up @@ -547,4 +578,4 @@ class ServerlessOfflineSns {
}
}

module.exports = ServerlessOfflineSns;
module.exports = ServerlessOfflineSns;
20 changes: 9 additions & 11 deletions src/sls-config-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ class ConfigServerless extends Serverless {

this.pluginManager.validateCommand(this.processedInput.commands);

return this.variables
.populateService()
.then(() => {
this.service.mergeResourceArrays();
this.service.setFunctionNames(this.processedInput.options);
this.service.validate();
});
return this.variables.populateService().then(() => {
this.service.mergeResourceArrays();
this.service.setFunctionNames(this.processedInput.options);
this.service.validate();
});
}
}

const normalizeResources = config => {
const normalizeResources = (config) => {
if (!config.resources) {
return config.resources;
}
Expand All @@ -53,7 +51,7 @@ const normalizeResources = config => {
...Outputs,
},
}),
{},
{}
);

return {
Expand All @@ -62,7 +60,7 @@ const normalizeResources = config => {
};

export async function loadServerlessConfig(cwd = process.cwd(), debug) {
console.log("debug loadServerlessConfig", cwd)
console.log("debug loadServerlessConfig", cwd);
const stat = fs.statSync(cwd);
if (!stat.isDirectory()) {
cwd = path.dirname(cwd);
Expand All @@ -81,7 +79,7 @@ export async function loadServerlessConfig(cwd = process.cwd(), debug) {
},
resources: normalizeResources(config),
};

console.log("output");

return output;
Expand Down
2 changes: 1 addition & 1 deletion src/sns-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,4 @@ export class SNSAdapter implements ISNSAdapter {
clientContext: {},
};
}
}
}
2 changes: 1 addition & 1 deletion src/sns-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,4 @@ export class SNSServer implements ISNSServer {
}
this.pluginDebug(msg, "server");
}
}
}

0 comments on commit 9bd7b07

Please sign in to comment.