#!/usr/bin/env node const { execSync } = require('child_process'); const fs = require('fs'); const path = require('path'); const { OpenAI } = require('openai'); const yargs = require('yargs/yargs'); const { hideBin } = require('yargs/helpers'); // Initialize OpenAI client const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, }); // Parse command line arguments const argv = yargs(hideBin(process.argv)) .option('path', { alias: 'p', type: 'string', description: 'Path to the file or directory', demandOption: true, }) .option('staged', { alias: 's', type: 'boolean', description: 'Use staged changes instead of comparing against the base branch', default: false, }) .help() .alias('help', 'h').argv; // Function to find the nearest package.json and get the package name function getPackageName(filePath) { let dir = filePath; while (true) { const packageJsonPath = path.join(dir, 'package.json'); if (fs.existsSync(packageJsonPath)) { const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); if (packageJson.name) { return packageJson.name; } else { console.error(`Package name not found in ${packageJsonPath}`); process.exit(1); } } const parentDir = path.dirname(dir); if (parentDir === dir) { console.error('Reached root directory without finding package.json'); process.exit(1); } dir = parentDir; } } function sanitizeInput(input) { return input.replace(/[^a-zA-Z0-9_\-\/\.]/g, ''); } function getAllowedScopes() { const packagesDir = path.resolve(__dirname, 'packages'); const scopes = []; fs.readdirSync(packagesDir).forEach((dir) => { const projectJsonPath = path.join(packagesDir, dir, 'project.json'); if (fs.existsSync(projectJsonPath)) { const projectJson = JSON.parse(fs.readFileSync(projectJsonPath, 'utf8')); if (projectJson.name) { scopes.push(projectJson.name); } } }); return scopes; } async function generateCommitMessage(patch, packageName) { const allowedScopes = getAllowedScopes().join(', '); const prompt = `Generate a conventional commit message for the following git patch. RULES: Never author BREAKING CHANGE messages, they are not allowed. Body's lines must not be longer than 100 characters [body-max-line-length] Provide no explanation Focus on a statement of work of the changes: ${patch} Allowed scopes: ${allowedScopes} Please format the commit message as follows: ():