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

@gatewayapps/cradle-template-emitter

v0.5.3

Published

Cradle emitter for reading Handlebars based template files

Downloads

27

Readme

Cradle Template Emitter - A Cradle Emitter

What is Cradle Template Emitter?

The Cradle Template Emitter is an Emitter plugin for Cradle that accepts the Cradle schema and uses Handlebars templates in order to output a desired result. This can be anything from code files to documentation, from unit tests to configuration files.

Getting Started
Configuration
Templates
Executing the emitter
Emitter options
Provided Handlebars Helper Functions

Getting Started

To get started, first install Cradle in your local project folder: npm i --save-dev @gatewayapps/cradle.
Next, install the Cradle Template Emitter: npm i --save-dev @gatewayapps/cradle-template-emitter.

Configuration

Please see the Cradle ReadMe for general information about the Cradle configuration file.

To add an emitter, inside the configuration file:

const emitterOpts = [
 new cradle.EmitterOptions(
    'newEmitter',
    '@gatewayapps/cradle-template-emitter',
    {
      sourcePath: './cradle/templates/template1.handlebars',
      outputPath: './src/server/output.ts',
      overwriteExisting: true,
    },
    console
  ),
]

Then, at the end of the configuration file, make sure emitterOpts is passed in as the second argument for initializing CradleConfig:

module.exports = new cradle.CradleConfig(loaderOptions, emitterOpts)

Templates

The template emitter uses Handlebars for the template engine. Refer to the Handlebars website for questions regarding syntax.

Executing the emitter

Run the emitter using the emit command from Cradle: npx cradle emit -c cradle.config.js -e newEmitter. The -e argument can be omitted if multiple emitters are configured and should be run simultaneously

Emitter options

  • sourcePath[string]: The path of the handlebars template file
  • outputPath[string]: The path of the output. If the directory path does not exist, it will be created during execution
  • overwriteExisting[bool]: defaults to true. If true, the file should be overridden on each execution. If false the file will not be overridden if it already exists.
  • mode[string]: defaults to model. If schema, the template emitter will provide the raw Cradle schema. If model, the template emitter will provide a model based schema. schema is particularly useful if writing to a single file. model is more useful if writing separate files per Model.
  • languageType[string]: Specify the language type to output. Currently, mongoose, js, ts and sequelize are the only accepted values.
  • registerCustomHelpers[Function]: Returns a registerHelper which accepts a name and a Handlebars function. A function that can be used to add custom Handlebars based helpers to aid in writing templates.
  • shouldEmit[Function]: A function that returns a CradleModel that can be used to determine whether a Model should be emitted
  • onFileEmitted[Function]: Returns the file path as a string. A function that executes after a file is emitted. This can be useful for linting
  • onFilesEmitted[Function]: Returns an array of file paths as a string. A function that executes after all files have been emitted. This can be useful for linting

Provided Handlebars Helper Functions

The template emitter provides a set of custom helper functions out of the box. As stated above, custom helper methods can be defined in the Cradle configuration file. See below for the helper methods and a simple example.

  • ifEquals: {{#ifEquals 1 1}}Equals!{{/ifEquals}}
  • ifNotEquals: {{#ifNotEquals 1 2}}Does not equal!{{/ifNotEquals}}
  • toLowerCase: {{toLowerCase "HELLO WORLD"}} // outputs hello world
  • isArray: {{#isArray TypeName}}I'm an array!{{/isArray}}
  • isNotArray: {{#isNotArray TypeName}}I'm not an array!{{/isNotArray}}
  • isBaseDataType: {{#isBaseDataType prop}}I'm a base/primitive data type{{/isBaseDataType}}
  • isObject: {{#isObject prop}}I'm an object!{{/isObject}}
  • getDistinctObjects: {{#each (getDistinctObjects Properties) as |p|}}{{p.TypeName}}{{/each}}
  • getReferences: {{#each (getReferences References) as |ref|}}{{ref.Name}}{{/each}}
  • getDistinctForeignModels: {{#each (getDistinctForeignModels References) as |ref|}}{{ref.Name}}{{/each}}