1.0.11 • Published 30 days ago

generathor v1.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
30 days ago

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

VariableRequiredDescription
templateYesThe template to use.
sourceYesThe source to use to load the items.
fileYesThe file to write the output to.
prepareItemsNoCallback 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

VariableRequiredDescription
templateYesThe template to use.
directoryYesThe directory to write the output to.
sourceYesThe source to use to load the items.
fileNameYesCallback to get the file name for each item.
prepareItemsNoCallback 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.)
1.0.11

30 days ago

1.0.9

1 year ago

1.0.10

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

2 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago