All files express.ts

85.71% Statements 18/21
60% Branches 6/10
100% Functions 1/1
88.89% Lines 16/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42        4x                   2x 2x   1x 1x 1x   1x 1x 1x 1x 1x 1x   1x       1x   1x     1x        
import { outputHTML, } from './';
import path from 'path';
import fs from 'fs';
 
const scopedEval = eval;
/**
 * Use JSONX for express view rendering
 * @param {string} filePath - path to jsonx express view 
 * @param {object} options - property used for express view {locals}
 * @param {object} options.__boundConfig - property used to bind this object for JSONX, can be used to add custom components
 * @param {string} [options.__DOCTYPE="<!DOCTYPE html>"] - html doctype string
 * @param {*} callback 
 */
export function __express(filePath:string, options:any, callback:any) {
  try {
    let jsonxModule = options.__jsonx;
    
    Eif (filePath) {
      const jsFile = fs.readFileSync(filePath).toString();
      jsonxModule = scopedEval(jsFile.toString());
    }
    const resources = Object.assign({}, options);
    delete resources.__boundConfig;
    delete resources.__DOCTYPE;
    delete resources.__jsonx;
    const context = Object.assign({}, options.__boundConfig);
    Iif (path.extname('.json')) context.useJSON = true;
  
    const jsonxRenderedString = outputHTML.call(context, {
      jsonx: jsonxModule,
      resources,
    });
    const template = `${options.__DOCTYPE || '<!DOCTYPE html>'}
${jsonxRenderedString}`;
    Eif (typeof callback === 'function') callback(null, template);
    else return template;
  } catch (e) {
    Eif (typeof callback === 'function') callback(e);
    else throw e;
  }
}