@labnex/cli
Version:
CLI for Labnex, an AI-Powered Testing Automation Platform
135 lines • 5.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.authCommand = void 0;
const commander_1 = require("commander");
const inquirer_1 = __importDefault(require("inquirer"));
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const client_1 = require("../api/client");
const config_1 = require("../utils/config");
exports.authCommand = new commander_1.Command('auth')
.description('Manage Labnex authentication, API tokens, and session status.')
.addCommand(new commander_1.Command('login')
.description('Authenticate with the Labnex platform to obtain an API token.')
.option('-e, --email <email>', 'Email address')
.option('-p, --password <password>', 'Password (not recommended for security)')
.action(async (options) => {
try {
let { email, password } = options;
// Prompt for email if not provided
if (!email) {
const emailPrompt = await inquirer_1.default.prompt([
{
type: 'input',
name: 'email',
message: 'Email:',
validate: (input) => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(input) || 'Please enter a valid email address';
}
}
]);
email = emailPrompt.email;
}
// Prompt for password if not provided
if (!password) {
const passwordPrompt = await inquirer_1.default.prompt([
{
type: 'password',
name: 'password',
message: 'Password:',
mask: '*',
validate: (input) => input.length > 0 || 'Password is required'
}
]);
password = passwordPrompt.password;
}
const spinner = (0, ora_1.default)('Authenticating...').start();
try {
const response = await client_1.apiClient.login(email, password);
if (response.success && response.data.token) {
// Save auth data to config
await (0, config_1.updateConfig)({
token: response.data.token,
email: email,
userId: response.data.user.id
});
spinner.succeed(chalk_1.default.green(`Successfully logged in as ${email}`));
console.log(chalk_1.default.gray(`Token saved to config file`));
}
else {
spinner.fail(chalk_1.default.red('Login failed: ' + (response.error || 'Unknown error')));
}
}
catch (error) {
spinner.fail(chalk_1.default.red('Login failed: ' + (error instanceof Error ? error.message : 'Unknown error')));
}
}
catch (error) {
console.error(chalk_1.default.red('Error:'), error instanceof Error ? error.message : 'Unknown error');
}
}))
.addCommand(new commander_1.Command('logout')
.description('Clear local authentication data and log out from Labnex.')
.action(async () => {
try {
const spinner = (0, ora_1.default)('Logging out...').start();
await (0, config_1.clearConfig)();
spinner.succeed(chalk_1.default.green('Successfully logged out'));
}
catch (error) {
console.error(chalk_1.default.red('Error:'), error.message);
}
}))
.addCommand(new commander_1.Command('status')
.description('Check your current Labnex authentication status and API configuration.')
.action(async () => {
try {
const config = await (0, config_1.loadConfig)();
if (!config.token) {
console.log(chalk_1.default.yellow('Not authenticated. Run: labnex auth login'));
return;
}
const spinner = (0, ora_1.default)('Checking authentication...').start();
try {
const response = await client_1.apiClient.me();
if (response.success) {
spinner.succeed(chalk_1.default.green('Authenticated'));
console.log(chalk_1.default.gray(`Email: ${config.email}`));
console.log(chalk_1.default.gray(`API URL: ${config.apiUrl}`));
}
else {
spinner.fail(chalk_1.default.red('Authentication invalid'));
}
}
catch (error) {
spinner.fail(chalk_1.default.red('Authentication check failed'));
if (error.response?.status === 401) {
console.log(chalk_1.default.yellow('Please login again: labnex auth login'));
}
}
}
catch (error) {
console.error(chalk_1.default.red('Error:'), error.message);
}
}))
.addCommand(new commander_1.Command('use-key')
.description('Save an API key issued from the Labnex dashboard (no email/password login required).')
.argument('<token>', 'API key that starts with lab_')
.action(async (token) => {
try {
if (!token || token.length < 10) {
console.error(chalk_1.default.red('Invalid token.'));
return;
}
await (0, config_1.updateConfig)({ token });
console.log(chalk_1.default.green('✓ API key saved. You can now run Labnex commands.'));
}
catch (error) {
console.error(chalk_1.default.red('Failed to save key:'), error.message);
}
}));
//# sourceMappingURL=auth.js.map