|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { parseArgs } from "node:util"; |
| 4 | +import { readFile } from "node:fs/promises"; |
| 5 | +import path from "node:path"; |
| 6 | +import { pathToFileURL } from "node:url"; |
| 7 | +import { run, type ILambdaFunction } from "./standalone" with { external: "true" }; |
| 8 | +import { log } from "./lib/utils/colorize"; |
| 9 | + |
| 10 | +function printHelpAndExit() { |
| 11 | + log.setDebug(true); |
| 12 | + log.GREY("Usage example:"); |
| 13 | + |
| 14 | + console.log(`aws-lambda -p 3000 --debug --functions "src/lambdas/**/*.ts"\n`); |
| 15 | + |
| 16 | + log.BR_BLUE("Options:"); |
| 17 | + |
| 18 | + for (const [optionName, value] of Object.entries(options)) { |
| 19 | + let printableName = optionName; |
| 20 | + |
| 21 | + if (value.short) { |
| 22 | + printableName += `, -${value.short}`; |
| 23 | + } |
| 24 | + |
| 25 | + let content = `\t\ttype: ${value.type}`; |
| 26 | + if (value.description) { |
| 27 | + content += `\n\t\tdescription: ${value.description}`; |
| 28 | + } |
| 29 | + |
| 30 | + if ("default" in value) { |
| 31 | + content += `\n\t\tdefault: ${value.default}`; |
| 32 | + } |
| 33 | + if (value.example) { |
| 34 | + content += `\n\t\texample: ${value.example}`; |
| 35 | + } |
| 36 | + |
| 37 | + content += "\n"; |
| 38 | + |
| 39 | + log.CYAN(`\t --${printableName}`); |
| 40 | + log.GREY(content); |
| 41 | + } |
| 42 | + |
| 43 | + process.exit(0); |
| 44 | +} |
| 45 | + |
| 46 | +function getNumberOrDefault(value: any, defaultValue: number) { |
| 47 | + if (!value || isNaN(value)) { |
| 48 | + return defaultValue; |
| 49 | + } |
| 50 | + |
| 51 | + return Number(value); |
| 52 | +} |
| 53 | + |
| 54 | +async function getFunctionsDefinitionFromFile(filePath?: string) { |
| 55 | + if (!filePath) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (filePath.endsWith(".js")) { |
| 60 | + throw new Error("Only .json, .mjs and .cjs are supported for --definitions option."); |
| 61 | + } |
| 62 | + |
| 63 | + if (filePath.endsWith(".json")) { |
| 64 | + const defs = JSON.parse(await readFile(filePath, "utf-8")); |
| 65 | + return defs.functions; |
| 66 | + } |
| 67 | + |
| 68 | + if (filePath.endsWith(".mjs") || filePath.endsWith(".cjs")) { |
| 69 | + const modulePath = pathToFileURL(path.resolve(process.cwd(), filePath)).href; |
| 70 | + const mod = await import(modulePath); |
| 71 | + |
| 72 | + return mod.functions; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async function getFromGlob(excludePattern: RegExp, handlerName: string, matchPattern?: string[]) { |
| 77 | + if (!matchPattern) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const majorNodeVersion = Number(process.versions.node.slice(0, process.versions.node.indexOf("."))); |
| 82 | + |
| 83 | + if (majorNodeVersion < 22) { |
| 84 | + throw new Error("--functions option is only supported on Node22 and higher."); |
| 85 | + } |
| 86 | + |
| 87 | + const { glob } = await import("node:fs/promises"); |
| 88 | + |
| 89 | + const handlers: Map<string, ILambdaFunction> = new Map(); |
| 90 | + |
| 91 | + for await (const entry of glob(matchPattern)) { |
| 92 | + if (entry.match(excludePattern)) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + const parent = path.basename(path.dirname(entry)); |
| 97 | + const parsedPath = path.parse(entry); |
| 98 | + |
| 99 | + let funcName: string; |
| 100 | + |
| 101 | + if (parsedPath.name == "index") { |
| 102 | + if (!handlers.has(parent)) { |
| 103 | + funcName = parent; |
| 104 | + } else { |
| 105 | + funcName = entry.replaceAll(path.sep, "_"); |
| 106 | + } |
| 107 | + } else { |
| 108 | + if (!handlers.has(parsedPath.name)) { |
| 109 | + funcName = parsedPath.name; |
| 110 | + } else if (!handlers.has(`${parent}_${parsedPath.name}`)) { |
| 111 | + funcName = `${parent}_${parsedPath.name}`; |
| 112 | + } else { |
| 113 | + funcName = entry.replaceAll(path.sep, "_"); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + handlers.set(funcName, { |
| 118 | + name: funcName, |
| 119 | + // @ts-ignore |
| 120 | + handler: entry.replace(parsedPath.ext, `.${handlerName}`), |
| 121 | + // @ts-ignore |
| 122 | + runtime: parsedPath.ext == ".py" ? "python3.7" : parsedPath.ext == ".rb" ? "ruby2.7" : `nodejs${majorNodeVersion}.x`, |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + return Array.from(handlers.values()); |
| 127 | +} |
| 128 | + |
| 129 | +function getDefaultEnvs(env: string[]) { |
| 130 | + const environment: Record<string, string> = {}; |
| 131 | + |
| 132 | + for (const s of env) { |
| 133 | + const [key, ...rawValue] = s.split("="); |
| 134 | + |
| 135 | + environment[key] = rawValue.join("="); |
| 136 | + } |
| 137 | + |
| 138 | + return environment; |
| 139 | +} |
| 140 | + |
| 141 | +interface ICliOptions { |
| 142 | + type: "string" | "boolean"; |
| 143 | + multiple?: boolean | undefined; |
| 144 | + short?: string | undefined; |
| 145 | + default?: string | boolean | string[] | boolean[] | undefined; |
| 146 | + description?: string; |
| 147 | + example?: string; |
| 148 | +} |
| 149 | + |
| 150 | +const options: Record<string, ICliOptions> = { |
| 151 | + port: { type: "string", short: "p", default: "0", description: "Set server port." }, |
| 152 | + debug: { type: "boolean", default: false, description: "Enable debug mode. When enabled aws-lambda will print usefull informations." }, |
| 153 | + config: { type: "string", short: "c", description: "Path to 'defineConfig' file." }, |
| 154 | + runtime: { type: "string", short: "r", description: "Set default runtime (ex: nodejs22.x, python3.7, ruby2.7 etc.)." }, |
| 155 | + timeout: { type: "string", short: "t", default: "3", description: "Set default timeout." }, |
| 156 | + definitions: { type: "string", short: "d", description: "Path to .json, .mjs, .cjs file with Lambda function definitions." }, |
| 157 | + functions: { type: "string", short: "f", multiple: true, description: "Glob pattern to automatically find and define Lambda handlers." }, |
| 158 | + exclude: { type: "string", short: "x", default: "\.(test|spec)\.", description: "RegExp string to exclude found enteries from --functions." }, |
| 159 | + handlerName: { type: "string", default: "handler", description: "Handler function name. To be used with --functions." }, |
| 160 | + env: { |
| 161 | + type: "string", |
| 162 | + short: "e", |
| 163 | + multiple: true, |
| 164 | + default: [], |
| 165 | + description: "Environment variables to be injected into Lambdas. All existing AWS_* are automatically injected.", |
| 166 | + example: "-e API_KEY=supersecret -e API_URL=https://website.com", |
| 167 | + }, |
| 168 | + help: { type: "boolean", short: "h" }, |
| 169 | +}; |
| 170 | + |
| 171 | +const { values } = parseArgs({ |
| 172 | + strict: false as true, |
| 173 | + options, |
| 174 | +}); |
| 175 | + |
| 176 | +const { port, config, debug, help, runtime, definitions, timeout, functions, handlerName, exclude, env } = values; |
| 177 | + |
| 178 | +if (help) { |
| 179 | + printHelpAndExit(); |
| 180 | +} |
| 181 | + |
| 182 | +if (definitions && functions) { |
| 183 | + throw new Error("Can not use --definitions (-d) and --functions (-f) together."); |
| 184 | +} |
| 185 | + |
| 186 | +// @ts-ignore |
| 187 | +const functionDefs = functions ? await getFromGlob(new RegExp(exclude), handlerName, functions as string[]) : await getFunctionsDefinitionFromFile(definitions as string); |
| 188 | + |
| 189 | +run({ |
| 190 | + // @ts-ignore |
| 191 | + debug, |
| 192 | + // @ts-ignore |
| 193 | + configPath: config, |
| 194 | + port: getNumberOrDefault(port, 0), |
| 195 | + functions: functionDefs, |
| 196 | + defaults: { |
| 197 | + // @ts-ignore |
| 198 | + environment: getDefaultEnvs(env), |
| 199 | + // @ts-ignore |
| 200 | + runtime, |
| 201 | + timeout: getNumberOrDefault(timeout, 3), |
| 202 | + }, |
| 203 | +}); |
0 commit comments