Skip to content
This repository
executable file 170 lines (138 sloc) 4.95 kb
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
#!/usr/bin/env node

/*
* Copyright 2011 Twitter, Inc.
* 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.
*/


// dependencies
var hogan = require('../lib/hogan.js')
  , path = require('path')
  , nopt = require('nopt')
  , mkderp = require('mkdirp')
  , fs = require('fs');


// locals
var specials = ['/', '.', '*', '+', '?', '|','(', ')', '[', ']', '{', '}', '\\']
  , specialsRegExp = new RegExp('(\\' + specials.join('|\\') + ')', 'g')
  , options = {
    'namespace': String,
    'outputdir': path,
    'variable': String,
    'wrapper': String,
    'version': true,
    'help': true
    }
  , shortHand = {
    'n': ['--namespace'],
    'o': ['--outputdir'],
    'v': ['--variable'],
    'w': ['--wrapper'],
    'h': ['--help'],
    'v': ['--version']
    }
  , templates;


// options
options = nopt(options, shortHand);


// escape special regexp characters
function esc(text) {
  return text.replace(specialsRegExp, '\\$1');
}


// cyan function for rob
function cyan(text) {
  return '\033[36m' + text + '\033[39m';
}


// check for dirs and correct ext (<3 for windows)
function extractFiles(args) {
  var usage = '\n' +
              cyan('USAGE:') + ' hulk [--wrapper wrapper] [--outputdir outputdir] [--namespace namespace] [--variable variable] FILES\n\n' +
              cyan('OPTIONS:') + ' [-w, --wrapper] :: wraps the template (i.e. amd)\n' +
              ' [-o, --outputdir] :: outputs the templates as individual files to a directory\n\n' +
              ' [-n, --namespace] :: prepend string to template names\n\n' +
              ' [-v, --variable] :: variable name for non-amd wrapper\n\n' +
              cyan('EXAMPLE:') + ' hulk --wrapper amd ./templates/*.mustache\n\n' +
              cyan('NOTE:') + ' hulk supports the "*" wildcard and allows you to target specific extensions too\n',
      files = [];

  if (options.version) {
    console.log(require('../package.json').version);
    process.exit(0);
  }

  if (!args.length || options.help) {
    console.log(usage);
    process.exit(0);
  }

  args.forEach(function (arg) {

    if (/\*/.test(arg)) {
      arg = arg.split('*');
      return files = files.concat(
        fs.readdirSync(arg[0] || '.')
          .map(function (f) {
            var file = path.join(arg[0], f);
            return new RegExp(esc(arg[1]) + '$').test(f) && fs.statSync(file).isFile() && file;
          })
          .filter(function (f) {
            return f;
          })
      );
    }

    if (fs.statSync(arg).isFile()) files.push(arg);

  })

  return files;
}


// remove utf-8 byte order mark, http://en.wikipedia.org/wiki/Byte_order_mark
function removeByteOrderMark(text) {
  if (text.charCodeAt(0) === 0xfeff) {
    return text.substring(1);
  }
  return text;
}


// wrap templates
function wrap(file, name, openedFile) {
  switch(options.wrapper) {
    case "amd":
      return 'define('+ (!options.outputdir ? '"' + path.join(path.dirname(file), name) + '", ' : '') + '[ "hogan.js" ], function(Hogan){ return new Hogan.Template(' + hogan.compile(openedFile, { asString: 1 }) + ');});';
    default:
      return (options.variable || 'templates')
        + '["' + name + '"] = new Hogan.Template('
        + hogan.compile(openedFile, { asString: 1 })
        + ');';
  }
}


// write the directory
if (options.outputdir) {
  mkderp.sync(options.outputdir);
}


// Prepend namespace to template name
function namespace(name) {
    return (options.namespace || '') + name;
}


// write a template foreach file that matches template extension
templates = extractFiles(options.argv.remain)
  .map(function (file) {
    var openedFile = fs.readFileSync(file, 'utf-8'), name;
    if (!openedFile) return;
    name = namespace(path.basename(file).replace(/\..*$/, ''));
    openedFile = removeByteOrderMark(openedFile.trim());
    openedFile = wrap(file, name, openedFile);
    if (!options.outputdir) return openedFile;
    var vn = options.variable || 'templates';
    fs.writeFileSync(path.join(options.outputdir, name + '.js')
      , 'if (!!!' + vn + ') var ' + vn + ' = {};\n' + openedFile);
  })
  .filter(function (t) {
    return t;
  });


// output templates
if (!templates.length || options.outputdir) process.exit(0);
if (!options.wrapper) {
  var vn = options.variable || 'templates';
  console.log('if (!!!' + vn + ') var ' + vn + ' = {};');
}
console.log(templates.join('\n'));
Something went wrong with that request. Please try again.