UNPKG

@vechain/vebetterdao-contracts

Version:

Open-source repository that houses the smart contracts powering the decentralized VeBetterDAO on the VeChain Thor blockchain.

118 lines (117 loc) 5.04 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readFilesFromDirectory = readFilesFromDirectory; exports.formData = formData; exports.getFolderName = getFolderName; exports.zipFolder = zipFolder; exports.copyImages = copyImages; exports.saveContractsToFile = saveContractsToFile; exports.saveLibrariesToFile = saveLibrariesToFile; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const form_data_1 = __importDefault(require("form-data")); const archiver_1 = __importDefault(require("archiver")); const mime_1 = require("./utils/mime"); /** * Reads files from a directory and returns an array of `File` objects. * * @param dirPath - The path to the directory to read. * @returns A promise that resolves to an array of `File` objects. * * @throws An error if the directory does not exist. */ async function readFilesFromDirectory(dirPath) { const entries = await fs_1.default.promises.readdir(dirPath, { withFileTypes: true }); const files = []; for (const entry of entries) { if (entry.isFile()) { const fileName = entry.name; const filePath = path_1.default.join(dirPath, fileName); const content = await fs_1.default.promises.readFile(filePath); const mimeType = (0, mime_1.getMimeType)(fileName); const file = new File([content], fileName, { type: mimeType }); files.push(file); } } return files; } function formData(path) { // Create a form data instance const form = new form_data_1.default(); form.append("file", fs_1.default.createReadStream(path)); return form; } function getFolderName(folderPath) { return path_1.default.basename(folderPath); } function copyImages(srcFolder, destFolder) { // Ensure the destination folder exists if (!fs_1.default.existsSync(destFolder)) { fs_1.default.mkdirSync(destFolder, { recursive: true }); } // Read all files in the source folder const files = fs_1.default.readdirSync(srcFolder); // Copy each image file from the source folder to the destination folder files.forEach(file => { const srcFilePath = path_1.default.join(srcFolder, file); const destFilePath = path_1.default.join(destFolder, file); // Check if the file is an image (you can extend this to check for specific image types) if (file.match(/\.(jpg|jpeg|png|gif)$/i)) { fs_1.default.copyFileSync(srcFilePath, destFilePath); } }); return destFolder; } async function zipFolder(sourceDir, outPath) { // Ensure that the stream truncates (overwrites) the file if it already exists const stream = fs_1.default.createWriteStream(outPath, { flags: "w" }); const archive = (0, archiver_1.default)("zip", { zlib: { level: 9 } }); return new Promise((resolve, reject) => { archive .directory(sourceDir, path_1.default.basename(sourceDir)) // Keeps the root folder .on("error", err => reject(new Error(`Error archiving the folder: ${err}`))) .pipe(stream); stream.on("close", () => resolve()); archive.finalize(); }); } /** * Save the deployed contracts addresses to a file. * @param contracts - The deployed contracts * @param libraries - The deployed libraries */ async function saveContractsToFile(contracts, libraries) { const OUTPUT_PATH = path_1.default.join(__dirname, `../../deploy_output`); // Reset the output directory if (fs_1.default.existsSync(OUTPUT_PATH)) { fs_1.default.rmSync(OUTPUT_PATH, { recursive: true }); } // Ensure the output directory exists fs_1.default.mkdirSync(OUTPUT_PATH); await fs_1.default.promises.writeFile(`${OUTPUT_PATH}/contracts.txt`, JSON.stringify(contracts, null, 2)); await fs_1.default.promises.writeFile(`${OUTPUT_PATH}/libraries.txt`, JSON.stringify(libraries, null, 2)); console.log(`Contracts and libraries addresses saved to ${OUTPUT_PATH}`); } /** * Save new libraries deployed to a file * @param contracts - The deployed contracts * @param libraries - The deployed libraries */ async function saveLibrariesToFile(libraries) { const OUTPUT_PATH = path_1.default.join(__dirname, `../../deploy_output`); const LIBRARY_FILE_PATH = path_1.default.join(OUTPUT_PATH, "libraries.txt"); // Ensure the output directory exists if (!fs_1.default.existsSync(OUTPUT_PATH)) { fs_1.default.mkdirSync(OUTPUT_PATH); } // Remove the existing libraries file if it exists if (fs_1.default.existsSync(LIBRARY_FILE_PATH)) { fs_1.default.unlinkSync(LIBRARY_FILE_PATH); } // Write the new libraries file await fs_1.default.promises.writeFile(LIBRARY_FILE_PATH, JSON.stringify(libraries, null, 2)); console.log(`Libraries addresses saved to ${LIBRARY_FILE_PATH}`); }