Files
vercel/test/lib/run-build-lambda.js
Nathan Rajlich a7a5bf1a12 [tests] Pass in builder to runBuildLambda() (#9262)
Removes the need for `next`/`static-build` to be present in the root `package.json` file.
2023-01-19 15:10:06 -08:00

60 lines
1.5 KiB
JavaScript

const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const json5 = require('json5');
const { glob } = require('@vercel/build-utils');
exports.createRunBuildLambda = function (builder) {
return async inputPath => {
const inputFiles = await glob('**', inputPath);
const nowJsonRef = inputFiles['vercel.json'] || inputFiles['now.json'];
if (typeof expect !== 'undefined') {
expect(nowJsonRef).toBeDefined();
}
const nowJson = json5.parse(await fs.readFile(nowJsonRef.fsPath, 'utf8'));
const build = nowJson.builds[0];
if (typeof expect !== 'undefined') {
expect(build.src.includes('*')).toBeFalsy();
}
const entrypoint = build.src.replace(/^\//, ''); // strip leftmost slash
if (typeof expect !== 'undefined') {
expect(inputFiles[entrypoint]).toBeDefined();
}
let workPath = path.join(
os.tmpdir(),
`vercel-${Date.now()}-${Math.floor(Math.random() * 100)}`
);
await fs.ensureDir(workPath);
workPath = await fs.realpath(workPath);
console.log('building in', workPath);
const buildResult = await builder.build({
files: inputFiles,
entrypoint,
config: build.config,
workPath,
});
const { output } = buildResult;
// Windows support
if (output) {
buildResult.output = Object.keys(output).reduce(
(result, path) => ({
...result,
[path.replace(/\\/g, '/')]: output[path],
}),
{}
);
}
return {
buildResult,
workPath,
};
};
};