# Code coverage report for lib/precompiler.js

## Statements: 97.53% (79 / 81) Branches: 94.59% (70 / 74) Functions: 100% (6 / 6) Lines: 97.5% (78 / 80) Ignored: none

All files » lib/ » precompiler.js

    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
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    1

    1
    54
    3
    3

    51
    51
    3

    48
    51
    51

    3

    45
    3

    42
    6

    36
    36
    3

    36
    3
    3

    36
    36

    36
    36
    27
    12
    15
    3

    12

    27
    27
    27
    27
    27

    1
    57

    57
    15
    45

    45
    21

    42

    42

    42

    42
    3

    42
    21
    21
    21

    42

    42
    9
    33
    9
    3

    9

    24
    3

    24

    36
    36

    36
    27
    12
    6
    3

    3

    12
    15
    12

    36

    36
    3

    36

    36

    var fs = require('fs'),
        Handlebars = require('./index'),
        basename = require('path').basename,
        uglify = require('uglify-js');

    module.exports.cli = function(opts) {
      if (opts.version) {
        console.log(Handlebars.VERSION);
        return;
      }

      var template = [0];
      if (!opts.templates.length) {
        throw new Handlebars.Exception('Must define at least one template or directory.');
      }

      opts.templates.forEach(function(template) {
        try {
          fs.statSync(template);
        } catch (err) {
          throw new Handlebars.Exception('Unable to open template file "' + template + '"');
        }
      });

      if (opts.simple && opts.min) {
        throw new Handlebars.Exception('Unable to minimze simple output');
      }
      if (opts.simple && (opts.templates.length !== 1 || fs.statSync(opts.templates[0]).isDirectory())) {
        throw new Handlebars.Exception('Unable to output multiple templates in simple mode');
      }

      // Convert the known list into a hash
      var known = {};
      if (opts.known && !Array.isArray(opts.known)) {
        opts.known = [opts.known];
      }
      if (opts.known) {
        for (var i = 0, len = opts.known.length; i < len; i++) {
          known[opts.known[i]] = true;
        }
      }

      // Build file extension pattern
      var extension = opts.extension.replace(/[\\^$*+?.():=!|{}\-\[\]]/g, function(arg) { return '\\' + arg; });
      extension = new RegExp('\\.' + extension + '$');

      var output = [];
      if (!opts.simple) {
        if (opts.amd) {
          output.push('define([\'' + opts.handlebarPath + 'handlebars.runtime\'], function(Handlebars) {\n  Handlebars = Handlebars["default"];');
        } else if (opts.commonjs) {
          output.push('var Handlebars = require("' + opts.commonjs + '");');
        } else {
          output.push('(function() {\n');
        }
        output.push('  var template = Handlebars.template, templates = ');
        output.push(opts.namespace);
        output.push(' = ');
        output.push(opts.namespace);
        output.push(' || {};\n');
      }
      function processTemplate(template, root) {
        var path = template,
            stat = fs.statSync(path);
        if (stat.isDirectory()) {
          fs.readdirSync(template).map(function(file) {
            var path = template + '/' + file;

            if (extension.test(path) || fs.statSync(path).isDirectory()) {
              processTemplate(path, root || template);
            }
          });
        } else {
          var data = fs.readFileSync(path, 'utf8');

          Iif (opts.bom && data.indexOf('\uFEFF') === 0) {
            data = data.substring(1);
          }

          var options = {
            knownHelpers: known,
            knownHelpersOnly: opts.o
          };

          if (opts.data) {
            options.data = true;
          }

          // Clean the template name
          if (!root) {
            template = basename(template);
          } else Eif (template.indexOf(root) === 0) {
            template = template.substring(root.length+1);
          }
          template = template.replace(extension, '');

          if (opts.simple) {
            output.push(Handlebars.precompile(data, options) + '\n');
          } else if (opts.partial) {
            if(opts.amd && (opts.templates.length == 1 && !fs.statSync(opts.templates[0]).isDirectory())) {
              output.push('return ');
            }
            output.push('Handlebars.partials[\'' + template + '\'] = template(' + Handlebars.precompile(data, options) + ');\n');
          } else {
            if(opts.amd && (opts.templates.length == 1 && !fs.statSync(opts.templates[0]).isDirectory())) {
              output.push('return ');
            }
            output.push('templates[\'' + template + '\'] = template(' + Handlebars.precompile(data, options) + ');\n');
          }
        }
      }

      opts.templates.forEach(function(template) {
        processTemplate(template, opts.root);
      });

      // Output the content
      if (!opts.simple) {
        if (opts.amd) {
          if(opts.templates.length > 1 || (opts.templates.length == 1 && fs.statSync(opts.templates[0]).isDirectory())) {
            if(opts.partial){
              output.push('return Handlebars.partials;\n');
            } else {
              output.push('return templates;\n');
            }
          }
          output.push('});');
        } else if (!opts.commonjs) {
          output.push('})();');
        }
      }
      output = output.join('');

      if (opts.min) {
        output = uglify.minify(output, {fromString: true}).code;
      }

      Iif (opts.output) {
        fs.writeFileSync(opts.output, output, 'utf8');
      } else {
        console.log(output);
      }
    };