
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
@botanicastudios/code2prompt
Advanced tools
NodeJS Class for transforming a given codebase directory into an LLM prompt
Generate LLM prompts from your codebase easily with code2prompt
, a Node.js package designed to help you create structured documentation or coding challenge prompts directly from your project's source code.
Install code2prompt
using npm:
npm install code2prompt
Or using yarn:
yarn add code2prompt
Here's a simple example on how to use code2prompt to generate a prompt from your codebase:
const Code2Prompt = require("code2prompt");
!(async function () {
const options = {
path: "/path/to/your/codebase",
extensions: ["js", "ts"], // Specify the extensions to filter for
//template: 'templates/default.hbs',
template: "templates/write-readme.hbs",
ignore: ["**/node_modules/**"], // Specify patterns to ignore
OPENAI_KEY: "YOUR_OPENAI API KEY", // (optional) needed for 'request'
ANTHROPIC_KEY: "YOUR_ANTHROPIC API KEY", // (optional) needed for 'request'
GROQ_KEY: "YOUR_GROQ API KEY", // (optional) needed for 'request'
diff: false, // Set to true to enable diff mode
diffPath: "/path/to/comparison/codebase", // Path to compare against in diff mode
};
const code2Prompt = new Code2Prompt(options);
const prompt = await code2Prompt.generateContextPrompt();
console.log(prompt);
// make request to LLM API
setModelPreferences(["OPENAI", "ANTHROPIC", "GROQ"]); // define priority order for requests
const generateReadme = await code2Prompt.request(
"Generate a readme file from the given codebase",
z.object({
readme: z.string().describe("The generated contents of the readme file"),
})
);
// generatedReadme = { data: { readme: 'Generated readme.md content' }, usage:{ totalTokens, promptTokens, completionTokens } }
console.log("Generated readme.md", generateReadme.data.readme);
// some templates (like write-readme) contain a 'schema' md code block with the return schema for the prompt, so you can call it as is and it'll work
const generateReadme2 = await code2Prompt.request();
})();
Code2Prompt supports a diff mode where it compares two directories and generates diffs instead of showing complete file contents. This is useful for understanding what changed between two versions of a codebase.
const Code2Prompt = require("code2prompt");
!(async function () {
const options = {
path: "/path/to/your/current/codebase",
diffPath: "/path/to/your/previous/codebase",
diff: true, // Enable diff mode
extensions: ["js", "ts"],
ignore: ["**/node_modules/**"],
};
const code2Prompt = new Code2Prompt(options);
const prompt = await code2Prompt.generateContextPrompt();
console.log(prompt);
// You can also use this with LLM requests to ask about the changes
const analysis = await code2Prompt.request(
"Analyze the changes between these two codebases and explain their impact"
);
console.log(analysis.data);
})();
In diff mode, the generated prompt will:
This makes it easy for LLMs to understand the changes between different versions of your code.
code2prompt uses Handlebars templates to format the output. You can specify a custom template path in the options to use your own Handlebars template. Here's a basic template example:
Project Path:
{{absolute_code_path}}
Source Tree:
{{source_tree}}
{{#each files}}
{{#if code}}
`{{path}}`:
{{code}}
{{/if}}
{{/each}}
Contributions to code2prompt are welcome! Please feel free to submit issues, pull requests, or suggest features.
This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
NodeJS Class for transforming a given codebase directory into an LLM prompt
We found that @botanicastudios/code2prompt demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.