npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

code2prompt

v1.0.96

Published

NodeJS Class for transforming a given codebase directory into an LLM prompt

Downloads

20

Readme

code2prompt

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.

Features

  • Recursively traverse your codebase directory.
  • Filter files by extension to include only relevant code files.
  • Automatically ignore specified directories or files using glob patterns.
  • Generate structured data including a source tree and file contents.
  • Utilize Handlebars templates for flexible output formatting.

Installation

Install code2prompt using npm:

npm install code2prompt

Or using yarn:

yarn add code2prompt

Usage

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' // needed for calling 'request', if not it's optional
    };
    const code2Prompt = new Code2Prompt(options);
    const prompt = await code2Prompt.generateContextPrompt();
    console.log(prompt);
    // make request to openAI
    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();
}();

Custom Templates

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}}

Contributing

Contributions to code2prompt are welcome! Please feel free to submit issues, pull requests, or suggest features.

License

This project is licensed under the MIT License - see the LICENSE file for details.