qraft
Version:
A powerful CLI tool to qraft structured project setups from GitHub template repositories
220 lines • 10.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.authCommand = authCommand;
const chalk_1 = __importDefault(require("chalk"));
const commander_1 = require("commander");
const inquirer_1 = __importDefault(require("inquirer"));
const interactiveMode_1 = require("../interactive/interactiveMode");
function authCommand(boxManager) {
const auth = new commander_1.Command('auth');
// Login - set global token
auth
.command('login')
.description('Set up GitHub authentication')
.option('-t, --token <token>', 'GitHub personal access token')
.option('-i, --interactive', 'use interactive mode')
.action(async (options) => {
try {
// Use interactive mode if requested or no token provided
if (options.interactive || !options.token) {
const interactiveMode = new interactiveMode_1.InteractiveMode(boxManager);
await interactiveMode.setupAuthentication();
return;
}
// Non-interactive mode with provided token
const token = options.token;
// Test the token
console.log(chalk_1.default.blue('\n⏳ Testing authentication...'));
await boxManager.setGlobalToken(token);
// Test with default registry
const defaultRegistry = await boxManager.getDefaultRegistry();
const authResult = await boxManager.testAuthentication(defaultRegistry);
if (authResult.authenticated) {
console.log(chalk_1.default.green.bold('\n✅ Authentication successful!'));
console.log(chalk_1.default.gray(` Authenticated as: ${authResult.user}`));
console.log(chalk_1.default.gray(' Global token has been saved.'));
}
else {
console.error(chalk_1.default.red.bold('\n❌ Authentication failed'));
console.error(chalk_1.default.red(` Error: ${authResult.error}`));
console.error(chalk_1.default.gray('\nPlease check your token and try again.'));
process.exit(1);
}
}
catch (error) {
console.error(chalk_1.default.red('Error setting up authentication:'), error instanceof Error ? error.message : 'Unknown error');
process.exit(1);
}
});
// Set token for specific registry
auth
.command('token')
.description('Set authentication token for a specific registry')
.option('-r, --registry <registry>', 'registry name (required)')
.option('-t, --token <token>', 'GitHub personal access token')
.action(async (options) => {
try {
if (!options.registry) {
console.error(chalk_1.default.red('Error: Registry name is required'));
console.error(chalk_1.default.gray('Use: qraft auth token --registry <name> --token <token>'));
process.exit(1);
}
let token = options.token;
if (!token) {
const { inputToken } = await inquirer_1.default.prompt([
{
type: 'password',
name: 'inputToken',
message: `Enter GitHub token for registry "${options.registry}":`,
mask: '*',
validate: (input) => {
if (!input.trim()) {
return 'Token cannot be empty';
}
return true;
}
}
]);
token = inputToken;
}
// Test the token
console.log(chalk_1.default.blue('\n⏳ Testing authentication...'));
await boxManager.setRegistryToken(options.registry, token);
const authResult = await boxManager.testAuthentication(options.registry);
if (authResult.authenticated) {
console.log(chalk_1.default.green.bold('\n✅ Authentication successful!'));
console.log(chalk_1.default.gray(` Registry: ${options.registry}`));
console.log(chalk_1.default.gray(` Authenticated as: ${authResult.user}`));
}
else {
console.error(chalk_1.default.red.bold('\n❌ Authentication failed'));
console.error(chalk_1.default.red(` Error: ${authResult.error}`));
process.exit(1);
}
}
catch (error) {
console.error(chalk_1.default.red('Error setting registry token:'), error instanceof Error ? error.message : 'Unknown error');
process.exit(1);
}
});
// Test authentication
auth
.command('test')
.description('Test authentication for registries')
.option('-r, --registry <registry>', 'test specific registry')
.option('--all', 'test all registries')
.action(async (options) => {
try {
if (options.all) {
console.log(chalk_1.default.blue.bold('🔐 Testing Authentication for All Registries\n'));
const registries = await boxManager.listRegistries();
for (const registry of registries) {
console.log(chalk_1.default.yellow(`Testing ${registry.name}...`));
const hasAuth = await boxManager.hasAuthentication(registry.name);
if (!hasAuth) {
console.log(chalk_1.default.gray(' No authentication configured'));
continue;
}
const authResult = await boxManager.testAuthentication(registry.name);
if (authResult.authenticated) {
console.log(chalk_1.default.green(` ✅ Authenticated as: ${authResult.user}`));
}
else {
console.log(chalk_1.default.red(` ❌ Failed: ${authResult.error}`));
}
}
}
else if (options.registry) {
console.log(chalk_1.default.blue.bold(`🔐 Testing Authentication for ${options.registry}\n`));
const hasAuth = await boxManager.hasAuthentication(options.registry);
if (!hasAuth) {
console.log(chalk_1.default.yellow('No authentication configured for this registry'));
console.log(chalk_1.default.gray('Set up authentication:'));
console.log(chalk_1.default.cyan(` qraft auth token --registry ${options.registry}`));
return;
}
const authResult = await boxManager.testAuthentication(options.registry);
if (authResult.authenticated) {
console.log(chalk_1.default.green.bold('✅ Authentication successful!'));
console.log(chalk_1.default.gray(` Authenticated as: ${authResult.user}`));
}
else {
console.log(chalk_1.default.red.bold('❌ Authentication failed'));
console.log(chalk_1.default.red(` Error: ${authResult.error}`));
process.exit(1);
}
}
else {
// Test default registry
const defaultRegistry = await boxManager.getDefaultRegistry();
console.log(chalk_1.default.blue.bold(`🔐 Testing Authentication for ${defaultRegistry} (default)\n`));
const hasAuth = await boxManager.hasAuthentication(defaultRegistry);
if (!hasAuth) {
console.log(chalk_1.default.yellow('No authentication configured'));
console.log(chalk_1.default.gray('Set up authentication:'));
console.log(chalk_1.default.cyan(' qraft auth login'));
return;
}
const authResult = await boxManager.testAuthentication(defaultRegistry);
if (authResult.authenticated) {
console.log(chalk_1.default.green.bold('✅ Authentication successful!'));
console.log(chalk_1.default.gray(` Authenticated as: ${authResult.user}`));
}
else {
console.log(chalk_1.default.red.bold('❌ Authentication failed'));
console.log(chalk_1.default.red(` Error: ${authResult.error}`));
process.exit(1);
}
}
}
catch (error) {
console.error(chalk_1.default.red('Error testing authentication:'), error instanceof Error ? error.message : 'Unknown error');
process.exit(1);
}
});
// Show authentication status
auth
.command('status')
.description('Show authentication status for all registries')
.action(async () => {
try {
console.log(chalk_1.default.blue.bold('🔐 Authentication Status\n'));
const registries = await boxManager.listRegistries();
for (const registry of registries) {
console.log(chalk_1.default.yellow.bold(`${registry.name}:`));
const hasAuth = await boxManager.hasAuthentication(registry.name);
if (hasAuth) {
console.log(chalk_1.default.green(' ✅ Token configured'));
try {
const authResult = await boxManager.testAuthentication(registry.name);
if (authResult.authenticated) {
console.log(chalk_1.default.gray(` Authenticated as: ${authResult.user}`));
}
else {
console.log(chalk_1.default.red(` ❌ Token invalid: ${authResult.error}`));
}
}
catch (error) {
console.log(chalk_1.default.red(' ❌ Failed to test token'));
}
}
else {
console.log(chalk_1.default.gray(' ❌ No token configured'));
}
if (registry.isDefault) {
console.log(chalk_1.default.green(' (default registry)'));
}
console.log();
}
}
catch (error) {
console.error(chalk_1.default.red('Error checking authentication status:'), error instanceof Error ? error.message : 'Unknown error');
process.exit(1);
}
});
return auth;
}
//# sourceMappingURL=auth.js.map