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

generathor

v1.0.11

Published

Generate files from templates and data sources automatically.

Downloads

99

Readme

Generathor

Generathor helps you automatically generate files from various data sources using templates. It saves time and prevents manual errors in repetitive tasks.

Why use Generathor?

Manual file creation can lead to mistakes. Generathor automates this process, ensuring accuracy and speeding up the workflow.

How does it work?

Generathor is built around two main components: Sources and Generators.

Sources

Sources provide the data for generating files. You can create custom sources by extending the Source class.

import { Source } from 'generathor';

class CustomSource extends Source {
  constructor() {
    super();
  }

  public async load(): Promise<void> {
    this.$items = await this.fetchData(); // Load your data here
  }

  public items(): Item[] {
    return this.$items;
  }
}

Generators

Generators use the data from sources to create files using handlebars templates. There are two types:

  • GeneratorForCollection: Creates one file for a collection of items.
  • GeneratorForItem: Creates individual files for each item.

Example for GeneratorForCollection:

new GeneratorForCollection({
  template: './template.handlebars',
  source: 'custom',
  file: './collection.txt',
  prepareItems(items) {
    return items.map(item => ({
      ...item,
      transformed: item.name.toUpperCase(), // Example transformation
    }));
  }
});

Configuration

| Variable | Required | Description | |----------------|----------|------------------------------------------------------------------------| | template | Yes | The template to use. | | source | Yes | The source to use to load the items. | | file | Yes | The file to write the output to. | | prepareItems | No | Callback to modify the items before using them to generate the output. |

This will generate a single file (collection.txt) for all the items in your source.

Example for GeneratorForItem:

new GeneratorForItem({
  template: './templates/item.handlebars',
  source: 'custom',
  directory: './output/items',
  fileName(item) {
    return `${item.id}.txt`;
  },
  prepareItems(items) {
    return items.map(item => ({
      ...item,
      transformed: item.name.toUpperCase(), // Example transformation
    }));
  }
});

Configuration

| Variable | Required | Description | |----------------|----------|------------------------------------------------------------------------| | template | Yes | The template to use. | | directory | Yes | The directory to write the output to. | | source | Yes | The source to use to load the items. | | fileName | Yes | Callback to get the file name for each item. | | prepareItems | No | Callback to modify the items before using them to generate the output. |

This will create one file per item in the source (e.g., 1.txt, 2.txt, etc.).

How to use Generathor

  1. Install Generathor:

    $ npm i -D generathor
  2. Configure by creating a generathor.config.js file at your project root:

    const path = require('path');
    const { ArraySource, GeneratorForItem, GeneratorForCollection } = require('generathor');
    
    module.exports = {
      sources: {
        db: new ArraySource([
          { table: 'table_1', columns: ['id', 'name'] },
          { table: 'table_2', columns: ['id', 'status'] },
        ]),
      },
      templates: {
        models: new GeneratorForCollection({
          template: './templates/export-models.handlebars',
          source: 'db',
          file: './result/index.js',
        }),
        'export-models': new GeneratorForItem({
          template: path.resolve('./templates/model.handlebars'),
          source: 'db',
          directory: path.resolve('./result'),
          fileName(item) {
            return item.table + '.js';
          },
        }),
      },
    };
  3. Run:

    $ generathor

    To see more options:

    $ generathor --help

Ecosystem

Explore related packages:

TODO

  • [ ] Add objection js generators - WIP
  • [ ] Add support for the overwriteFiles configuration option.
  • [ ] Add more sources (API, CSV, etc.)