[484] | 1 | /** |
---|
| 2 | * Module dependencies. |
---|
| 3 | */ |
---|
| 4 | |
---|
| 5 | var fs = require('fs') |
---|
| 6 | , path = require('path') |
---|
| 7 | , join = path.join |
---|
| 8 | , debug = require('debug')('mocha:watch'); |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * Ignored directories. |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | var ignore = ['node_modules', '.git']; |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * Escape special characters in the given string of html. |
---|
| 18 | * |
---|
| 19 | * @param {String} html |
---|
| 20 | * @return {String} |
---|
| 21 | * @api private |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | exports.escape = function(html){ |
---|
| 25 | return String(html) |
---|
| 26 | .replace(/&/g, '&') |
---|
| 27 | .replace(/"/g, '"') |
---|
| 28 | .replace(/</g, '<') |
---|
| 29 | .replace(/>/g, '>'); |
---|
| 30 | }; |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Array#forEach (<=IE8) |
---|
| 34 | * |
---|
| 35 | * @param {Array} array |
---|
| 36 | * @param {Function} fn |
---|
| 37 | * @param {Object} scope |
---|
| 38 | * @api private |
---|
| 39 | */ |
---|
| 40 | |
---|
| 41 | exports.forEach = function(arr, fn, scope){ |
---|
| 42 | for (var i = 0, l = arr.length; i < l; i++) |
---|
| 43 | fn.call(scope, arr[i], i); |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Array#indexOf (<=IE8) |
---|
| 48 | * |
---|
| 49 | * @parma {Array} arr |
---|
| 50 | * @param {Object} obj to find index of |
---|
| 51 | * @param {Number} start |
---|
| 52 | * @api private |
---|
| 53 | */ |
---|
| 54 | |
---|
| 55 | exports.indexOf = function(arr, obj, start){ |
---|
| 56 | for (var i = start || 0, l = arr.length; i < l; i++) { |
---|
| 57 | if (arr[i] === obj) |
---|
| 58 | return i; |
---|
| 59 | } |
---|
| 60 | return -1; |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Array#reduce (<=IE8) |
---|
| 65 | * |
---|
| 66 | * @param {Array} array |
---|
| 67 | * @param {Function} fn |
---|
| 68 | * @param {Object} initial value |
---|
| 69 | * @api private |
---|
| 70 | */ |
---|
| 71 | |
---|
| 72 | exports.reduce = function(arr, fn, val){ |
---|
| 73 | var rval = val; |
---|
| 74 | |
---|
| 75 | for (var i = 0, l = arr.length; i < l; i++) { |
---|
| 76 | rval = fn(rval, arr[i], i, arr); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | return rval; |
---|
| 80 | }; |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Array#filter (<=IE8) |
---|
| 84 | * |
---|
| 85 | * @param {Array} array |
---|
| 86 | * @param {Function} fn |
---|
| 87 | * @api private |
---|
| 88 | */ |
---|
| 89 | |
---|
| 90 | exports.filter = function(arr, fn){ |
---|
| 91 | var ret = []; |
---|
| 92 | |
---|
| 93 | for (var i = 0, l = arr.length; i < l; i++) { |
---|
| 94 | var val = arr[i]; |
---|
| 95 | if (fn(val, i, arr)) ret.push(val); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | return ret; |
---|
| 99 | }; |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | * Object.keys (<=IE8) |
---|
| 103 | * |
---|
| 104 | * @param {Object} obj |
---|
| 105 | * @return {Array} keys |
---|
| 106 | * @api private |
---|
| 107 | */ |
---|
| 108 | |
---|
| 109 | exports.keys = Object.keys || function(obj) { |
---|
| 110 | var keys = [] |
---|
| 111 | , has = Object.prototype.hasOwnProperty // for `window` on <=IE8 |
---|
| 112 | |
---|
| 113 | for (var key in obj) { |
---|
| 114 | if (has.call(obj, key)) { |
---|
| 115 | keys.push(key); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | return keys; |
---|
| 120 | }; |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * Watch the given `files` for changes |
---|
| 124 | * and invoke `fn(file)` on modification. |
---|
| 125 | * |
---|
| 126 | * @param {Array} files |
---|
| 127 | * @param {Function} fn |
---|
| 128 | * @api private |
---|
| 129 | */ |
---|
| 130 | |
---|
| 131 | exports.watch = function(files, fn){ |
---|
| 132 | var options = { interval: 100 }; |
---|
| 133 | files.forEach(function(file){ |
---|
| 134 | debug('file %s', file); |
---|
| 135 | fs.watchFile(file, options, function(curr, prev){ |
---|
| 136 | if (prev.mtime < curr.mtime) fn(file); |
---|
| 137 | }); |
---|
| 138 | }); |
---|
| 139 | }; |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | * Ignored files. |
---|
| 143 | */ |
---|
| 144 | |
---|
| 145 | function ignored(path){ |
---|
| 146 | return !~ignore.indexOf(path); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | * Lookup files in the given `dir`. |
---|
| 151 | * |
---|
| 152 | * @return {Array} |
---|
| 153 | * @api private |
---|
| 154 | */ |
---|
| 155 | |
---|
| 156 | exports.files = function(dir, ret){ |
---|
| 157 | ret = ret || []; |
---|
| 158 | |
---|
| 159 | fs.readdirSync(dir) |
---|
| 160 | .filter(ignored) |
---|
| 161 | .forEach(function(path){ |
---|
| 162 | path = join(dir, path); |
---|
| 163 | if (fs.statSync(path).isDirectory()) { |
---|
| 164 | exports.files(path, ret); |
---|
| 165 | } else if (path.match(/\.(js|coffee|litcoffee|coffee.md)$/)) { |
---|
| 166 | ret.push(path); |
---|
| 167 | } |
---|
| 168 | }); |
---|
| 169 | |
---|
| 170 | return ret; |
---|
| 171 | }; |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Compute a slug from the given `str`. |
---|
| 175 | * |
---|
| 176 | * @param {String} str |
---|
| 177 | * @return {String} |
---|
| 178 | * @api private |
---|
| 179 | */ |
---|
| 180 | |
---|
| 181 | exports.slug = function(str){ |
---|
| 182 | return str |
---|
| 183 | .toLowerCase() |
---|
| 184 | .replace(/ +/g, '-') |
---|
| 185 | .replace(/[^-\w]/g, ''); |
---|
| 186 | }; |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | * Strip the function definition from `str`, |
---|
| 190 | * and re-indent for pre whitespace. |
---|
| 191 | */ |
---|
| 192 | |
---|
| 193 | exports.clean = function(str) { |
---|
| 194 | str = str |
---|
| 195 | .replace(/^function *\(.*\) *{/, '') |
---|
| 196 | .replace(/\s+\}$/, ''); |
---|
| 197 | |
---|
| 198 | var whitespace = str.match(/^\n?(\s*)/)[1] |
---|
| 199 | , re = new RegExp('^' + whitespace, 'gm'); |
---|
| 200 | |
---|
| 201 | str = str.replace(re, ''); |
---|
| 202 | |
---|
| 203 | return exports.trim(str); |
---|
| 204 | }; |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | * Escape regular expression characters in `str`. |
---|
| 208 | * |
---|
| 209 | * @param {String} str |
---|
| 210 | * @return {String} |
---|
| 211 | * @api private |
---|
| 212 | */ |
---|
| 213 | |
---|
| 214 | exports.escapeRegexp = function(str){ |
---|
| 215 | return str.replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&"); |
---|
| 216 | }; |
---|
| 217 | |
---|
| 218 | /** |
---|
| 219 | * Trim the given `str`. |
---|
| 220 | * |
---|
| 221 | * @param {String} str |
---|
| 222 | * @return {String} |
---|
| 223 | * @api private |
---|
| 224 | */ |
---|
| 225 | |
---|
| 226 | exports.trim = function(str){ |
---|
| 227 | return str.replace(/^\s+|\s+$/g, ''); |
---|
| 228 | }; |
---|
| 229 | |
---|
| 230 | /** |
---|
| 231 | * Parse the given `qs`. |
---|
| 232 | * |
---|
| 233 | * @param {String} qs |
---|
| 234 | * @return {Object} |
---|
| 235 | * @api private |
---|
| 236 | */ |
---|
| 237 | |
---|
| 238 | exports.parseQuery = function(qs){ |
---|
| 239 | return exports.reduce(qs.replace('?', '').split('&'), function(obj, pair){ |
---|
| 240 | var i = pair.indexOf('=') |
---|
| 241 | , key = pair.slice(0, i) |
---|
| 242 | , val = pair.slice(++i); |
---|
| 243 | |
---|
| 244 | obj[key] = decodeURIComponent(val); |
---|
| 245 | return obj; |
---|
| 246 | }, {}); |
---|
| 247 | }; |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | * Highlight the given string of `js`. |
---|
| 251 | * |
---|
| 252 | * @param {String} js |
---|
| 253 | * @return {String} |
---|
| 254 | * @api private |
---|
| 255 | */ |
---|
| 256 | |
---|
| 257 | function highlight(js) { |
---|
| 258 | return js |
---|
| 259 | .replace(/</g, '<') |
---|
| 260 | .replace(/>/g, '>') |
---|
| 261 | .replace(/\/\/(.*)/gm, '<span class="comment">//$1</span>') |
---|
| 262 | .replace(/('.*?')/gm, '<span class="string">$1</span>') |
---|
| 263 | .replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>') |
---|
| 264 | .replace(/(\d+)/gm, '<span class="number">$1</span>') |
---|
| 265 | .replace(/\bnew *(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>') |
---|
| 266 | .replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>') |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | /** |
---|
| 270 | * Highlight the contents of tag `name`. |
---|
| 271 | * |
---|
| 272 | * @param {String} name |
---|
| 273 | * @api private |
---|
| 274 | */ |
---|
| 275 | |
---|
| 276 | exports.highlightTags = function(name) { |
---|
| 277 | var code = document.getElementsByTagName(name); |
---|
| 278 | for (var i = 0, len = code.length; i < len; ++i) { |
---|
| 279 | code[i].innerHTML = highlight(code[i].innerHTML); |
---|
| 280 | } |
---|
| 281 | }; |
---|