Code Monkey home page Code Monkey logo

snip's Introduction

Snip

A recipe based file generator.

Usage

Snip requires predefined templates or recipes to work. All files within ~/.config/snip will be scanned and added as available templates and recipes that snip can use.

Templates are simply regular files that can contain variables escaped by placing them within pair curly brackets, for example:

Hello {{ name }}

These files can then be generated by interactively providing the required variables or programatically by using recipes.

Recipes are script files that can do anything for example perform system commands, collect user input and anything else that can be done within scripts. When the recipe is a shell script (such as zsh, bash, etc.) some convenience functions are provided for easy template handling and user interaction.
A full list of predefined commands is provided bellow.

Snip can be invoked directly and will present a list of available recipes:

$ snip

Snip can receive various arguments depending on the recipe used, for example the React scripts provided with the package can accept a relative or absolute path for component generation:

$ snip src/ExampleComponent

Commands

When the recipe is defined as a shell script the following predefined commands can be used:

resolve

resolve FILENAME

Resolve the full path of FILENAME by searching in the following directories:

  • the working directory
  • inside of ~/.config/snip

The full file path is returned or the command will exit with exit code 1.

variables

variables TEMPLATE

Collect all variables from a template and return a JSON object with the variable names as keys and empty strings as values.

data=$(variables 'react/component/component.tsx')

In this example data might be:

{
  "foo": "bar"
}

merge

merge VARS1 VARS2

Merge two JSON objects and return the resulting JSON object. If a key exists in both objects the latter one will take precedence.

merged=$(merge $data1 $data2)

set-value

set-value VARS value

Set a value for a given key in a JSON object. If the key did not exist it will be added. The original data will not be altered, a modified copy is returned instead.

copy=$(set-value "$data" "name" "Hello")

get-value

get-value VARS value

Get the value of a given key from a JSON object.

value=$(get-value $data key)

edit

edit VARS

Edit the data interactively in $EDITOR.

data=$(edit $data)

render

render TEMPLATE

Render the given template by printing it to stdout with the substituted variables. The output should usually be redirected to the output file.

render 'template.js' $data > $output.js

Templates

Templates are files that can contain variables escaped by double curly braces for example {{ variable }}.

An example of a template is:

// hello.js
console.log("hello {{ name }}");

Recipes

Recipes can be any executable file that aims to programatically generate files and possibly interact with the user, read templates or execute other system commands.

Typically shell scripts are used and some commands are provided to deal with common tasks such as substituting variables in a template. A full list of commands can be found in the above section.

Using predefined recipes

In order to use predefined recipes from this repository simply find the files/recipes you want to use and copy them to ~/.config/snip.
For example:

$ cp -r recipes/react ~/.config/snip

Prerequisites

The following dependencies must be installed:

Installation

Simply symlink the snip.zsh script to a location in your path.

For example:

$ sudo ln -s snip.zsh /usr/local/bin/snip

Snip will utilize the $EDITOR environmental variable for interactive file editing, in order to use a specific editor simply set the variable accordingly.

Authors

shiro [email protected]

Licesne

MIT

snip's People

Contributors

shiro avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.