UNPKG

@rnaga/wp-node

Version:

👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**

161 lines (160 loc) • 6.24 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.writeDFile = exports.writeFile = exports.readJsonFiles = exports.readJsonFile = exports.updateEnvFile = exports.copyFile = exports.readFile = exports.fileExists = exports.mkdir = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const mkdir = (directory) => { // Ensure the directory exists if (!fs.existsSync(directory)) { fs.mkdirSync(directory, { recursive: true }); } }; exports.mkdir = mkdir; const fileExists = (directory) => { return fs.existsSync(directory); }; exports.fileExists = fileExists; const readFile = (filePath) => { return !(0, exports.fileExists)(filePath) ? undefined : fs.readFileSync(filePath, "utf8"); }; exports.readFile = readFile; const copyFile = (sourcePath, destinationPath, options) => { if (options?.recursive) { fs.cpSync(sourcePath, destinationPath, options); } else { fs.copyFileSync(sourcePath, destinationPath); } return true; }; exports.copyFile = copyFile; const updateEnvFile = (envs, options) => { const { environment, distDir = "." } = options; const envPath = `${distDir}/.env${environment ? `.${environment}` : ""}`; try { if (!fs.existsSync(envPath)) { fs.writeFileSync(envPath, "", "utf-8"); } // Read the current env file content const currentContent = fs.readFileSync(envPath, "utf-8"); const lines = currentContent.split("\n"); const existingKeys = new Set(); // Build a map of existing keys lines.forEach((line) => { const match = line.match(/^\s*([^#][^=]*?)\s*=/); if (match) { existingKeys.add(match[1].trim()); } }); let updatedContent = currentContent; // Loop through new envs and update the file content for (const [key, value] of Object.entries(envs)) { const envString = `${key}=${value}`; if (existingKeys.has(key)) { // Comment out existing key updatedContent = updatedContent.replace(new RegExp(`^\\s*(${key}\\s*=\\s*[^\\n]*)`, "gm"), `# $1`); } // Add new key-value pair updatedContent += `\n${envString}`; } // Write the updated content back to the file fs.writeFileSync(envPath, updatedContent, "utf-8"); console.log(`Environment variables in ${envPath} updated successfully.`); } catch (err) { console.error("Failed to update environment variables:", err); } }; exports.updateEnvFile = updateEnvFile; const readJsonFile = (filePath) => { try { const data = fs.readFileSync(filePath, "utf8"); const json = JSON.parse(data); return json; } catch (e) { return undefined; } }; exports.readJsonFile = readJsonFile; const readJsonFiles = (directoryPath) => { let mergedData = undefined; // Read directory and process each file try { fs.readdirSync(directoryPath).forEach((file) => { if (path.extname(file) === ".json") { // Construct full file path const filePath = path.join(directoryPath, file); // Read and parse JSON file try { // const data = fs.readFileSync(filePath, "utf8"); // const json = JSON.parse(data); const json = (0, exports.readJsonFile)(filePath); // Merge jsonData into mergedData mergedData = { ...mergedData, ...json }; } catch (error) { console.info(`Error reading or parsing ${file}:`, error); } } }); } catch (e) { console.info(`No JSON files found in ${directoryPath}`); } return mergedData; }; exports.readJsonFiles = readJsonFiles; const writeFile = (filePath, content) => { fs.writeFileSync(filePath, content); console.log(`${filePath} created successfully.`); }; exports.writeFile = writeFile; // Write ts declaration file const writeDFile = (directoryPrefix, filename, content) => { fs.writeFileSync(`${directoryPrefix}${filename}`, `export {}\n${content}`); // Update index.d.ts if the code `export ${filename}` is not present in index.d.ts const indexFile = `${directoryPrefix}/index.d.ts`; const targetExport = `export * from "./${filename.slice(0, -5)}";`; const indexContent = fs.existsSync(indexFile) ? fs.readFileSync(indexFile, "utf-8") : ""; if (!indexContent.includes(targetExport)) { fs.writeFileSync(indexFile, `${indexContent}\n${targetExport}`); } console.log(`${directoryPrefix}${filename} created successfully.`); }; exports.writeDFile = writeDFile;