UNPKG

add-inbox

Version:

Add inbox notifications to your application with one command

150 lines 7.84 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupEnvExampleNextJs = setupEnvExampleNextJs; exports.setupEnvExampleReact = setupEnvExampleReact; const constants_1 = require("../constants"); const file_1 = __importStar(require("../utils/file")); const logger_1 = __importDefault(require("../utils/logger")); const react_version_1 = require("./react-version"); function setupEnvExampleNextJs(updateExisting, appId = null) { // Validate appId to prevent injection if (appId && (appId.includes('\n') || appId.includes('\r'))) { throw new Error('Invalid appId: cannot contain newline characters'); } logger_1.default.gray('• Setting up environment configuration for Next.js...'); const envExamplePath = file_1.default.joinPaths(process.cwd(), '.env.example'); const envLocalPath = file_1.default.joinPaths(process.cwd(), '.env.local'); // Validate appId: allow only alphanumeric and dashes const safeAppId = typeof appId === 'string' && /^[a-zA-Z0-9-]+$/.test(appId) ? appId : ''; // For .env.example, always use empty value const envExampleContent = `\n# Novu configuration (added by Novu Inbox Installer) ${constants_1.ENV_VARIABLES.NEXTJS.APP_ID}= `; // For .env.local, use provided appId if available const envLocalContent = `\n# Novu configuration (added by Novu Inbox Installer) ${constants_1.ENV_VARIABLES.NEXTJS.APP_ID}=${safeAppId} `; // Handle .env.example if (file_1.default.exists(envExamplePath)) { const existingContent = file_1.default.readFile(envExamplePath); if (existingContent?.includes(constants_1.ENV_VARIABLES.NEXTJS.APP_ID)) { logger_1.default.blue(' • Novu variables already detected in .env.example. No changes made.'); } else if (updateExisting) { file_1.default.appendFile(envExamplePath, envExampleContent); logger_1.default.blue(' • Appended Novu configuration to existing .env.example'); } else { logger_1.default.warning(' • .env.example exists. Skipping modification as Novu variables were not found and appending was not confirmed.'); logger_1.default.cyan(' Please manually add Novu variables to your .env.example:'); logger_1.default.cyan(` ${constants_1.ENV_VARIABLES.NEXTJS.APP_ID}=`); } } else { file_1.default.writeFile(envExamplePath, envExampleContent.trimStart()); logger_1.default.blue(' • Created .env.example with Novu configuration'); } // Handle .env.local if (file_1.default.exists(envLocalPath)) { const existingContent = file_1.default.readFile(envLocalPath); if (existingContent?.includes(constants_1.ENV_VARIABLES.NEXTJS.APP_ID)) { // Update only the Novu variable, preserve other content const updatedContent = (0, file_1.updateEnvVariable)(existingContent, constants_1.ENV_VARIABLES.NEXTJS.APP_ID, safeAppId); file_1.default.writeFile(envLocalPath, updatedContent); logger_1.default.blue(' • Updated Novu configuration in .env.local'); } else { file_1.default.appendFile(envLocalPath, envLocalContent); logger_1.default.blue(' • Added Novu configuration to existing .env.local'); } } else { file_1.default.writeFile(envLocalPath, envLocalContent.trimStart()); logger_1.default.blue(' • Created .env.local with Novu configuration'); } logger_1.default.gray(' Remember to fill in your Novu credentials in .env.local.'); logger_1.default.gray(' Ensure .env.local is in your .gitignore file.'); } function setupEnvExampleReact(updateExisting, appId = null) { logger_1.default.gray('• Setting up environment configuration for React...'); const envPath = file_1.default.joinPaths(process.cwd(), '.env.example'); const envLocalPath = file_1.default.joinPaths(process.cwd(), '.env'); // Get the appropriate environment variable name based on React version const envVarName = (0, react_version_1.getEnvironmentVariableName)(); // Validate appId: allow only alphanumeric and dashes const safeAppId = typeof appId === 'string' && /^[a-zA-Z0-9-]+$/.test(appId) ? appId : ''; // For .env.example, always use empty value const envExampleContent = `\n# Novu configuration (added by Novu Inbox Installer) ${envVarName}= `; // For .env, use provided appId if available const envContent = `\n# Novu configuration (added by Novu Inbox Installer) ${envVarName}=${safeAppId} `; if (file_1.default.exists(envPath)) { const existingContent = file_1.default.readFile(envPath); if (existingContent?.includes(envVarName)) { logger_1.default.blue(' • Novu variables already detected in .env.example. No changes made.'); } else if (updateExisting) { file_1.default.appendFile(envPath, envExampleContent); logger_1.default.blue(' • Appended Novu configuration to existing .env.example'); } else { logger_1.default.warning(' • .env.example exists. Skipping modification as Novu variables were not found and appending was not confirmed.'); logger_1.default.cyan(' Please manually add Novu variables to your .env.example:'); logger_1.default.cyan(` ${envVarName}=`); } } else { file_1.default.writeFile(envPath, envExampleContent.trimStart()); logger_1.default.blue(' • Created .env.example with Novu configuration'); } // Handle .env if (file_1.default.exists(envLocalPath)) { const existingContent = file_1.default.readFile(envLocalPath); if (existingContent?.includes(envVarName)) { // Update only the Novu variable, preserve other content const updatedContent = (0, file_1.updateEnvVariable)(existingContent, envVarName, safeAppId); file_1.default.writeFile(envLocalPath, updatedContent); logger_1.default.blue(' • Updated Novu configuration in .env'); } else { file_1.default.appendFile(envLocalPath, envContent); logger_1.default.blue(' • Added Novu configuration to existing .env'); } } else { file_1.default.writeFile(envLocalPath, envContent.trimStart()); logger_1.default.blue(' • Created .env with Novu configuration'); } logger_1.default.gray(' Remember to fill in your Novu credentials in .env.'); logger_1.default.gray(' Ensure .env is in your .gitignore file.'); } //# sourceMappingURL=env.js.map