This repository was archived by the owner on Aug 3, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathhandler.js
More file actions
80 lines (80 loc) · 3.09 KB
/
Copy pathhandler.js
File metadata and controls
80 lines (80 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { S3 } from "@aws-sdk/client-s3";
import { Mutex } from "async-mutex";
import { promises as fs } from "fs";
import * as path from "path";
import { execSync } from 'child_process';
const s3 = new S3();
const buildArgs = {
directory: "/tmp/input",
verbose: true,
output: "/tmp/output",
serve: false,
fastRebuild: false,
baseDir: "",
port: 8080,
wsPort: 3001,
remoteDevHost: "",
bundleInfo: false,
concurrency: 4,
};
async function downloadFilesFromS3(bucketName, prefix, localDir) {
const listedObjects = await s3.listObjectsV2({ Bucket: bucketName, Prefix: prefix });
if (!listedObjects.Contents) {
console.log("No files found in S3 bucket.");
return;
}
await fs.mkdir(localDir, { recursive: true });
for (const obj of listedObjects.Contents) {
if (!obj.Key)
continue;
const filePath = path.join(localDir, obj.Key.replace(prefix, ""));
await fs.mkdir(path.dirname(filePath), { recursive: true });
const file = await s3.getObject({ Bucket: bucketName, Key: obj.Key });
if (file.Body) {
await fs.writeFile(filePath, await file.Body.transformToByteArray());
console.log(`Downloaded ${obj.Key} to ${filePath}`);
}
}
}
async function uploadFilesToS3(bucketName, localDir, prefix) {
const files = await fs.readdir(localDir, { withFileTypes: true });
for (const file of files) {
const filePath = path.join(localDir, file.name);
const s3Key = path.join(prefix, file.name);
const fileContent = await fs.readFile(filePath);
await s3.putObject({ Bucket: bucketName, Key: s3Key, Body: fileContent });
console.log(`Uploaded ${filePath} to s3://${bucketName}/${s3Key}`);
}
}
export const handler = async (event, context, callback) => {
try {
// Define paths
const NODE_MODULES_SOURCE = '/var/task/node_modules';
const NODE_MODULES_TMP = '/tmp/node_modules';
console.log('Copying node_modules to /tmp...');
execSync(`cp -r ${NODE_MODULES_SOURCE} ${NODE_MODULES_TMP}`);
const body = JSON.parse(event.body || "{}");
const { inputBucketName, folderPrefix, outputBucketName } = body;
if (!inputBucketName || !folderPrefix || !outputBucketName) {
return {
statusCode: 400,
body: JSON.stringify({ message: "Missing required parameters." }),
};
}
await downloadFilesFromS3(inputBucketName, folderPrefix, buildArgs.directory);
const buildQuartz = (await import("./transpiled-build.mjs")).default;
await buildQuartz(buildArgs, new Mutex());
await uploadFilesToS3(outputBucketName, buildArgs.output, "processed/");
return {
statusCode: 200,
body: JSON.stringify({ message: "Build completed and files uploaded successfully." }),
};
}
catch (error) {
console.error("Error during build:", error);
return {
statusCode: 500,
body: JSON.stringify({ message: "Build failed", error: error.message }),
};
}
};