Initial commit

This commit is contained in:
Diego Marcos
2025-05-23 08:52:14 -07:00
committed by Diego Marcos Segura
commit 3332f609a9
129 changed files with 64575 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
import fs from "node:fs";
import { copyFile, mkdir, rename } from "node:fs/promises";
import path from "node:path";
function copyDir(src, dest) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
// Example usage:
const sourceDir = "examples";
const destinationDir = "site/examples";
copyDir("examples", "site/examples");
await copyFile("index.html", "site/examples/index.html");
await rename("site/examples/viewer", "site/viewer");
await mkdir("site/examples/vendor", { recursive: true });
copyDir("node_modules/three", "site/examples/js/vendor/three");
copyDir("dist", "site/dist");
console.log("Site generated in site directory.");
+8
View File
@@ -0,0 +1,8 @@
import { existsSync } from "node:fs";
import { rm } from "node:fs/promises";
const assetsDirectory = "examples/assets/";
if (existsSync(assetsDirectory)) {
await rm(assetsDirectory, { recursive: true, force: true });
console.log(`Directory ${assetsDirectory} deleted`);
}
+8
View File
@@ -0,0 +1,8 @@
import { existsSync } from "node:fs";
import { rm } from "node:fs/promises";
const siteDirectory = "site";
if (existsSync(siteDirectory)) {
await rm(siteDirectory, { recursive: true, force: true });
console.log(`Directory ${siteDirectory} deleted`);
}
+64
View File
@@ -0,0 +1,64 @@
import { createWriteStream, existsSync } from "node:fs";
import { mkdir, readFile, rm } from "node:fs/promises";
import * as http from "node:http";
import * as https from "node:https";
import { get } from "node:https";
import { basename, join } from "node:path";
import { URL } from "node:url";
const assetsDirectory = "examples/assets/";
await mkdir(assetsDirectory, { recursive: true });
const urls = JSON.parse(await readFile("examples/assets.json", "utf8"));
let filesToDownload = 0;
for (const [key, data] of Object.entries(urls)) {
const url = data.url;
const directory = assetsDirectory + data.directory;
const filePath = join(directory, key);
await mkdir(directory, { recursive: true });
if (existsSync(filePath)) {
console.log(
`File: ${filePath} ' exists. Do not download again. npm run assets:clean to delete`,
);
continue;
}
downloadFile(url, key, filePath);
console.log(`Downloading file ${key}, from URL: ${url}`);
}
function downloadFile(fileUrl, fileName, filePath) {
const url = new URL(fileUrl);
const file = createWriteStream(filePath);
const protocol = url.protocol === "https:" ? https.get : http.get;
filesToDownload++;
protocol(fileUrl, (response) => {
if (response.statusCode !== 200) {
filesToDownload--;
console.error(
`Download failed: ${response.statusCode} ${response.statusMessage}`,
);
return;
}
response.pipe(file);
file.on("finish", () => {
file.close(() => {
filesToDownload--;
console.log(`${fileName} downloaded to ${filePath}`);
if (filesToDownload === 0) {
console.log("Success! All assets downloaded");
}
});
});
}).on("error", (err) => {
console.error(`Error: ${err.message}`);
});
}
+64
View File
@@ -0,0 +1,64 @@
import fs from "node:fs/promises";
import http from "node:http";
import path from "node:path";
import { fileURLToPath } from "node:url";
// Resolve __dirname in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Get directory to serve
const root = path.resolve(process.argv[2] || ".");
const mimeTypes = {
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpeg",
".svg": "image/svg+xml",
};
const server = http.createServer(async (req, res) => {
try {
const requestedPath = decodeURIComponent(req.url.split("?")[0]);
const fullPath = path.join(root, requestedPath);
const safePath = path.normalize(fullPath);
if (!safePath.startsWith(root)) {
res.writeHead(403);
return res.end("Access denied");
}
const stats = await fs.stat(safePath);
if (stats.isDirectory()) {
const indexPath = path.join(safePath, "index.html");
try {
const content = await fs.readFile(indexPath);
res.writeHead(200, { "Content-Type": "text/html" });
return res.end(content);
} catch {
res.writeHead(403);
return res.end("Directory listing is disabled");
}
} else {
const ext = path.extname(safePath).toLowerCase();
const contentType = mimeTypes[ext] || "application/octet-stream";
const content = await fs.readFile(safePath);
res.writeHead(200, { "Content-Type": contentType });
res.end(content);
}
} catch (err) {
res.writeHead(err.code === "ENOENT" ? 404 : 500);
res.end(
err.code === "ENOENT" ? "Not Found" : `Server Error: ${err.message}`,
);
}
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Serving ${root} at http://localhost:${PORT}`);
});