@gatewayapps/cradle-template-emitter
v0.5.3
Published
Cradle emitter for reading Handlebars based template files
Downloads
67
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 fileoutputPath[string]
: The path of the output. If the directory path does not exist, it will be created during executionoverwriteExisting[bool]
: defaults totrue
. Iftrue
, the file should be overridden on each execution. Iffalse
the file will not be overridden if it already exists.mode[string]
: defaults tomodel
. Ifschema
, the template emitter will provide the raw Cradle schema. Ifmodel
, 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
andsequelize
are the only accepted values.registerCustomHelpers[Function]
: Returns aregisterHelper
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 aCradleModel
that can be used to determine whether a Model should be emittedonFileEmitted[Function]
: Returns the file path as a string. A function that executes after a file is emitted. This can be useful for lintingonFilesEmitted[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}}