UNPKG

ezyenv

Version:

Push `env` to github but using ezyenv which is tool to automatically generate sample env files from your existing `.env` files! making example env file, easy, intutive to share.

149 lines (147 loc) 5.44 kB
#!/usr/bin/env node "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // index.ts var index_exports = {}; __export(index_exports, { addScriptToPackageJSON: () => addScriptToPackageJSON, getPackageJSON: () => getPackageJSON, getPackageManager: () => getPackageManager }); module.exports = __toCommonJS(index_exports); var import_fs = require("fs"); var import_path = require("path"); var file = process.argv?.[2]; var sample = process.argv?.[3] ?? "example"; var envFiles = (0, import_fs.existsSync)(".") ? (0, import_fs.readdirSync)(".").filter( (file2) => /^(?:.*\.env|\.env(?:\..+)?)$/.test(file2) && !file2.toLowerCase().includes("example") && !file2.toLowerCase().includes("sample") && !file2.toLowerCase().includes(sample) ) : []; if (envFiles.length === 0 && !file) { console.log("No .env files found."); process.exit(1); } var generateExampleFile = (file2) => { try { const envPath = (0, import_path.resolve)(file2); if (!(0, import_fs.existsSync)(envPath)) { console.error(`Error: File '${file2}' does not exist`); return; } const content = (0, import_fs.readFileSync)(envPath, "utf-8"); const lines = content.split("\n"); const exampleContent = lines.map((line) => { if (line.trim().startsWith("#") || !line.includes("=")) return line; return line.split("=")[0] + "="; }).join("\n"); const examplePath = `${file2}.${sample}`; const fileName = examplePath.split("/").pop(); (0, import_fs.writeFileSync)(examplePath, exampleContent); const packageJSON = getPackageJSON(); if (packageJSON && !packageJSON.scripts.ezy) { const manager = getPackageManager(); switch (manager) { case "npm": addScriptToPackageJSON("ezy", "npx ezyenv"); break; case "yarn": addScriptToPackageJSON("ezy", "yarn ezyenv"); break; case "pnpm": addScriptToPackageJSON("ezy", "pnpm ezyenv"); break; case "bun": addScriptToPackageJSON("ezy", "bunx ezyenv"); break; default: console.log("ezyenv:\x1B[31m Script not added! \x1B[0m"); } } console.log(`ezyenv:\x1B[32m created ${fileName}\x1B[0m`); } catch (error) { console.error( `Error processing file '${file2}':`, error instanceof Error ? error.message : error ); } }; function getPackageJSON() { const packageJSONPath = (0, import_path.resolve)(process.cwd(), "package.json"); try { return JSON.parse((0, import_fs.readFileSync)(packageJSONPath, "utf-8")); } catch (error) { return void 0; } } function getPackageManager() { const projectDir = process.cwd(); if ((0, import_fs.existsSync)((0, import_path.join)(projectDir, "yarn.lock"))) { return "yarn"; } if ((0, import_fs.existsSync)((0, import_path.join)(projectDir, "pnpm-lock.yaml")) || (0, import_fs.existsSync)((0, import_path.join)(projectDir, "pnpm-workspace.yaml"))) { return "pnpm"; } if ((0, import_fs.existsSync)((0, import_path.join)(projectDir, "package-lock.json"))) { return "npm"; } if ((0, import_fs.existsSync)((0, import_path.join)(projectDir, "bun.lockb"))) { return "bun"; } return "unknown"; } function addScriptToPackageJSON(scriptName, scriptCommand) { const packageJSONPath = (0, import_path.resolve)(process.cwd(), "package.json"); try { const packageJSON = getPackageJSON(); if (!packageJSON) throw new Error("No package.json found"); packageJSON.scripts = packageJSON.scripts || {}; packageJSON.scripts[scriptName] = scriptCommand; (0, import_fs.writeFileSync)(packageJSONPath, JSON.stringify(packageJSON, null, 2)); console.log(`Script '${scriptName}' added!`); } catch (error) { console.error("Error adding script to package.json:", error); } } var removeExampleFiles = async () => { try { const files = (0, import_fs.readdirSync)(".").filter( (file2) => /^(?:.*\.env|\.env(?:\..+)?)$/.test(file2) && file2.toLowerCase().includes("example") || file2.toLowerCase().includes("sample") || file2.toLowerCase().includes("eg") || file2.toLowerCase().includes(sample) ); files.forEach((file2) => { (0, import_fs.unlinkSync)(file2); }); console.log("ezyenv: removed!"); process.exit(0); } catch (error) { console.error("Error removing example files:", error); } }; if (file === "rm") { removeExampleFiles(); } else if (file) { generateExampleFile(file); } else { envFiles.forEach(generateExampleFile); } process.exit(0); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { addScriptToPackageJSON, getPackageJSON, getPackageManager });