CLI for quickly creating boilerplate files based on templates you provide.
Switch branches/tags
greenkeeper/colors-1.2.0 greenkeeper/colors-1.2.1 greenkeeper/colors-1.2.2 greenkeeper/colors-1.2.3 greenkeeper/colors-1.2.4 greenkeeper/colors-1.3.0 greenkeeper/colors-1.3.1 greenkeeper/colors-1.3.2 greenkeeper/colors-1.3.3 greenkeeper/eslint-4.0.0 greenkeeper/eslint-4.1.0 greenkeeper/eslint-4.1.1 greenkeeper/eslint-4.2.0 greenkeeper/eslint-4.3.0 greenkeeper/eslint-4.4.0 greenkeeper/eslint-4.4.1 greenkeeper/eslint-4.5.0 greenkeeper/eslint-4.6.0 greenkeeper/eslint-4.6.1 greenkeeper/eslint-4.7.0 greenkeeper/eslint-4.7.1 greenkeeper/eslint-4.7.2 greenkeeper/eslint-4.8.0 greenkeeper/eslint-4.9.0 greenkeeper/eslint-4.10.0 greenkeeper/eslint-4.11.0 greenkeeper/eslint-4.12.0 greenkeeper/eslint-4.12.1 greenkeeper/eslint-4.13.0 greenkeeper/eslint-4.13.1 greenkeeper/eslint-4.14.0 greenkeeper/eslint-4.15.0 greenkeeper/eslint-4.16.0 greenkeeper/eslint-4.17.0 greenkeeper/eslint-4.18.0 greenkeeper/eslint-4.18.1 greenkeeper/eslint-4.18.2 greenkeeper/eslint-4.19.0 greenkeeper/eslint-4.19.1 greenkeeper/eslint-5.0.0 greenkeeper/eslint-5.0.1 greenkeeper/eslint-plugin-jest-21.0.0 greenkeeper/eslint-plugin-jest-21.0.2 greenkeeper/eslint-plugin-jest-21.1.0 greenkeeper/eslint-plugin-jest-21.2.0 greenkeeper/eslint-plugin-jest-21.3.0 greenkeeper/eslint-plugin-jest-21.3.1 greenkeeper/eslint-plugin-jest-21.3.2 greenkeeper/eslint-plugin-jest-21.4.0 greenkeeper/eslint-plugin-jest-21.4.1 greenkeeper/eslint-plugin-jest-21.4.2 greenkeeper/eslint-plugin-jest-21.4.3 greenkeeper/eslint-plugin-jest-21.5.0 greenkeeper/eslint-plugin-jest-21.6.0 greenkeeper/eslint-plugin-jest-21.6.1 greenkeeper/eslint-plugin-jest-21.6.2 greenkeeper/eslint-plugin-jest-21.7.0 greenkeeper/eslint-plugin-jest-21.7.1 greenkeeper/eslint-plugin-jest-21.8.0 greenkeeper/eslint-plugin-jest-21.9.0 greenkeeper/eslint-plugin-jest-21.10.0 greenkeeper/eslint-plugin-jest-21.11.0 greenkeeper/eslint-plugin-jest-21.12.0 greenkeeper/eslint-plugin-jest-21.12.1 greenkeeper/eslint-plugin-jest-21.12.2 greenkeeper/eslint-plugin-jest-21.12.3 greenkeeper/eslint-plugin-jest-21.13.0 greenkeeper/eslint-plugin-jest-21.14.0 greenkeeper/eslint-plugin-jest-21.14.1 greenkeeper/eslint-plugin-jest-21.15.0 greenkeeper/eslint-plugin-jest-21.15.1 greenkeeper/eslint-plugin-jest-21.15.2 greenkeeper/eslint-plugin-jest-21.15.3 greenkeeper/eslint-plugin-jest-21.16.0 greenkeeper/eslint-plugin-jest-21.16.1 greenkeeper/eslint-plugin-jest-21.17.0 greenkeeper/eslint-plugin-jest-21.18.0 greenkeeper/eslint-plugin-jest-21.19.0 greenkeeper/eslint-plugin-jest-21.20.0 greenkeeper/eslint-plugin-jest-21.20.1 greenkeeper/eslint-plugin-jest-21.20.2 greenkeeper/eslint-plugin-jest-21.21.0 greenkeeper/eslint-plugin-jest-21.22.0 greenkeeper/eslint-plugin-jest-21.22.1 greenkeeper/eslint-plugin-jest-21.23.0 greenkeeper/eslint-plugin-jest-21.24.0 greenkeeper/eslint-plugin-jest-21.24.1 greenkeeper/eslint-plugin-jest-21.25.0 greenkeeper/eslint-plugin-jest-21.25.1 greenkeeper/eslint-plugin-jest-21.26.0 greenkeeper/eslint-plugin-jest-21.26.1 greenkeeper/eslint-plugin-jest-21.26.2 greenkeeper/eslint-plugin-jest-21.27.0 greenkeeper/eslint-plugin-jest-21.27.1 greenkeeper/eslint-plugin-jest-21.27.2 greenkeeper/eslint-plugin-jest-22.0.0 greenkeeper/eslint-plugin-jest-22.0.1 greenkeeper/eslint-plugin-jest-22.1.0 greenkeeper/eslint-plugin-jest-22.1.1 greenkeeper/eslint-plugin-jest-22.1.2 greenkeeper/fs-extra-4.0.0 greenkeeper/fs-extra-4.0.1 greenkeeper/fs-extra-4.0.2 greenkeeper/fs-extra-4.0.3 greenkeeper/fs-extra-5.0.0 greenkeeper/fs-extra-6.0.0 greenkeeper/fs-extra-6.0.1 greenkeeper/fs-extra-7.0.0 greenkeeper/fs-extra-7.0.1 greenkeeper/inquirer-3.1.1 greenkeeper/inquirer-3.2.0 greenkeeper/inquirer-3.2.1 greenkeeper/inquirer-3.2.2 greenkeeper/inquirer-3.2.3 greenkeeper/inquirer-3.3.0 greenkeeper/inquirer-4.0.0 greenkeeper/inquirer-4.0.1 greenkeeper/inquirer-4.0.2 greenkeeper/inquirer-5.0.0 greenkeeper/inquirer-5.0.1 greenkeeper/inquirer-5.1.0 greenkeeper/inquirer-5.2.0 greenkeeper/inquirer-6.0.0 greenkeeper/inquirer-6.1.0 greenkeeper/inquirer-6.2.0 greenkeeper/inquirer-6.2.1 greenkeeper/jest-21.0.1 greenkeeper/jest-21.0.2 greenkeeper/jest-21.1.0 greenkeeper/jest-21.2.0 greenkeeper/jest-21.2.1 greenkeeper/jest-22.0.0 greenkeeper/jest-22.0.1 greenkeeper/jest-22.0.2 greenkeeper/jest-22.0.3 greenkeeper/jest-22.0.4 greenkeeper/jest-22.0.5 greenkeeper/jest-22.0.6 greenkeeper/jest-22.1.0 greenkeeper/jest-22.1.1 greenkeeper/jest-22.1.2 greenkeeper/jest-22.1.3 greenkeeper/jest-22.1.4 master new-gifs update-documentation
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
images
playground
src
.eslintignore
.eslintrc
.gitignore
circle.yml
package.json
readme.md
yarn.lock

readme.md

cookiecutter 🍪 ✂️

CircleCI Greenkeeper badge npm version

A CLI for creating boilerplate files/folders based on templates you provide. Like Yeoman or Python's Cookiecutter but much, much simpler.

Show me:

Example with Error

It is designed for existing projects where you want an easy way to create boilerplate files - instead of having to copy paste an existing file & then remember to make the necessary changes.

A good use-case for cookiecutter is, for example, creating a new Redux connected React component within a project. Not hard... but also not fun.

Cookiecutter supports:

  • Multiple templates with the ability to pick which template to use via the CLI.
  • Multiple fields per template with the ability to specify them from the CLI.
  • Custom validation & error messages for fields.
  • Any type of file or folder structure.

Cookiecutter doesn't support:

  • Conditionals or any other logic in templates. You can however achieve similar things by having multiple templates).
  • Bootstrapping a project from scratch (i.e I have no code but need to set a project up)
  • Adding code to existing files
  • Lots of other things Yeoman does support. If your use-case is complex, cookiecutter might not cut it.

Quick Setup

First npm install --save-dev cookiecutter. You can also install this globally if you prefer.

The add the following to your package.json (no need to do this if you are using yarn):

"scripts": {
    "cookiecutter": "cookiecutter"
}

You can now start creating your template. For example if you create a 'templates' folder in your root and add the following template:

// templates/COMPONENT_NAME.js
import React from 'react';

class COMPONENT_NAME extends React.Component {
    render() {
        return <div>Hello World</div>;
    }
}

export default COMPONENT_NAME;

You can now configure Cookiecutter to replace the string COMPONENT_NAME like so by creating a file in the root of your project called cookiecutter.config.js. Cookiecutter will always look in your root for a file called cookiecutter.config.js.

// cookiecutter.config.js
module.exports = [
    {
        name: "Normal React Component",
        templatePath: "templates/COMPONENT_NAME.js",
        outputPath: "src/components/",
        fields: [
            {
                templateVariable: 'COMPONENT_NAME',
                question: "What is the component's name?",
            }
        ]
    }
];

If you now run npm run cookiecutter you will be prompted to pick which template you'd like to use. After which you will be asked to answer each field's question. It should look something like this:

Example from Tutorial

That's it. Cookiecutter now created the following file:

    - src
      - components
+       - ExampleComponent.js
        - OtherComponent.js
        - Etc.js

Cookiecutter will never overwrite any existing files.

Validating fields / custom validation

To do this you can add isValid and a custom errorMessage to a fields configuration. Using the example above you could, for example, ensure component names follow a naming convention.

// in cookiecutter.config.js
module.exports = [
    {
       name: "Normal React Component",
        templatePath: "templates/COMPONENT_NAME/index.js",
        outputPath: "src/components/",
        fields: [
            {
                templateVariable: 'COMPONENT_NAME',
                question: "What is the component's name?",
+               errorMessage: 'A component must be in PascalCase and can only include letters.',
+               isValid(value) {
+                   return !!value.match(/^[A-Z][a-z]+(?:[A-Z][a-z]+)*$/g);
+               }
            }
        ]
    }
];

Multiple templates

You can configure multiple templates and Cookiecutter will let you pick which template to use. The template's 'name' is what will be used in the CLI when asking which template to use.

You can specify multiple templates like so:

// in cookiecutter.config.js
module.exports = [
    {
       // Configuration for the 1st template
    },
    {
       // Configuration for the 2nd template
    },
];

Templates with folders

If you specify a folder as a template, then the folder will be created in the output directory as well. This is useful if you have a folder structure such as:

- ComponentName
    - index.js
    - styles.css

Cookiecutter will recursively copy all files within your templates folder and replace any occurrences of your specified fields.

If you don't want this behavior, specify a file as your template's templatePath.

Custom config location

If you would like your config to be somewhere else you can use the -c=path/to/your/config.js flag when you call cookiecutter.

Contributing

After cloning the repo yarn install.

You can play with the cli in the 'playground' folder. To do this, cd into it and run . run.sh. This is like running the cli once it's published and on npm but allows you to work on it locally.

To run tests run yarn test. To lint files (this will also happen in CircleCI when you open a PR): yarn lint

Roadmap/Plans

  • Offer a set of helpers for validation user input. Esp. for common 'cases' such as snake_case, PascalCase etc.
  • Allow users to specify a custom path for their config.
  • Offer the ability for people to show a message that is only shown AFTER a template is successfully rendered. This could be helpful for things such as help-text such as: 'You can import this component like so: ...'