Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
created api ref generator script from parsing commands into md with the new -cmd2md option
0divf44d1f830da3
diff --git a/packages/cli/package.json b/packages/cli/package.jsonindex 45919f8ab..80e4a7356 100644--- a/packages/cli/package.json+++ b/packages/cli/package.json@@ -32,6 +32,7 @@"@types/inquirer": "^9.0.7","@types/node": "^18.18.6","@types/update-notifier": "6.0.5",+ "json2md": "^2.0.1","knip": "^2.33.4","npm-check-updates": "^16.14.6","tsup": "^7.2.0",diff --git a/packages/cli/scripts/generate_api_ref.sh b/packages/cli/scripts/generate_api_ref.shnew file mode 100755index 000000000..437148dda--- /dev/null+++ b/packages/cli/scripts/generate_api_ref.sh@@ -0,0 +1,21 @@+#!/usr/bin/env bash++set -euo pipefail++# This script generates the CLI API reference markdown files+## Run it in the `cli/` directory+mkdir -p api_ref++npx tsup && echo && node dist/index.js -cmd2md++# move to docs (for later use)+#for file in api_ref/*.md; do+# # Extract the filename without extension+# filename=$(basename "$file" .md)+#+# # Create the directory if it doesn't exist+# mkdir -p "../../apps/web/src/app/(docs)/docs/api-reference/cli/$filename"+#+# # Move the file to the new location and rename it to page.mdx+# mv "$file" "../../apps/web/src/app/(docs)/docs/api-reference/cli/$filename/page.mdx"+#donediff --git a/packages/cli/src/index.ts b/packages/cli/src/index.tsindex 56155e430..e9a9dca44 100644--- a/packages/cli/src/index.ts+++ b/packages/cli/src/index.ts@@ -2,10 +2,11 @@import * as updateNotifier from 'update-notifier'+import * as commander from 'commander'import * as packageJSON from '../package.json'import { program } from './commands'-import * as commander from 'commander'-import * as stripAnsi from 'strip-ansi'+import { commands2md } from './utils/commands2md'+import { parse } from 'path'export const pkg = packageJSON@@ -18,25 +19,9 @@ updateNotifierprogram.version(packageJSON.version, undefined, 'display E2B CLI version')- .addOption(new commander.Option('-cmd2json').hideHelp())- .on('option:-cmd2json', () => {- process.stdout.write(- JSON.stringify(- program.commands- .map((x: any) => ({- command: x.name(),- description: stripAnsi.default(x.description()),- options: x.options.map((y: any) => ({- flags: y.flags,- description: stripAnsi.default(y.description),- defaultValue: y.defaultValue,- })),- }))- .sort((row1: any, row2: any) =>- row1.name().localeCompare(row2.name()),- ),- ),- )+ .addOption(new commander.Option('-cmd2md').hideHelp())+ .on('option:-cmd2md', () => {+ commands2md(program.commands as any)process.exit(0)}).parse()diff --git a/packages/cli/src/utils/commands2md.ts b/packages/cli/src/utils/commands2md.tsnew file mode 100644index 000000000..96948d2d9--- /dev/null+++ b/packages/cli/src/utils/commands2md.ts@@ -0,0 +1,82 @@+import { Command } from 'commander'+import fs from 'fs'+import json2md from 'json2md'+import path from 'path'++/**+ * Converts command objects to Markdown documentation.+ * This function takes an array of command objects and generates a structured+ * Markdown document describing each command, its usage, options, and subcommands.+ * @returns A string containing the entire markdown documentation for all commands.+ */+export function commands2md(commands: Command[]): void {+ const outputDir = 'api_ref'+ if (!fs.existsSync(outputDir)) {+ fs.mkdirSync(outputDir, { recursive: true })+ }++ function commandToMd(+ command: any,+ parentName: string = ''+ ): [string, string] {+ const commandName = command.name() as string+ const fullName = parentName ? `${parentName} ${commandName}` : commandName++ const mdStructure = [+ { h1: `e2b ${fullName}` },+ { p: command.description() },+ { h2: 'Usage' },+ {+ code: {+ language: 'bash',+ content: `e2b ${fullName} ${command.usage()}`,+ },+ },+ ...(command.options.length > 0+ ? [+ { h2: 'Options' },+ {+ ul: command.options.map(+ (y: any) =>+ `\`${y.flags}: ${y.description} ${+ y.defaultValue !== undefined+ ? `[default: ${y.defaultValue}]`+ : ''+ }\``+ ),+ },+ ]+ : []),+ ]++ let mdContent = json2md(mdStructure)++ // Process subcommands+ command.commands.forEach((subcommand: any) => {+ const [, subMdContent] = commandToMd(subcommand, fullName)+ mdContent += subMdContent + '\n\n'+ })++ // Clean the mdContent+ mdContent = mdContent+ .replace(/</g, '<')+ .replace(/>/g, '>')+ .replace(/\[1m/g, '')+ .replace(/\[22m/g, '')++ return [fullName, mdContent]+ }++ commands.forEach((command: any) => {+ try {+ const [commandName, mdContent] = commandToMd(command)+ const fileName = `${commandName}.md`+ const filePath = path.join(outputDir, fileName)+ fs.writeFileSync(filePath, mdContent)+ console.log(`Generated documentation for ${commandName} at ${filePath}`)+ } catch (error) {+ console.error(`Error processing command: ${command.name()}`)+ console.error(error)+ }+ })+}