build-in-public-bot
Version:
AI-powered CLI bot for automating build-in-public tweets with code screenshots
155 lines (152 loc) • 6.6 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.autoCommand = void 0;
const commander_1 = require("commander");
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const config_1 = require("../services/config");
const errors_1 = require("../utils/errors");
const GIT_HOOK_TEMPLATE = `#!/bin/sh
# Build in Public Bot - Post-commit hook
# Only run if BIP_AUTO_TWEET is set to true
if [ "$BIP_AUTO_TWEET" = "true" ]; then
# Run in background to not block git
(
# Wait a moment for the commit to be fully processed
sleep 2
# Generate and save a tweet draft about the commit
npx build-in-public summary --save > /dev/null 2>&1
echo "Build in Public Bot: Tweet draft saved for your commit!"
) &
fi
`;
exports.autoCommand = new commander_1.Command('auto')
.description('Set up automatic tweet generation for git commits')
.option('--enable', 'Enable auto-tweet generation')
.option('--disable', 'Disable auto-tweet generation')
.option('--status', 'Show current auto-tweet status')
.action(async (options) => {
try {
const configService = config_1.ConfigService.getInstance();
const gitDir = path.join(process.cwd(), '.git');
try {
await fs.access(gitDir);
}
catch {
console.log(chalk_1.default.red('❌ Not a git repository'));
console.log(chalk_1.default.dim('Run this command in the root of your git project'));
return;
}
const hookPath = path.join(gitDir, 'hooks', 'post-commit');
if (options.status) {
let hookExists = false;
let hookEnabled = false;
try {
await fs.access(hookPath);
hookExists = true;
const content = await fs.readFile(hookPath, 'utf-8');
hookEnabled = content.includes('Build in Public Bot');
}
catch {
}
console.log(chalk_1.default.cyan('🤖 Auto-tweet Status:'));
console.log(` Hook installed: ${hookExists ? chalk_1.default.green('Yes') : chalk_1.default.red('No')}`);
console.log(` Hook active: ${hookEnabled ? chalk_1.default.green('Yes') : chalk_1.default.red('No')}`);
console.log(` Environment variable: ${process.env.BIP_AUTO_TWEET === 'true' ? chalk_1.default.green('Enabled') : chalk_1.default.yellow('Disabled')}`);
if (hookExists && hookEnabled) {
console.log('\n' + chalk_1.default.dim('To enable auto-tweets, set: export BIP_AUTO_TWEET=true'));
console.log(chalk_1.default.dim('To disable temporarily: export BIP_AUTO_TWEET=false'));
}
return;
}
if (options.disable) {
const spinner = (0, ora_1.default)('Disabling auto-tweet generation...').start();
try {
const content = await fs.readFile(hookPath, 'utf-8');
if (content.includes('Build in Public Bot')) {
await fs.unlink(hookPath);
spinner.succeed('Auto-tweet generation disabled');
}
else {
spinner.warn('Auto-tweet hook was not installed');
}
}
catch {
spinner.warn('No git hook found');
}
return;
}
const spinner = (0, ora_1.default)('Setting up auto-tweet generation...').start();
let existingContent = '';
try {
existingContent = await fs.readFile(hookPath, 'utf-8');
if (existingContent.includes('Build in Public Bot')) {
spinner.succeed('Auto-tweet generation is already enabled');
console.log(chalk_1.default.dim('\nSet BIP_AUTO_TWEET=true in your environment to activate'));
return;
}
}
catch {
}
if (existingContent) {
const updatedContent = existingContent + '\n\n' + GIT_HOOK_TEMPLATE;
await fs.writeFile(hookPath, updatedContent);
}
else {
await fs.writeFile(hookPath, GIT_HOOK_TEMPLATE);
}
await fs.chmod(hookPath, '755');
spinner.succeed('Auto-tweet generation enabled!');
console.log('\n' + chalk_1.default.green('✅ Git hook installed successfully'));
console.log('\nTo activate auto-tweets:');
console.log(chalk_1.default.white(' export BIP_AUTO_TWEET=true'));
console.log('\nTo temporarily disable:');
console.log(chalk_1.default.white(' export BIP_AUTO_TWEET=false'));
console.log('\n' + chalk_1.default.dim('A tweet draft will be saved after each commit when enabled'));
await configService.update({
autoTweet: { enabled: true }
});
}
catch (error) {
(0, errors_1.handleError)(error);
}
});
//# sourceMappingURL=auto.js.map
;