Skip to content

gannochenko/generilla

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Contributors Stargazers Issues MIT License Language LinkedIn


πŸ’» + 🦍 = πŸ“ƒπŸ“ƒπŸ“ƒ

Generilla

Simple code generator for JS/JS applications

Report Bug Β· Request Feature

Table of Contents

About The Project

Generilla is a tool for running generilla code generators. See below how to make your own generator with Generilla.

Getting Started

Prerequisites

Installation

To avoid problems with permissions and sudo, it is possible to install the package locally and then add it's folder to PATH variable.

  1. Create ~/.node folder

    mkdir ~/.node
  2. Install the package

    yarn global add @generilla/cli --prefix ~/.node
  3. Add ~/.node folder to PATH

    export PATH=${PATH}:${HOME}/.node/bin
  4. Add the command above to ~/.bashrc (or ~/.bash_profile in case of MacOS)

  5. You should be able to run the generilla CLI command now

Upgrading

If you followed the way how the installation was done, then do upgrading as following:

yarn global upgrade @generilla/cli --prefix ~/.node

Manage generators

Creating a local generator

Generilla allows to bootstrap a skeleton generator to boost up the progress. Type

generilla scaffold

to create a new generator in the current folder.

You will see the following file structure:

generator-name/
β””-- template/  <-- file templates
└── index.js   <-- generator logic

Generator lifecycle

If you open index.js for editing, you will see a lifecycle of a generator. The typical lifecycle of a generator is the following:

  1. run async onBeforeExecution()
  2. ask questions returned by async getQuestions()
  3. refine answers by running async refineAnswers()
  4. install dependencies, if async getDependencies() returned a non-empty list
  5. install dev dependencies, if async getDevDependencies() returned a non-empty list
  6. run async onAfterExecution()

All of these methods are optional, use them only when you need them.

Templating mechanism

Inside of each file in template/ folder you may use ejs template syntax.

For example, let's say you have two questions: use_react and generator_name. Then in any file you can write something like
Hello, this is <%- generator_name %> speaking!.

Furthermore, you can inject these templates into the name of the files, by naming the files like this:
hello-[generator_name].js. All symbols, except [a-zA-Z0-9_-\.] will be omitted.

You can also conditionally enable or disable rendering of files or sub-folders, by giving names like this:
[?use_react]react/ or [?use_react][generator_name].saga.ts.

Provided tools

Each function in the lifecycle has access to the following properties of the generator object:

  • this.answers - list of answers provided by a user
  • this.originalAnswers - list of answers before running refineAnswers()
  • this.context - an object than contains the following data:
    • generatorPath - a folder where the generator is located
    • destinationPath - a folder where the output will be placed (by default it is a current folder)
  • this.util - an object that contains references to some tools:
    • inquirer - a reference to inquirer
    • execa - a reference to execa
    • ejs - a reference to ejs
    • caseFormatter - a reference to caseFormatter
    • makeTemplate() - an internal tool for processing folders other than template/
          const template = this.util.makeTemplate(absolutePathToOtherTemplateFolder);
          await template.copy(absoluteDestinationPath, answers);

Custom dependecies

Each generator may have package.json next to index.js file. In this case, after linking a generator generilla will automatically run

yarn

or

npm install

on it.

If you need more examples, check out these generators.

Linking a local generator

A generator created with generilla scaffold appears in the generator list automatically. If you have another generator made manually, you can add it by typing

generilla generator add <absolute-path-to-generator>

You can type afterwards

generilla list

to see if the generator is really there.

Linking a remote generator

If you have a generator that is hosted as a GitHUB repository, it is possible to link it by typing

generilla generator add https://github.com/joe/generator

If you have a mono-repository with several generators laying in sub-folders, the following also works:

generilla generator add https://github.com/joe/generators/tree/master/generator/foo/bar

Note that currently only master branch is supported.

You can also use this syntax:

generilla generator add https://github.com/joe/generators.git|/generator/foo/bar

or:

generilla generator add git@github.com:joe/generators.git|master|/awesome.generator/foo/bar

Whatever works for you better.

ℹ️ If there is a package.json file located next to the index.js file, then when the remote generator is linked, Generilla will try to install the dependencies automatically.

Upgrading a remote generator

To pull new changes made on the remotely linked generator, type

generilla generator update <wildcard>

Where <wildcard> is a wildcard matched against the generator name or id.

For instance, to update all remote generators, type

generilla generator update \*

or

generilla generator update gen-*

Removing a generator

To remove one or several generators of any type, use

generilla generator remove <wildcard>

Local generators will not be removed physically, they get un-symlinked.

Using a generator

One way or another, as soon as the generator is in the list, type

generilla

You should be able to see your generator in the list. After choosing a generator and answering some questions πŸ•΅οΈ the output will be generated in the current folder.

Logo

Type generilla -h to find out about all available commands.

Roadmap

  • Bugfixing :)
  • Better edge cases handling.
  • Allow generilla to run a generator without linking it, like this: generilla run /path/to/generator.
  • Enable making of self-executable generators, that are distributed as an npm package and export a CLI command that runs generilla run <generator>. This will allow using yarn create <generator-name>, which is quite cool.

See the open issues for a list of proposed features (and known issues).

Development

  1. Clone the repo
    git clone https://github.com/gannochenko/generilla.git
  2. Install NPM packages
    cd generilla;
    yarn;
    yarn run boostrap;
  3. Link the core package
    cd packages/core;
    yarn link;
    cd ../cli;
    yarn link @generilla/core;
  4. Go back to core and start watching for changes
    cd ../core;
    yarn run build:watch;
  5. In the other terminal go to cli and run yarn start
    cd ../cli;
    yarn start executes main cli binary. Use the development version as usual, for example yarn start list will be the same as generilla list and so on. The generated data will appear in the _output folder, and linked generators - in _generators folder.

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Built With

Contact

Sergei Gannochenko - Linkedin

Project Link: https://github.com/gannochenko/generilla