Skip to content
代码片段 群组 项目
template-engine.js 2.5 KB
更新 更旧
  • 了解如何忽略特定修订
  • /**
     *
     *    Copyright (c) 2020 Silicon Labs
     *
     *    Licensed under the Apache License, Version 2.0 (the "License");
     *    you may not use this file except in compliance with the License.
     *    You may obtain a copy of the License at
     *
     *        http://www.apache.org/licenses/LICENSE-2.0
     *
     *    Unless required by applicable law or agreed to in writing, software
     *    distributed under the License is distributed on an "AS IS" BASIS,
     *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     *    See the License for the specific language governing permissions and
     *    limitations under the License.
     */
    
    /**
     * @module JS API: generator logic
     */
    
    const fsPromise = require('fs').promises
    
    const promisedHandlebars = require('promised-handlebars')
    const handlebars = promisedHandlebars(require('handlebars'))
    
    var globalHelpersInitialized = false
    
    
    const templateCompileOptions = {
      noEscape: true,
    }
    
    const precompiledTemplates = {}
    
    
    function produceCompiledTemplate(singleTemplatePkg) {
    
      if (singleTemplatePkg.id in precompiledTemplates)
        return Promise.resolve(precompiledTemplates[singleTemplatePkg.id])
    
        return fsPromise.readFile(singleTemplatePkg.path, 'utf8').then((data) => {
    
          var template = handlebars.compile(data, templateCompileOptions)
    
          precompiledTemplates[singleTemplatePkg.id] = template
    
    
    /**
     * Given db connection, session and a single template package, produce the output.
     *
     * @param {*} db
     * @param {*} sessionId
     * @param {*} singlePkg
     * @returns Promise that resolves with the 'utf8' string that contains the generated content.
     */
    
    function produceContent(
      db,
      sessionId,
      singleTemplatePkg,
      genTemplateJsonPackageId
    ) {
      return produceCompiledTemplate(singleTemplatePkg).then((template) =>
    
          global: {
            db: db,
            sessionId: sessionId,
    
            genTemplatePackageId: genTemplateJsonPackageId,
    
    function loadHelper(path) {
      var helpers = require(path)
      for (const singleHelper in helpers) {
        handlebars.registerHelper(singleHelper, helpers[singleHelper])
      }
    }
    
    
    function initializeGlobalHelpers() {
      if (globalHelpersInitialized) return
    
      var includedHelpers = [
        './helper-zcl.js',
        './helper-zap.js',
        './helper-c.js',
        './helper-session.js',
      ]
    
    
      includedHelpers.forEach((element) => {
    
      globalHelpersInitialized = true
    
    exports.loadHelper = loadHelper