@ace-sdk/cli
Version:
ACE CLI - Command-line tool for intelligent pattern learning and playbook management
83 lines • 3.23 kB
JavaScript
/**
* Refresh Command - Refresh organizations from server
* @package @ace-sdk/cli
*/
import ora from 'ora';
import chalk from 'chalk';
import { isAuthenticated, isUserAuthenticated, refreshOrganizations, getCurrentUser } from '@ace-sdk/core';
import { globalOptions } from '../cli.js';
export async function refreshCommand() {
const spinner = ora({ isSilent: globalOptions.quiet || globalOptions.json });
// Check if logged in
if (!isAuthenticated()) {
if (globalOptions.json) {
console.log(JSON.stringify({
error: true,
message: 'Not logged in'
}));
}
else {
console.log(chalk.yellow('Not logged in'));
console.log(chalk.gray('Run'), chalk.white('ce-ace login'), chalk.gray('to authenticate'));
}
process.exit(1);
}
// Check if using user token
if (!isUserAuthenticated()) {
if (globalOptions.json) {
console.log(JSON.stringify({
error: true,
message: 'Cannot refresh with legacy token'
}));
}
else {
console.log(chalk.yellow('Cannot refresh with legacy token'));
console.log(chalk.gray('Run'), chalk.white('ce-ace login'), chalk.gray('to upgrade to user authentication'));
}
process.exit(1);
}
spinner.start('Refreshing organizations from server...');
try {
const beforeUser = getCurrentUser();
const beforeCount = beforeUser?.organizations?.length || 0;
const orgs = await refreshOrganizations();
spinner.succeed('Organizations refreshed');
if (globalOptions.json) {
console.log(JSON.stringify({
success: true,
count: orgs.length,
organizations: orgs
}));
}
else {
const diff = orgs.length - beforeCount;
const diffText = diff > 0 ? chalk.green(`+${diff}`) : diff < 0 ? chalk.red(`${diff}`) : chalk.gray('no change');
console.log();
console.log(chalk.cyan(`Organizations (${orgs.length})`), chalk.gray(`[${diffText}]`));
console.log(chalk.gray('────────────────────────────────────────'));
for (const org of orgs) {
console.log(` ${chalk.white(org.name)} ${chalk.gray(`[${org.role}]`)}`);
if (globalOptions.verbose) {
console.log(` ${chalk.gray('ID:')} ${org.org_id}`);
}
}
if (orgs.length === 0) {
console.log(chalk.gray(' No organizations found'));
}
}
}
catch (error) {
spinner.fail('Failed to refresh organizations');
if (globalOptions.json) {
console.error(JSON.stringify({
error: true,
message: error instanceof Error ? error.message : String(error)
}));
}
else {
console.error(chalk.red(error instanceof Error ? error.message : String(error)));
}
process.exit(1);
}
}
//# sourceMappingURL=refresh.js.map