|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2011 Twitter, Inc. |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | +// dependencies |
| 20 | +var hogan = require('hogan.js'); |
| 21 | +var path = require('path'); |
| 22 | +var nopt = require('nopt'); |
| 23 | +var mkderp = require('mkdirp'); |
| 24 | +var fs = require('fs'); |
| 25 | + |
| 26 | + |
| 27 | +// locals |
| 28 | +var specials = ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\']; |
| 29 | +var specialsRegExp = new RegExp('(\\' + specials.join('|\\') + ')', 'g'); |
| 30 | +var options = { |
| 31 | + 'namespace': String, |
| 32 | + 'outputdir': path, |
| 33 | + 'variable': String, |
| 34 | + 'wrapper': String, |
| 35 | + 'version': true, |
| 36 | + 'help': true |
| 37 | +}; |
| 38 | +var shortHand = { |
| 39 | + 'n': ['--namespace'], |
| 40 | + 'o': ['--outputdir'], |
| 41 | + 'vn': ['--variable'], |
| 42 | + 'w': ['--wrapper'], |
| 43 | + 'h': ['--help'], |
| 44 | + 'v': ['--version'] |
| 45 | +}; |
| 46 | +var templates; |
| 47 | + |
| 48 | + |
| 49 | +// options |
| 50 | +options = nopt(options, shortHand); |
| 51 | + |
| 52 | + |
| 53 | +// escape special regexp characters |
| 54 | +function esc(text) { |
| 55 | + return text.replace(specialsRegExp, '\\$1'); |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +// cyan function for rob |
| 60 | +function cyan(text) { |
| 61 | + return '\033[36m' + text + '\033[39m'; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +// check for dirs and correct ext (<3 for windows) |
| 66 | +function extractFiles(args) { |
| 67 | + var usage = '\n' + |
| 68 | + cyan('USAGE:') + ' hulk [--wrapper wrapper] [--outputdir outputdir] ' + |
| 69 | + '[--namespace namespace] [--variable variable] FILES\n\n' + |
| 70 | + cyan('OPTIONS:') + ' [-w, --wrapper] :: wraps the template (i.e. amd)\n' + |
| 71 | + ' [-o, --outputdir] :: outputs the templates as individual files to a directory\n\n' + |
| 72 | + ' [-n, --namespace] :: prepend string to template names\n\n' + |
| 73 | + ' [-vn, --variable] :: variable name for non-amd wrapper\n\n' + |
| 74 | + cyan('EXAMPLE:') + ' hulk --wrapper amd ./templates/*.mustache\n\n' + |
| 75 | + cyan('NOTE:') + ' hulk supports the "*" wildcard and allows you to target specific extensions too\n'; |
| 76 | + var files = []; |
| 77 | + |
| 78 | + if (options.version) { |
| 79 | + console.log(require('../package.json').version); |
| 80 | + process.exit(0); |
| 81 | + } |
| 82 | + |
| 83 | + if (!args.length || options.help) { |
| 84 | + console.log(usage); |
| 85 | + process.exit(0); |
| 86 | + } |
| 87 | + |
| 88 | + args.forEach(function(arg) { |
| 89 | + |
| 90 | + if (/\*/.test(arg)) { |
| 91 | + arg = arg.split('*'); |
| 92 | + return files = files.concat( |
| 93 | + fs.readdirSync(arg[0] || '.') |
| 94 | + .map(function(f) { |
| 95 | + var file = path.join(arg[0], f); |
| 96 | + return new RegExp(esc(arg[1]) + '$').test(f) && fs.statSync(file).isFile() && file; |
| 97 | + }) |
| 98 | + .filter(function(f) { |
| 99 | + return f; |
| 100 | + }) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + if (fs.statSync(arg).isFile()) files.push(arg); |
| 105 | + |
| 106 | + }); |
| 107 | + |
| 108 | + return files; |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +// remove utf-8 byte order mark, http://en.wikipedia.org/wiki/Byte_order_mark |
| 113 | +function removeByteOrderMark(text) { |
| 114 | + if (text.charCodeAt(0) === 0xfeff) { |
| 115 | + return text.substring(1); |
| 116 | + } |
| 117 | + return text; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +// wrap templates |
| 122 | +function wrap(file, name, openedFile) { |
| 123 | + switch (options.wrapper) { |
| 124 | + case "amd": |
| 125 | + return 'define(' + (!options.outputdir ? '"' + path.join(path.dirname(file), name) + '", ' : '') + |
| 126 | + '[ "hogan.js" ], function(Hogan){ return new Hogan.Template(' + |
| 127 | + hogan.compile(openedFile, {asString: 1}) + |
| 128 | + ');});'; |
| 129 | + case "node": |
| 130 | + var globalObj = 'global.' + (options.variable || 'templates') + '["' + name + '"]'; |
| 131 | + var globalStmt = globalObj + ' = new Hogan.Template(' + hogan.compile(openedFile, {asString: 1}) + ');'; |
| 132 | + var nodeOutput = globalStmt; |
| 133 | + |
| 134 | + // if we have a template per file the export will expose the template directly |
| 135 | + if (options.outputdir) { |
| 136 | + nodeOutput = nodeOutput + '\n' + 'module.exports = ' + globalObj + ';'; |
| 137 | + } |
| 138 | + |
| 139 | + return nodeOutput; |
| 140 | + default: |
| 141 | + return (options.variable || 'templates') + |
| 142 | + '["' + name + '"] = new Hogan.Template(' + |
| 143 | + hogan.compile(openedFile, {asString: 1}) + |
| 144 | + ');'; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +function prepareOutput(content) { |
| 150 | + var variableName = options.variable || 'templates'; |
| 151 | + switch (options.wrapper) { |
| 152 | + case "amd": |
| 153 | + return content; |
| 154 | + case "node": |
| 155 | + var nodeExport = ''; |
| 156 | + |
| 157 | + // if we have aggregated templates the export will expose the template map |
| 158 | + if (!options.outputdir) { |
| 159 | + nodeExport = 'module.exports = global.' + variableName + ';\n'; |
| 160 | + } |
| 161 | + |
| 162 | + return '(function() {\n' + |
| 163 | + 'if (!!!global.' + variableName + ') global.' + variableName + ' = {};\n' + |
| 164 | + content + '\n' + |
| 165 | + nodeExport + |
| 166 | + '})();'; |
| 167 | + default: |
| 168 | + return 'if (!!!' + variableName + ') var ' + variableName + ' = {};\n' + content; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | +// write the directory |
| 174 | +if (options.outputdir) { |
| 175 | + mkderp.sync(options.outputdir); |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | +// Prepend namespace to template name |
| 180 | +function namespace(name) { |
| 181 | + return (options.namespace || '') + name; |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +// write a template foreach file that matches template extension |
| 186 | +templates = extractFiles(options.argv.remain) |
| 187 | + .map(function(file) { |
| 188 | + var openedFile = fs.readFileSync(file, 'utf-8'); |
| 189 | + var name; |
| 190 | + if (!openedFile) return; |
| 191 | + name = namespace(path.basename(file).replace(/\..*$/, '')); |
| 192 | + openedFile = removeByteOrderMark(openedFile.trim()); |
| 193 | + openedFile = wrap(file, name, openedFile); |
| 194 | + if (!options.outputdir) return openedFile; |
| 195 | + fs.writeFileSync(path.join(options.outputdir, name + '.js') |
| 196 | + , prepareOutput(openedFile)); |
| 197 | + }) |
| 198 | + .filter(function(t) { |
| 199 | + return t; |
| 200 | + }); |
| 201 | + |
| 202 | + |
| 203 | +// output templates |
| 204 | +if (!templates.length || options.outputdir) process.exit(0); |
| 205 | + |
| 206 | +console.log(prepareOutput(templates.join('\n'))); |
0 commit comments