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

sneed

v0.0.7

Published

Simple utility for creating versionable scaffolds from custom EJS templates

Downloads

9

Readme

Sneed

NPM npm

Sneed is a simple scaffolding cli tool for developers, to generate source files based on templates they define and can keep in source control in their project.

Getting started

Install

Install sneed globally

$ npm install -g sneed

or locally & invoke by npm scripts

Initialize environment

Sneed's init command will generate required config & template folder for you

$ sneed init

Define commands

Open created .sneedrc.js in editor & create scaffolding/editing commands as you wish.

See configuration section below

Run scaffolding

Once sneed is configured, you can execute your commands that will generate files based on your definition

$ sneed <your-command> --var1 Value1 --var2 Value2 ...

Configuring scaffolding

Scaffolding is primary feature of Sneed that allows you to generate source code files based on your templates to your desired destination. Sneed allows configuration as .sneedrc, .sneedrc.json, .sneedrc.js, .sneedrc.yaml

Example .sneedrc.js

module.exports = {
  templateFolder: 'templates',
  commands: {
    // name of command (used in cli)
    ScaffoldHelloWorldFile: {
      scaffolds: [
        {
          // your template in "templates" folder
          template: 'hello-world.ejs',
          // destination file path
          target: 'src/hello-world.js'
        }
      ],
      // not important for scaffolding
      edits: [],
      variables: {
        // cli '--greet <value>' argument will passed into template
        greet: {}
      }
    }
  }
}

templates/hello-world.ejs

console.log('<%= greet %>')

Now you can execute scaffolding your command ScaffoldHelloWorldFile to generate src/hello-world.js file

$ sneed ScaffoldHelloWorldFile --greet "Hello world"

which will generate

src/hello-world.js

console.log('Hello world')

For more advanced examples check examples // TODO

Configuring editing

File editing is secondary feature of Sneed and can be used to automatically modify existing source files to insert custom content.

This feature is useful for example if you want Sneed to automatically inject import statements of your newly generated components.

Lets have following: .sneedrc.js

module.exports = {
  templateFolder: 'templates',
  commands: {
    ScaffoldComponentAndRegister: {
      scaffolds: [],
      edits: [
        {
          target: 'src/register-components.js',
          mark: '// SNEED INSERT HERE',
          template: 'register-component.ejs',
          editType: 'insertAfter'
        }
      ],
      variables: {
        name: {}
      }
    }
  }
}

This will be rendered after our "mark"

templates/register-component.ejs

register.component(require('./<%= name %>'))

This is actual source file that we edit

src/register-components.js

register.component(require('./digging'))
register.component(require('./sewing'))
// SNEED INSERT HERE
register.component(require('./brewing'))

Now we execute

$ sneed ScaffoldComponentAndRegister --name building

and Sneed will insert rendered code block right after "// SNEED INSERT HERE" marking resulting in

register.component(require('./digging'))
register.component(require('./sewing'))
// SNEED INSERT HERE
register.component(require('./building'))
register.component(require('./brewing'))

Note that you need to place spaces & newlines into your template manually to correctly insert stuff

Variables

Sneed provides a way how to pass custom data (e.g. component names, switches...) into rendering engine. This way you can customize how your template behaves.

Variables without "default" option are mandatory, and must be specified as CLI option.

Example .sneedrc.js

module.exports = {
  templateFolder: 'templates',
  commands: {
    VariableTest: {
      scaffolds: [
        {
          template: 'vars.ejs',
          target: 'src/vars.txt'
        }
      ],
      edits: [],
      variables: {
        name: {},
        surname: {},
        greet: { default: false }
      }
    }
  }
}

templates/vars.ejs

<% if (greet == 'true') { %>
Greetings fellow citizen,
<% } %>
you have new message <%= name %> <%= surname %>!

Executing comman without --greet switch

$ sneed VariableTest --name John --surname Wick

generates

you have new message John Wick!

Executing comman with --greet switch

$ sneed VariableTest --name John --surname Wick --greet

generates

Greetings fellow citizen,

you have new message John Wick!

Templating engine

Sneed uses EJS templating engine with all its features.

It's recommended to use <%- value %> tags for rendering custom values, as it doesn't escape html chars which might be in your values and would likely result in invalid source code.

<%- %> render unescaped value (<>hello<>):

<%- '<>hello<>' %>

<%= %> render escaped value (<>hello<>):

<%= '<>hello<>' %>

<% %> are used for control flow statements:

<% if (value === 'hello') { %> Hello there <% } %>

<%- include(templateFile, variables) %> for including other templates

<%- include('ClassTemplate', { class: 'Chuck' }) %>

(including is relative to configs "templatesFolder")

Check EJS docs for more!

Path templating

Sneed also templates your paths in config! Temlatable config options are template, target and mark

Example .sneedrc.js

module.exports = {
  templateFolder: 'templates',
  commands: {
    TemplatedPath: {
      scaffolds: [
        {
          template: 'example-template.ejs',
          target: '<%= folder %>/example.js'
        }
      ],
      edits: [],
      variables: {
        folder: {}
      }
    }
  }
}

Allows you to choose denstination path by "--folder" variable

Helpers - Case transformation

Sneed provides set of function from package change-case that can be accessed in templates under casing property. With these functions you can easily reuse identifier names in different casing conventions across your template.

Available functions are: camelCase, capitalCase, constantCase, dotCase, headerCase, noCase, paramCase, pascalCase, pathCase, sentenceCase, snakeCase

see change-case for more

Example template.ejs

class <%= casing.pascalCase(name) %> { }
var <%= casing.camelCase(name) %> = 42

Passing --name hello-world will render

class HelloWorld {}
var helloWorld = 42

Helpers - Paths

Sneed provides path helper function that can be accessed in templates under path property. The functions are from native path package.

Example template.ejs

<%= path.basename('/foo/bar/index.html') %>

will render

index.html

Helpers - Lodash

Sneed provides utility swiss knife package Lodash accessible under property "_" which contains plethora of function for collection and object manipulation, transformation etc...

Examples

Changelog

0.0.7 (28.08.2021)

Version bump of dependencies