UNPKG

vercel-env-create

Version:

<div align="center"> <h1>Vercel Env Create 🛠️</h1> <p>The missing <code>vercel env create</code> command</p> <p> <a href="https://github.com/itsbrex/vercel-env-create" title="Screenshot of vercel-env-create"> <img alt="Screenshot of verce

130 lines (124 loc) 4.38 kB
#!/usr/bin/ env node 'use strict'; const fs = require('node:fs'); const path = require('node:path'); function _interopNamespaceDefault(e) { const n = Object.create(null); if (e) { for (const k in e) { n[k] = e[k]; } } n.default = e; return n; } const fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs); const path__namespace = /*#__PURE__*/_interopNamespaceDefault(path); const pattern = /process.env\.(\w+)/g; const suffixes = ["", ".example", ".local", ".development", ".production"]; const envVars = {}; for (const suffix of suffixes) { envVars[suffix] = /* @__PURE__ */ new Set(); } function searchDirectory(directory) { const excludeDirs = /* @__PURE__ */ new Set(["node_modules"]); const extensions = /* @__PURE__ */ new Set([".js", ".ts", ".jsx", ".tsx", ".html", ".css"]); for (const item of fs__namespace.readdirSync(directory)) { const itemPath = path__namespace.join(directory, item); const stat = fs__namespace.statSync(itemPath); if (stat.isDirectory() && !item.startsWith(".") && !excludeDirs.has(item)) { searchDirectory(itemPath); } else if (stat.isFile() && extensions.has(path__namespace.extname(itemPath))) { const contents = fs__namespace.readFileSync(itemPath, "utf8"); let match; while ((match = pattern.exec(contents)) !== null) { for (const [suffix, vars] of Object.entries(envVars)) { if (match[1] !== void 0 && !vars.has(match[1])) { vars.add(match[1]); } } } } } } const currentDirectory = process.cwd(); const envFilePaths = suffixes.map((suffix) => path__namespace.join(currentDirectory, `.env${suffix}`)); const exampleFiles = [".env.example", ".env.local.example"].map((example) => path__namespace.join(currentDirectory, example)); const existingFiles = envFilePaths.filter(fs__namespace.existsSync); for (const example of exampleFiles) { if (fs__namespace.existsSync(example)) { for (const filePath of envFilePaths) { if (!fs__namespace.existsSync(filePath)) { fs__namespace.copyFileSync(example, filePath); } } break; } } if (existingFiles.length > 0) { appendEnvVariables(); } else { createEnvFiles(); } function createEnvFiles() { return new Promise((resolve, reject) => { try { searchDirectory(currentDirectory); for (const suffix of suffixes) { const envFilePath = path__namespace.join(currentDirectory, `.env${suffix}`); const envFileContent = []; if (suffix !== "") { envFileContent.push(`ENV_FILE=.env${suffix}`); } for (const envVar of envVars[suffix] ?? []) { if (suffix === "" && envVar === "ENV_FILE") { continue; } else if (suffix !== "" && envVar === "ENV_FILE") { envFileContent.push(`${envVar}=.${suffix}`); } else { envFileContent.push(`${envVar}=`); } } fs__namespace.writeFileSync(envFilePath, envFileContent.join("\n")); } resolve(); } catch (error) { reject(error); } }); } function appendEnvVariables() { return new Promise((resolve, reject) => { try { searchDirectory(currentDirectory); for (const suffix of suffixes) { const envFilePath = path__namespace.join(currentDirectory, `.env${suffix}`); const envFileContent = []; if (fs__namespace.existsSync(envFilePath)) { const existingContent = fs__namespace.readFileSync(envFilePath, "utf8").split("\n"); const existingVars = new Set(existingContent.map((line) => line.split("=")[0])); for (const envVar of envVars[suffix] ?? []) { if (!existingVars.has(envVar)) { if (suffix === "" && envVar === "ENV_FILE") { continue; } else if (suffix !== "" && envVar === "ENV_FILE") { envFileContent.push(`${envVar}=.${suffix}`); } else { envFileContent.push(`${envVar}=`); } } } if (envFileContent.length > 0) { fs__namespace.appendFileSync(envFilePath, `${envFileContent.join("\n")} `); } } } resolve(); } catch (error) { reject(error); } }); } exports.appendEnvVariables = appendEnvVariables; exports.createEnvFiles = createEnvFiles;