@vxrn/takeout-cli
Version:
CLI tools for Takeout starter kit - interactive onboarding and project setup
98 lines (96 loc) • 3.97 kB
JavaScript
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: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__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: !0 }), mod);
var env_exports = {};
__export(env_exports, {
copyEnvFile: () => copyEnvFile,
createEnvLocal: () => createEnvLocal,
envFileExists: () => envFileExists,
generateSecret: () => generateSecret,
readEnvVariable: () => readEnvVariable,
updateEnvVariable: () => updateEnvVariable
});
module.exports = __toCommonJS(env_exports);
var import_node_crypto = require("node:crypto"), import_node_fs = require("node:fs"), import_node_path = require("node:path");
function generateSecret(length = 32) {
return (0, import_node_crypto.randomBytes)(length).toString("hex");
}
function envFileExists(cwd, filename = ".env") {
return (0, import_node_fs.existsSync)((0, import_node_path.join)(cwd, filename));
}
function copyEnvFile(cwd, source, target) {
const sourcePath = (0, import_node_path.join)(cwd, source), targetPath = (0, import_node_path.join)(cwd, target);
if (!(0, import_node_fs.existsSync)(sourcePath))
return { success: !1, error: `Source file ${source} not found` };
if ((0, import_node_fs.existsSync)(targetPath))
return { success: !1, error: `Target file ${target} already exists` };
try {
return (0, import_node_fs.copyFileSync)(sourcePath, targetPath), { success: !0 };
} catch (error) {
return {
success: !1,
error: error instanceof Error ? error.message : "Unknown error"
};
}
}
function updateEnvVariable(cwd, key, value, filename = ".env") {
const envPath = (0, import_node_path.join)(cwd, filename);
if (!(0, import_node_fs.existsSync)(envPath))
return { success: !1, error: `${filename} not found` };
try {
let content = (0, import_node_fs.readFileSync)(envPath, "utf-8");
const keyRegex = new RegExp(`^${key}=.*$`, "m");
return keyRegex.test(content) ? content = content.replace(keyRegex, `${key}=${value}`) : content = content.trimEnd() + `
${key}=${value}
`, (0, import_node_fs.writeFileSync)(envPath, content, "utf-8"), { success: !0 };
} catch (error) {
return {
success: !1,
error: error instanceof Error ? error.message : "Unknown error"
};
}
}
function createEnvLocal(cwd) {
const envLocalPath = (0, import_node_path.join)(cwd, ".env.local");
if ((0, import_node_fs.existsSync)(envLocalPath))
return { success: !0 };
const template = `# Local environment overrides
# This file is gitignored and never committed
# Add your personal secrets and local configuration here
# These values override .env
# Example:
# BETTER_AUTH_SECRET=your-secret-here
# AWS_ACCESS_KEY_ID=your-key-here
`;
try {
return (0, import_node_fs.writeFileSync)(envLocalPath, template, "utf-8"), { success: !0 };
} catch (error) {
return {
success: !1,
error: error instanceof Error ? error.message : "Unknown error"
};
}
}
function readEnvVariable(cwd, key, filename = ".env") {
const envPath = (0, import_node_path.join)(cwd, filename);
if (!(0, import_node_fs.existsSync)(envPath))
return null;
try {
const content = (0, import_node_fs.readFileSync)(envPath, "utf-8"), keyRegex = new RegExp(`^${key}=(.*)$`, "m");
return content.match(keyRegex)?.[1]?.trim().replace(/^["']|["']$/g, "") || null;
} catch {
return null;
}
}
//# sourceMappingURL=env.js.map