Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.build = async ({
files, entrypoint, workPath, config, meta,
}) => {
// Download all files to workPath
const downloadedFiles = await download(files, workPath, meta);
console.log('Config:', config);
let includedFiles = {};
if (config && config.includeFiles) {
// Find files for each glob
// eslint-disable-next-line no-restricted-syntax
for (const pattern of config.includeFiles) {
// eslint-disable-next-line no-await-in-loop
const matchedFiles = await glob(pattern, workPath);
Object.assign(includedFiles, matchedFiles);
}
// explicit and always include the entrypoint
Object.assign(includedFiles, {
[entrypoint]: files[entrypoint],
});
Learn more: https://github.com/golang/go/wiki/Modules
`);
}
const entrypointArr = entrypoint.split(sep);
// eslint-disable-next-line prefer-const
let [goPath, outDir] = await Promise.all([
getWriteableDirectory(),
getWriteableDirectory(),
]);
const srcPath = join(goPath, 'src', 'lambda');
let downloadedFiles;
if (meta.isDev) {
downloadedFiles = await download(files, workPath, meta);
} else {
downloadedFiles = await download(files, srcPath);
}
debug(`Parsing AST for "${entrypoint}"`);
let analyzed: string;
try {
let goModAbsPathDir = '';
for (const file of Object.keys(downloadedFiles)) {
if (file === 'go.mod') {
goModAbsPathDir = dirname(downloadedFiles[file].fsPath);
}
}
analyzed = await getAnalyzedEntrypoint(
downloadedFiles[entrypoint].fsPath,
goModAbsPathDir
base = workPath;
} else {
base = dirname(downloadedFiles['now.json'].fsPath);
}
const destNow = join(
base,
'.now',
'cache',
basename(entrypoint, '.go'),
'src',
'lambda'
);
// this will ensure Go rebuilt fast
goPath = join(base, '.now', 'cache', basename(entrypoint, '.go'));
await download(downloadedFiles, destNow);
downloadedFiles = await glob('**', destNow);
}
// find `go.mod` in downloadedFiles
const entrypointDirname = dirname(downloadedFiles[entrypoint].fsPath);
let isGoModExist = false;
let goModPath = '';
let isGoModInRootDir = false;
for (const file of Object.keys(downloadedFiles)) {
const fileDirname = dirname(downloadedFiles[file].fsPath);
if (file === 'go.mod') {
isGoModExist = true;
isGoModInRootDir = true;
goModPath = fileDirname;
} else if (file.endsWith('go.mod') && !file.endsWith('vendor')) {
exports.build = async ({ workPath, files, entrypoint, meta, config }) => {
const outDir = await getWritableDirectory();
await download(files, workPath, meta);
const handlerPath = path.join(__dirname, 'handler');
await copyFile(handlerPath, path.join(outDir, 'handler'));
const entrypointOutDir = path.join(outDir, path.dirname(entrypoint));
await mkdirp(entrypointOutDir);
// For now only the entrypoint file is copied into the lambda
await copyFile(
path.join(workPath, entrypoint),
path.join(outDir, entrypoint)
);
const lambda = await createLambda({
files: await glob('**', outDir),
handler: 'handler',
async function getIncludedFiles({ files, entrypoint, workPath, config, meta }) {
// Download all files to workPath
const downloadedFiles = await download(files, workPath, meta);
let includedFiles = {};
if (config && config.includeFiles) {
// Find files for each glob
for (const pattern of config.includeFiles) {
const matchedFiles = await glob(pattern, workPath);
Object.assign(includedFiles, matchedFiles);
}
// explicit and always include the entrypoint
Object.assign(includedFiles, {
[entrypoint]: files[entrypoint],
});
} else {
// Backwards compatibility
includedFiles = downloadedFiles;
}
export async function downloadFilesInWorkPath({
entrypoint,
workPath,
files,
meta,
config,
}: BuildOptions) {
debug('Downloading user files...');
let downloadedFiles = await download(files, workPath, meta);
if (meta && meta.isDev) {
let base = null;
if (config && config.zeroConfig) {
base = workPath;
} else {
base = dirname(downloadedFiles['now.json'].fsPath);
}
const destNow = join(base, '.now', 'cache', basename(entrypoint, '.py'));
await download(downloadedFiles, destNow);
downloadedFiles = await glob('**', destNow);
workPath = destNow;
}
return workPath;
}