flupke_website/deploy.mjs

101 lines
2.9 KiB
JavaScript

/* eslint-disable */
// @ts-nocheck
import dotenv from "dotenv";
import fs from "fs-extra";
import { exec } from "child_process";
import path from "path";
// Initialize environment variables
dotenv.config();
// Dynamically import the webdav module
import("webdav").then(({ createClient }) => {
function buildWithParcel() {
console.log("Building with Parcel...");
const baseCommand = "npx parcel build src/index.html";
return new Promise((resolve, reject) => {
exec(baseCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
reject(error);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
reject(stderr);
return;
}
console.log(stdout);
resolve();
});
});
}
// Step 2: Clean local folders
function cleanDist() {
console.log("Cleaning local folders...");
return Promise.all([fs.emptyDir("./dist")])
.then(() => console.log(".next/, and out/ folders cleaned"))
.catch((err) => console.error("Error cleaning folders:", err));
}
// Step 4: Log into WebDAV
function loginToWebDAV() {
console.log("Logging into WebDAV server...");
const client = createClient(process.env.WEBDAV_SERVER_URL, {
username: process.env.WEBDAV_USERNAME,
password: process.env.WEBDAV_PASSWORD,
});
return client;
}
// Step 7: Upload directory to WebDAV server
async function uploadDirectory(client, localDir, remoteDir) {
try {
// Read the contents of the local directory
const files = fs.readdirSync(localDir);
for (const file of files) {
const localFilePath = path.join(localDir, file);
const remoteFilePath = path.join(remoteDir, file);
// Check if the file is a directory
if (fs.statSync(localFilePath).isDirectory()) {
// Create directory on WebDAV server
await client.createDirectory(remoteFilePath);
// Recursive call to upload the directory content
await uploadDirectory(client, localFilePath, remoteFilePath);
} else {
// Read file content
const fileContent = fs.readFileSync(localFilePath);
// Upload file to WebDAV server
await client.putFileContents(remoteFilePath, fileContent);
console.log(`Uploaded ${remoteFilePath}`);
}
}
} catch (error) {
console.error("Error uploading directory:", error);
}
}
// empty async function to use await
async function empty() {}
// Start the process
// empty()
// .then(...
cleanDist()
.then(buildWithParcel)
.then(loginToWebDAV)
//manual clean up
.then((client) => uploadDirectory(client, "./dist", process.env.WEBDAV_PATH))
.then(cleanDist)
.then(() => console.log("Process completed successfully"))
.catch((err) => console.error("Process failed:", err));
});