UNPKG

quallaa-cli

Version:

Sets up core infrastructure services for AI-assisted development

187 lines • 7.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupGitHub = setupGitHub; exports.createGitHubRepo = createGitHubRepo; exports.initializeGitRepo = initializeGitRepo; exports.pushToGitHub = pushToGitHub; const chalk_1 = __importDefault(require("chalk")); const inquirer_1 = __importDefault(require("inquirer")); const axios_1 = __importDefault(require("axios")); const child_process_1 = require("child_process"); const util_1 = require("util"); const credentials_1 = require("../storage/credentials"); const execAsync = (0, util_1.promisify)(child_process_1.exec); const GITHUB_API_BASE = 'https://api.github.com'; async function setupGitHub() { try { console.log(chalk_1.default.gray('\nGitHub provides version control and repository hosting.')); console.log(chalk_1.default.gray('You\'ll need a GitHub account (free tier available).\n')); const existing = await (0, credentials_1.getCredentials)('github'); if (existing) { const { useExisting } = await inquirer_1.default.prompt([ { type: 'confirm', name: 'useExisting', message: 'Existing GitHub credentials found. Use them?', default: true, }, ]); if (useExisting) { return { success: true, service: 'github', message: 'Using existing credentials' }; } } const cliInstalled = await checkGitHubCLI(); const useMethod = cliInstalled ? await askAuthMethod() : 'token'; let token; let username; if (useMethod === 'cli' && cliInstalled) { const result = await authWithCLI(); token = result.token; username = result.username; } else { const result = await authWithToken(); token = result.token; username = result.username; } await (0, credentials_1.saveCredentials)('github', { token, username }); await configureGit(username); return { success: true, service: 'github', credentials: { github: { token, username } }, }; } catch (error) { return { success: false, service: 'github', message: error instanceof Error ? error.message : 'Setup failed', error: error, }; } } async function checkGitHubCLI() { try { await execAsync('gh --version'); return true; } catch { return false; } } async function askAuthMethod() { const { method } = await inquirer_1.default.prompt([ { type: 'list', name: 'method', message: 'How would you like to authenticate with GitHub?', choices: [ { name: 'GitHub CLI (recommended)', value: 'cli' }, { name: 'Personal Access Token', value: 'token' }, ], }, ]); return method; } async function authWithCLI() { console.log(chalk_1.default.yellow('\nšŸ” Authenticating with GitHub CLI...')); try { const { stdout: authStatus } = await execAsync('gh auth status'); if (authStatus.includes('Logged in')) { const { stdout: token } = await execAsync('gh auth token'); const { stdout: userJson } = await execAsync('gh api user'); const user = JSON.parse(userJson); return { token: token.trim(), username: user.login }; } } catch { } await execAsync('gh auth login --web --scopes repo,user,workflow'); const { stdout: token } = await execAsync('gh auth token'); const { stdout: userJson } = await execAsync('gh api user'); const user = JSON.parse(userJson); return { token: token.trim(), username: user.login }; } async function authWithToken() { console.log(chalk_1.default.yellow('\nšŸ“ Personal Access Token authentication')); console.log(chalk_1.default.gray('1. Go to: https://github.com/settings/tokens/new')); console.log(chalk_1.default.gray('2. Create a token with these scopes: repo, user, workflow')); console.log(chalk_1.default.gray('3. Copy the token and paste it below\n')); const { token } = await inquirer_1.default.prompt([ { type: 'password', name: 'token', message: 'GitHub Personal Access Token:', validate: (input) => input.length > 0 || 'Token is required', }, ]); const user = await verifyToken(token); return { token, username: user.login }; } async function verifyToken(token) { const response = await axios_1.default.get(`${GITHUB_API_BASE}/user`, { headers: { Authorization: `token ${token}`, Accept: 'application/vnd.github.v3+json', }, }); return response.data; } async function configureGit(_username) { console.log(chalk_1.default.yellow('\nāš™ļø Configuring Git...')); const credentials = await (0, credentials_1.getCredentials)('github'); const response = await axios_1.default.get(`${GITHUB_API_BASE}/user`, { headers: { Authorization: `token ${credentials.token}`, Accept: 'application/vnd.github.v3+json', }, }); const { name, email } = response.data; if (name) { await execAsync(`git config --global user.name "${name}"`); } if (email) { await execAsync(`git config --global user.email "${email}"`); } console.log(chalk_1.default.green('āœ“ Git configured')); } async function createGitHubRepo(name, description, isPrivate = false) { const credentials = await (0, credentials_1.getCredentials)('github'); if (!credentials?.token) { throw new Error('GitHub not configured. Run "quallaa setup github" first.'); } const response = await axios_1.default.post(`${GITHUB_API_BASE}/user/repos`, { name, description, private: isPrivate, auto_init: true, gitignore_template: 'Node', license_template: 'mit', }, { headers: { Authorization: `token ${credentials.token}`, Accept: 'application/vnd.github.v3+json', }, }); return response.data; } async function initializeGitRepo(projectPath) { const credentials = await (0, credentials_1.getCredentials)('github'); if (!credentials?.token || !credentials?.username) { throw new Error('GitHub not configured. Run "quallaa setup github" first.'); } await execAsync('git init', { cwd: projectPath }); await execAsync('git add .', { cwd: projectPath }); await execAsync('git commit -m "Initial commit from Quallaa CLI"', { cwd: projectPath }); console.log(chalk_1.default.green('āœ“ Git repository initialized')); } async function pushToGitHub(projectPath, repoUrl) { await execAsync(`git remote add origin ${repoUrl}`, { cwd: projectPath }); await execAsync('git push -u origin main', { cwd: projectPath }); console.log(chalk_1.default.green('āœ“ Code pushed to GitHub')); } //# sourceMappingURL=github.js.map