UNPKG

@godspeedsystems/godspeed

Version:

Godspeed CLI

125 lines 5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const cross_spawn_1 = __importDefault(require("cross-spawn")); const path_1 = __importDefault(require("path")); const promises_1 = require("fs/promises"); const ora_1 = __importDefault(require("ora")); // const projectDirPath = path.resolve(process.cwd(), projectName); const program = new commander_1.Command(); const spinner = (0, ora_1.default)({ text: 'Installing packages... ', spinner: { frames: ['🌍 ', '🌎 ', '🌏 ', '🌐 ', '🌑 ', '🌒 ', '🌓 ', '🌔 '], interval: 180, }, }); const enableAction = async () => { // install that package async function installtracing(tracing) { try { spinner.start(); const child = (0, cross_spawn_1.default)('npm', ['install', `${tracing}`, '--quiet', '--no-warnings', '--silent', '--progress=false'], { stdio: 'inherit', }); await new Promise((resolve) => { child.on('close', () => { resolve(); }); }); spinner.stop(); console.log('\notel installed successfully!'); } catch (error) { spinner.stop(); console.error('Error during installation:', error.message); } } // Call the installPlugin function try { const envFilePath = path_1.default.join(process.cwd(), ".env"); let envFileContents = await (0, promises_1.readFile)(envFilePath, "utf-8"); // Check if OTEL_ENABLED is already set to true if (envFileContents.includes("OTEL_ENABLED=true")) { await installtracing("@godspeedsystems/tracing"); console.log("Observability is already enabled in the project."); return; } else if (envFileContents.includes("OTEL_ENABLED=false")) { envFileContents = envFileContents.replace("OTEL_ENABLED=false", "OTEL_ENABLED=true"); } else { envFileContents += "\nOTEL_ENABLED=true"; } // Enable OTEL by updating the .env file await installtracing("@godspeedsystems/tracing"); await (0, promises_1.writeFile)(envFilePath, envFileContents, "utf-8"); console.log(`Observability has been enabled`); } catch (error) { console.error(`Error enabling Observability: ${error.message}`); } }; const disableAction = async () => { // uninstall that package async function uninstalltracing(tracing) { try { spinner.start(); // Use spawnCommand instead of spawnSync const child = (0, cross_spawn_1.default)('npm', ['uninstall', `${tracing}`, '--quiet', '--no-warnings', '--silent', '--progress=false'], { stdio: 'inherit', // Redirect output }); await new Promise((resolve) => { child.on('close', () => { resolve(); }); }); spinner.stop(); console.log('\notel uninstalled successfully!'); } catch (error) { spinner.stop(); console.error('Error during uninstallation:', error.message); } } // Call the uninstallPlugin function try { const envFilePath = path_1.default.join(process.cwd(), ".env"); let envFileContents = await (0, promises_1.readFile)(envFilePath, "utf-8"); // Check if OTEL_ENABLED is already set to false if (envFileContents.includes("OTEL_ENABLED=false")) { await uninstalltracing("@godspeedsystems/tracing"); console.log("Observability is already disabled."); return; } else if (envFileContents.includes("OTEL_ENABLED=true")) { envFileContents = envFileContents.replace("OTEL_ENABLED=true", "OTEL_ENABLED=false"); } else { envFileContents += "\nOTEL_ENABLED=false"; } await uninstalltracing("@godspeedsystems/tracing"); await (0, promises_1.writeFile)(envFilePath, envFileContents, "utf-8"); console.log(`Observability has been disabled in the project`); } catch (error) { console.error(`Error disabling Observability: ${error.message}`); } }; const enable = program .command("enable") .description(`enable Observability in project.`) .action(async () => { enableAction(); }); const disable = program .command("disable") .description(`disable Observability in project.`) .action(async () => { disableAction(); }); exports.default = { enable, disable }; //# sourceMappingURL=index.js.map