anomaly-cli
Version:
A command-line interface tool for anomaly detection and management
108 lines (107 loc) ⢠4.41 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { Command } from "commander";
import chalk from "chalk";
import { authManager } from "./auth/index.js";
import { createApp } from "./commands/createApp.js";
const program = new Command();
program.name("anomaly-cli").description("Anomaly CLI").version("0.0.1");
// Global variable to store current user
let currentUser = null;
// Authentication middleware
function requireAuth() {
return __awaiter(this, void 0, void 0, function* () {
console.log(chalk.cyan.bold("\nš Welcome to Anomaly CLI!\n"));
// Check if user is already authenticated
currentUser = yield authManager.getCurrentUser();
if (currentUser) {
console.log(chalk.green(`ā
Welcome back, ${currentUser.email}!\n`));
return currentUser;
}
// If not authenticated, prompt for authentication
console.log(chalk.yellow("š You need to sign in to use this CLI.\n"));
currentUser = yield authManager.promptAuthentication();
console.log(chalk.green(`\nš Welcome, ${currentUser.email}!\n`));
return currentUser;
});
}
// Auth command
program
.command("auth")
.description("Authentication commands")
.addCommand(new Command("status")
.description("Check authentication status")
.action(() => __awaiter(void 0, void 0, void 0, function* () {
const user = yield authManager.getCurrentUser();
if (user) {
console.log(chalk.green(`ā
Signed in as: ${user.email}`));
console.log(chalk.gray(` User ID: ${user.uid}`));
}
else {
console.log(chalk.red("ā Not signed in"));
}
})))
.addCommand(new Command("login")
.description("Sign in to your account")
.action(() => __awaiter(void 0, void 0, void 0, function* () {
try {
const user = yield authManager.promptAuthentication();
currentUser = user;
console.log(chalk.green(`\nš Welcome, ${user.email}!\n`));
}
catch (error) {
console.log(chalk.red("Authentication failed"));
}
})))
.addCommand(new Command("logout")
.description("Sign out of your account")
.action(() => __awaiter(void 0, void 0, void 0, function* () {
try {
yield authManager.signOut();
currentUser = null;
console.log(chalk.green("\nš You have been signed out successfully!\n"));
}
catch (error) {
console.log(chalk.red(`Error signing out: ${error.message}`));
}
})));
program
.command("init")
.description("Initialize and secure your app with anomaly protection")
.action(() => __awaiter(void 0, void 0, void 0, function* () {
const user = yield requireAuth();
yield createApp(user);
}));
// Add a public command that doesn't require authentication
program
.command("info")
.description("Show CLI information")
.action(() => {
console.log(chalk.blue.bold("\nš Anomaly CLI Information\n"));
console.log(`Version: ${chalk.cyan("0.0.1")}`);
console.log(`Description: ${chalk.gray("A powerful CLI tool with Firebase authentication")}`);
console.log(`\nTo get started:`);
console.log(` ⢠Run ${chalk.cyan("anomaly auth login")} to sign in`);
console.log(` ⢠Run ${chalk.cyan("anomaly init")} to secure your app`);
console.log(` ⢠Run ${chalk.cyan("anomaly --help")} to see all commands\n`);
});
// Handle errors gracefully
process.on("uncaughtException", (error) => {
if (error.name === "ExitPromptError") {
console.log(chalk.yellow("\nš Until next time!"));
process.exit(0);
}
else {
console.error(chalk.red("An unexpected error occurred:"), error.message);
process.exit(1);
}
});
program.parse(process.argv);