build-in-public-bot
Version:
AI-powered CLI bot for automating build-in-public tweets with code screenshots
127 lines ⢠5.7 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupApiCommand = void 0;
const commander_1 = require("commander");
const chalk_1 = __importDefault(require("chalk"));
const prompts_1 = require("../utils/prompts");
const logger_1 = require("../utils/logger");
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
exports.setupApiCommand = new commander_1.Command('setup-api')
.description('Set up or update your OpenRouter API key')
.action(async () => {
console.log(chalk_1.default.bold('\nš API Key Setup\n'));
if (process.env.OPENROUTER_API_KEY) {
const currentKey = process.env.OPENROUTER_API_KEY;
const maskedKey = currentKey.slice(0, 8) + '...' + currentKey.slice(-4);
console.log(chalk_1.default.green(`ā
Current API key: ${maskedKey}`));
const update = await (0, prompts_1.prompt)('Do you want to update it? (y/n)', {
validate: (input) => ['y', 'n', 'yes', 'no'].includes(input.toLowerCase())
});
if (update.toLowerCase() === 'n' || update.toLowerCase() === 'no') {
return;
}
}
else {
console.log(chalk_1.default.yellow('ā ļø No API key currently configured'));
}
console.log(chalk_1.default.dim('\nTo get an API key:'));
console.log(chalk_1.default.dim('1. Visit https://openrouter.ai'));
console.log(chalk_1.default.dim('2. Sign up for a free account'));
console.log(chalk_1.default.dim('3. Copy your API key from the dashboard\n'));
const apiKey = await (0, prompts_1.prompt)('Paste your OpenRouter API key', {
mask: true,
validate: (input) => {
if (!input)
return 'API key is required';
if (input.length < 20)
return 'Invalid API key format';
if (!input.startsWith('sk-'))
return 'API key should start with "sk-"';
return true;
}
});
const envPath = path.join(process.cwd(), '.env');
try {
let envContent = '';
try {
envContent = await fs.readFile(envPath, 'utf-8');
}
catch {
console.log(chalk_1.default.dim('Creating .env file...'));
}
if (envContent.includes('OPENROUTER_API_KEY')) {
envContent = envContent.replace(/OPENROUTER_API_KEY=.*/, `OPENROUTER_API_KEY=${apiKey}`);
console.log(chalk_1.default.green('ā
Updated existing API key in .env file'));
}
else {
envContent += `${envContent ? '\n' : ''}OPENROUTER_API_KEY=${apiKey}\n`;
console.log(chalk_1.default.green('ā
Added API key to .env file'));
}
await fs.writeFile(envPath, envContent);
process.env.OPENROUTER_API_KEY = apiKey;
try {
const gitignorePath = path.join(process.cwd(), '.gitignore');
let gitignoreContent = '';
try {
gitignoreContent = await fs.readFile(gitignorePath, 'utf-8');
}
catch {
}
if (!gitignoreContent.includes('.env')) {
gitignoreContent += `${gitignoreContent ? '\n' : ''}# Environment variables\n.env\n`;
await fs.writeFile(gitignorePath, gitignoreContent);
console.log(chalk_1.default.dim('ā Added .env to .gitignore'));
}
}
catch {
}
console.log(chalk_1.default.green('\nā
API key setup complete!'));
console.log(chalk_1.default.dim('\nYou can now use AI-powered tweet generation.'));
}
catch (error) {
logger_1.logger.error('Failed to save API key to .env file');
console.log(chalk_1.default.yellow('\nā ļø Could not save to .env file automatically'));
console.log(chalk_1.default.dim('\nAdd this to your .env file manually:'));
console.log(chalk_1.default.cyan(`OPENROUTER_API_KEY=${apiKey}`));
console.log(chalk_1.default.dim('\nOr add to your shell profile (~/.bashrc or ~/.zshrc):'));
console.log(chalk_1.default.cyan(`export OPENROUTER_API_KEY="${apiKey}"`));
}
});
//# sourceMappingURL=setup-api.js.map
;