Skip to content

Commit

Permalink
Custom helper support and getDistinctForeignModels helper (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinneff authored Oct 4, 2018
1 parent 3748e1d commit debd5ab
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
8 changes: 1 addition & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gatewayapps/cradle-template-emitter",
"version": "0.1.7",
"version": "0.1.8",
"description": "Cradle emitter for reading Handlebars based template files",
"main": "./dist/index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/TemplateEmitter/ITemplateEmitterOptions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { CradleModel } from '@gatewayapps/cradle'
import { HelperDelegate } from 'handlebars'

export interface ITemplateEmitterOptions {
readonly sourcePath: string
readonly outputPath: string
readonly overwriteExisting: boolean
readonly mode: string
readonly languageType: string
readonly registerCustomHelpers?: (register: (name: string, fn: HelperDelegate) => void) => void
readonly shouldEmit: (meta: CradleModel) => boolean
readonly onFileEmitted: (path: string) => any
}
25 changes: 25 additions & 0 deletions src/TemplateEmitter/TemplateEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ export class TemplateEmitter implements ICradleEmitter {
return got
})

handlebars.registerHelper('getDistinctForeignModels', (context, options) => {
const got: any[] = []

if (!context || Object.keys(context).length === 0) {
return []
}

for (const c in context) {
const ref = context[c]
if (!ref.ForeignModel) {
continue
}
if (got.indexOf(ref.ForeignModel) === -1) {
got.push(ref.ForeignModel)
}
}

return got
})

handlebars.registerHelper('getObjectProps', (context, options) => {
const got: any = []

Expand All @@ -217,6 +237,11 @@ export class TemplateEmitter implements ICradleEmitter {

return got
})

if (this.config && this.config.options.registerCustomHelpers) {
const register = handlebars.registerHelper.bind(handlebars)
this.config.options.registerCustomHelpers(register)
}
}
public writeFileContents(filePath: string, content: string) {
const fileExists = fs.existsSync(filePath)
Expand Down

0 comments on commit debd5ab

Please sign in to comment.