From f0ee2a7f52795732f3caf8fb1b57800379e4e9aa Mon Sep 17 00:00:00 2001 From: psoubrie Date: Thu, 25 Jan 2024 11:21:25 +0000 Subject: [PATCH] deploy update and canonical --- deploy.js | 88 ------------------------------------------- deploy.mjs | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 5 ++- src/index.html | 1 + 4 files changed, 105 insertions(+), 89 deletions(-) delete mode 100644 deploy.js create mode 100644 deploy.mjs diff --git a/deploy.js b/deploy.js deleted file mode 100644 index 355fdfe..0000000 --- a/deploy.js +++ /dev/null @@ -1,88 +0,0 @@ -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)); -}); diff --git a/deploy.mjs b/deploy.mjs new file mode 100644 index 0000000..ee0c82d --- /dev/null +++ b/deploy.mjs @@ -0,0 +1,100 @@ +/* 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)); +}); diff --git a/package.json b/package.json index 1983aa2..6c32e2c 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "devDependencies": { + "@types/dotenv": "^8.2.0", "parcel": "^2.10.2", "postcss": "^8.4.31", "prettier": "^3.1.0", @@ -7,6 +8,8 @@ "tailwindcss": "^3.3.5" }, "dependencies": { - "flowbite": "^2.2.1" + "flowbite": "^2.2.1", + "fs-extra": "^11.2.0", + "webdav": "^5.3.1" } } diff --git a/src/index.html b/src/index.html index 58e56bc..d52647c 100644 --- a/src/index.html +++ b/src/index.html @@ -10,6 +10,7 @@ rel="stylesheet" /> +