Skip to content

fix: otel commands updated #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 74 additions & 105 deletions src/commands/otel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,58 @@ import ora from 'ora';
// const projectDirPath = path.resolve(process.cwd(), projectName);

const program = new Command();
const spinner = ora({
text: 'Installing packages... ',
spinner: {
frames: ['🌍 ', '🌎 ', '🌏 ', '🌐 ', '🌑 ', '🌒 ', '🌓 ', '🌔 '],
interval: 180,
},
});

const enableAction = async () => {
// install that package
const spinner = ora({
text: 'Installing packages... ',
spinner: {
frames: ['🌍 ', '🌎 ', '🌏 ', '🌐 ', '🌑 ', '🌒 ', '🌓 ', '🌔 '],
interval: 180,
},
});


async function installtracing(tracing:any) {
try {
spinner.start();

// Use spawnCommand instead of spawnSync
const child = spawnSync('npm', ['install', `${tracing}`, '--quiet', '--no-warnings', '--silent', '--progress=false'], {
stdio: 'inherit', // Redirect output
});

await new Promise<void>((resolve) => {
child.on('close', () => {
resolve();
});
async function installtracing(tracing:any) {
try {
spinner.start();

const child = spawnSync('npm', ['install', `${tracing}`, '--quiet', '--no-warnings', '--silent', '--progress=false'], {
stdio: 'inherit',
});

await new Promise<void>((resolve) => {
child.on('close', () => {
resolve();
});

spinner.stop(); // Stop the spinner when the installation is complete
console.log('\notel installed successfully!');
} catch (error:any) {
spinner.stop(); // Stop the spinner in case of an error
console.error('Error during installation:', error.message);
}
});

spinner.stop();
console.log('\notel installed successfully!');
} catch (error:any) {
spinner.stop();
console.error('Error during installation:', error.message);
}
}

// Call the installPlugin function


try {
const envFilePath = path.join(process.cwd(), ".env");
let envFileContents = await 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")) {
const updatedData = envFileContents.replace("OTEL_ENABLED=false", "OTEL_ENABLED=true");
await installtracing("@godspeedsystems/tracing");
// Write the modified contents back to the .env file
fs.writeFile('.env', updatedData, (writeErr) => {
if (writeErr) {
console.error("Error writing to .env file:", writeErr);
} else {
console.log("Observability has been enabled");
}
})
}else{
envFileContents += "OTEL_ENABLED=true";
await installtracing("@godspeedsystems/tracing");
await writeFile(envFilePath, envFileContents, "utf-8");
console.log(`Observability has been enabled`);
} 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 writeFile(envFilePath, envFileContents, "utf-8");
console.log(`Observability has been enabled`);
} catch (error:any) {
console.error(`Error enabling Observability: ${error.message}`);
}
Expand All @@ -81,69 +67,52 @@ const enableAction = async () => {

const disableAction = async () => {
// uninstall that package
const spinner = ora({
text: 'Uninstalling packages... ',
spinner: {
frames: ['🌍 ', '🌎 ', '🌏 ', '🌐 ', '🌑 ', '🌒 ', '🌓 ', '🌔 '],
interval: 180,
},
});


async function uninstalltracing(tracing:any) {
try {
spinner.start();

// Use spawnCommand instead of spawnSync
const child = spawnSync('npm', ['uninstall', `${tracing}`, '--quiet', '--no-warnings', '--silent', '--progress=false'], {
stdio: 'inherit', // Redirect output
});

await new Promise<void>((resolve) => {
child.on('close', () => {
resolve();
});
async function uninstalltracing(tracing:any) {
try {
spinner.start();

// Use spawnCommand instead of spawnSync
const child = spawnSync('npm', ['uninstall', `${tracing}`, '--quiet', '--no-warnings', '--silent', '--progress=false'], {
stdio: 'inherit', // Redirect output
});

await new Promise<void>((resolve) => {
child.on('close', () => {
resolve();
});

spinner.stop(); // Stop the spinner when the installation is complete
console.log('\notel uninstalled successfully!');
} catch (error:any) {
spinner.stop(); // Stop the spinner in case of an error
console.error('Error during uninstallation:', error.message);
}
});

spinner.stop();
console.log('\notel uninstalled successfully!');
} catch (error:any) {
spinner.stop();
console.error('Error during uninstallation:', error.message);
}
}

// Call the uninstallPlugin function

try {
const envFilePath = path.join(process.cwd(), ".env");
let envFileContents = await readFile(envFilePath, "utf-8");

// Check if OTEL_ENABLED is already set to false
if (envFileContents.includes("OTEL_ENABLED=false")) {
console.log("Observability is already disabled.");
return;
}else if(envFileContents.includes("OTEL_ENABLED=true")){
const updatedData = envFileContents.replace("OTEL_ENABLED=true", "OTEL_ENABLED=false");
await uninstalltracing("@godspeedsystems/tracing");
// Write the modified contents back to the .env file
fs.writeFile('.env', updatedData, (writeErr) => {
if (writeErr) {
console.error("Error writing to .env file:", writeErr);
} else {
console.log("Observability has been disabled.");
}
})
}else{
envFileContents += "\nOTEL_ENABLED=false";
await uninstalltracing("@godspeedsystems/tracing");
await writeFile(envFilePath, envFileContents, "utf-8");
console.log(`Observability has been disabled in the project`);
}

} catch (error:any) {
console.error(`Error disabling Observability: ${error.message}`);
try {
const envFilePath = path.join(process.cwd(), ".env");
let envFileContents = await 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 writeFile(envFilePath, envFileContents, "utf-8");
console.log(`Observability has been disabled in the project`);

} catch (error:any) {
console.error(`Error disabling Observability: ${error.message}`);
}
};


Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"include": ["src"],
"compilerOptions": {
"target": "es2021",
"skipLibCheck": true,
"module": "commonjs",
"lib": ["es6", "es2021", "dom"],
"declaration": true,
Expand Down