Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const path = require('path');
const { readFileSync } = require('fs');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const { yamlParse } = require('yaml-cfn');
const conf = {
prodMode: process.env.NODE_ENV === 'production',
templatePath: './template.yaml',
};
const cfn = yamlParse(readFileSync(conf.templatePath));
const entries = Object.values(cfn.Resources)
// Find nodejs functions
.filter(v => v.Type === 'AWS::Serverless::Function')
.filter(v =>
(v.Properties.Runtime && v.Properties.Runtime.startsWith('nodejs')) ||
(!v.Properties.Runtime && cfn.Globals.Function.Runtime)
)
.map(v => ({
// Isolate handler src filename
handlerFile: v.Properties.Handler.split('.')[0],
// Build handler dst path
CodeUriDir: v.Properties.CodeUri.split('/').splice(2).join('/')
}))
.reduce(
(entries, v) =>
Object.assign(
private entryFor(projectKey: string, projectTemplate: string) {
const samConfig = yamlParse(fs.readFileSync(projectTemplate).toString());
const defaultRuntime = samConfig.Globals?.Function?.Runtime ?? null;
const defaultHandler = samConfig.Globals?.Function?.Handler ?? null;
const defaultCodeUri = samConfig.Globals?.Function?.CodeUri ?? null;
// Loop through all of the resources
for (const resourceKey in samConfig.Resources) {
const resource = samConfig.Resources[resourceKey];
// Find all of the functions
if (resource.Type === "AWS::Serverless::Function") {
const properties = resource.Properties;
if (!properties) {
throw new Error(`${resourceKey} is missing Properties`);
}
// Check the runtime is supported
compiler.hooks.afterEmit.tap("SamPlugin", (compilation: any) => {
if (this.samConfigs && this.launchConfig) {
for (const samConfig of this.samConfigs) {
fs.writeFileSync(`${samConfig.buildRoot}/template.yaml`, yamlDump(samConfig.samConfig));
}
if (this.options.vscodeDebug) {
if (!fs.existsSync(".vscode")) {
fs.mkdirSync(".vscode");
}
fs.writeFileSync(".vscode/launch.json", JSON.stringify(this.launchConfig));
}
} else {
console.log("It looks like SamPlugin.entry() was not called");
}
});
}
.catch(() => yamlParse(data))
)
function parseTemplate(text: string, ext: string): any {
try { return JSON.parse(text); } catch (e1) {
if (ext === "json") {
throw new Error(`Unable to parse JSON template: ${e1}`);
}
try { return yamlParse(text); } catch (e2) {
throw new Error(`Unable to parse YAML template: ${e2}`);
}
}
}