@adamtools/apifinder
Version:
Finds all api endpoints of any nextjs project and adds them into README.md , makes api documentation easier
64 lines (52 loc) ⢠2.04 kB
JavaScript
const { program } = require('commander');
const chalk = require('chalk');
const path = require('path');
const { ApiFinder } = require('../index');
program
.name('apifinder')
.description('Discover and document Next.js API endpoints')
.version('1.0.0')
.option('-p, --path <path>', 'Path to Next.js project', process.cwd())
.option('-o, --output <file>', 'Output README file', 'README.md')
.option('--dry-run', 'Show endpoints without updating README')
.option('--json', 'Output endpoints as JSON')
.parse();
const options = program.opts();
async function main() {
try {
console.log(chalk.blue('š Analyzing Next.js project for API endpoints...'));
const analyzer = new ApiFinder(options.path);
const endpoints = await analyzer.analyze();
if (endpoints.length === 0) {
console.log(chalk.yellow('ā ļø No API endpoints found in the project.'));
return;
}
console.log(chalk.green(`ā
Found ${endpoints.length} API endpoint(s):`));
// Display found endpoints
endpoints.forEach(endpoint => {
const methods = endpoint.methods.map(m =>
m === 'GET' ? chalk.green(m) :
m === 'POST' ? chalk.blue(m) :
m === 'PUT' ? chalk.yellow(m) :
m === 'DELETE' ? chalk.red(m) : chalk.gray(m)
).join(', ');
console.log(` ${chalk.cyan(endpoint.route)} - ${methods} (${chalk.gray(endpoint.file)})`);
});
if (options.json) {
console.log('\n' + JSON.stringify(endpoints, null, 2));
return;
}
if (!options.dryRun) {
const readmePath = await analyzer.updateReadme(options.output);
console.log(chalk.green(`\nš Updated ${readmePath} with API endpoints table`));
} else {
console.log('\nš Markdown table preview:');
console.log(analyzer.generateMarkdownTable());
}
} catch (error) {
console.error(chalk.red('ā Error:'), error.message);
process.exit(1);
}
}
main();