@faceteer/cdk
Version:
CDK 2.0 constructs and helpers that make composing a Lambda powered service easier.
126 lines (125 loc) • 4.87 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractHandlers = extractHandlers;
const fs = __importStar(require("fs"));
const pascal_case_1 = require("pascal-case");
const path = __importStar(require("path"));
const handler_1 = require("../handlers/handler");
const crypto = __importStar(require("crypto"));
function handlerName(handler) {
switch (handler.type) {
case handler_1.HandlerTypes.API:
return (0, pascal_case_1.pascalCase)(`Api ${handler.definition.name}`);
case handler_1.HandlerTypes.Queue:
return (0, pascal_case_1.pascalCase)(`Queue ${handler.definition.queueName}`);
case handler_1.HandlerTypes.Event:
return (0, pascal_case_1.pascalCase)(`Event ${handler.definition.name}`);
case handler_1.HandlerTypes.Cron:
return (0, pascal_case_1.pascalCase)(handler.definition.name);
case handler_1.HandlerTypes.Notification:
return (0, pascal_case_1.pascalCase)(`${handler.definition.topicName}-${handler.definition.name}`);
}
}
function makeHandlerDefinition({ handler, file, name, }) {
const { definition } = handler;
return {
...definition,
name,
path: file,
};
}
function extractHandlers(basePath) {
const files = getAllFiles(basePath);
// Sort the files to get consistent duplicate name handling
files.sort();
const handlers = {
api: {},
queue: {},
event: {},
notification: {},
cron: {},
};
for (const file of files) {
try {
const handler = require(file.replace(/\.ts$/g, '')).handler;
if (!handler) {
throw new Error(`${file} did not export a variable named 'handler'.`);
}
let name = handlerName(handler);
// In case of a collision, we'll add a portion of the file hash to the handler name.
const pathHash = crypto
.createHash('sha256')
.update(path.relative(basePath, file))
.digest('hex')
.slice(0, 6);
if (handlers[handler.type][name] !== undefined) {
name = `${name}${pathHash}`;
if (handlers[handler.type][name] !== undefined) {
// If they are still colliding with hash, we'll give up
throw new Error(`There are multiple function with the name ${handler.name}, please rename one of them.`);
}
}
const fullDefinition = makeHandlerDefinition({ handler, file, name });
handlers[handler.type][fullDefinition.name] = fullDefinition;
}
catch (error) {
console.error(`Failed to parse handler: ${file}`);
console.error(error);
throw error;
}
}
return handlers;
}
/**
* Get all of the files in a specified folder
* @param dirPath
* @param arrayOfFiles
*/
function getAllFiles(dirPath, arrayOfFiles = []) {
const files = fs.readdirSync(dirPath);
let filesArray = arrayOfFiles;
files.forEach((file) => {
if (fs.statSync(`${dirPath}/${file}`).isDirectory()) {
filesArray = getAllFiles(`${dirPath}/${file}`, arrayOfFiles);
}
else if (file.includes('.handler') &&
!file.includes('.test.') &&
file.endsWith('.ts')) {
filesArray.push(path.join(dirPath, '/', file));
}
});
return filesArray;
}