flupke_website/deploy.js

89 lines
2.6 KiB
JavaScript

require("dotenv").config();
const fs = require("fs-extra");
const { exec } = require("child_process");
import("webdav").then(({ createClient }) => {
// Step 1: Clean the dist/ folder
function cleanDist() {
console.log("Cleaning dist/ folder...");
return fs
.emptyDir("./dist")
.then(() => console.log("dist/ folder cleaned"))
.catch((err) => console.error("Error cleaning dist/ folder:", err));
}
// Step 2: Build with Parcel
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 3: 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 4: Clean main folder on server
async function cleanMainFolder(client) {
console.log("Cleaning main folder on server...");
try {
await client.deleteFile(process.env.WEBDAV_PATH).catch(() => {}); // Ignore error if folder doesn't exist
await client.createDirectory(process.env.WEBDAV_PATH);
console.log("main folder on server cleaned");
} catch (err) {
console.error("Error cleaning main folder on server:", err);
throw err;
}
}
// Step 5: Transfer content
async function transferContent(client) {
console.log("Transferring content to server...");
try {
const files = await fs.readdir("./dist");
for (const file of files) {
await client.putFileContents(
process.env.WEBDAV_PATH.concat(file),
await fs.readFile(`./dist/${file}`)
);
console.log(`Transferred ${file}`);
}
} catch (err) {
console.error("Error transferring content:", err);
throw err;
}
}
// Start the process
cleanDist()
.then(buildWithParcel)
.then(loginToWebDAV)
// .then((client) => cleanMainFolder(client).then(() => client)) WILL DELETE OTHER SUBFOLDERS
.then((client) => transferContent(client))
.then(cleanDist)
.then(() => console.log("Process completed successfully"))
.catch((err) => console.error("Process failed:", err));
});