UNPKG

jitter

Version:

Simple continuous compilation for CoffeeScript

374 lines (331 loc) 12.3 kB
// Generated by CoffeeScript 1.7.1 /* Jitter, a CoffeeScript compilation utility The latest version and documentation, can be found at: http://github.com/TrevorBurnham/Jitter Copyright (c) 2010 Trevor Burnham http://iterative.ly Based on command.coffee by Jeremy Ashkenas http://jashkenas.github.com/coffee-script/documentation/docs/command.html Growl notification code contributed by Andrey Tarantsov http://www.tarantsov.com/ SourceMaps support by Aria Minaei https://twitter.com/ariaminaei https://github.com/AriaMinaei Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ (function() { var BANNER, CoffeeScript, baseSource, baseTarget, baseTest, color, compile, compileScript, compileScripts, die, e, exec, fs, isSubpath, isWatched, jsPath, notify, optionParser, options, optparse, parseOptions, path, print, puts, q, readScript, rootCompile, runTests, target_ext, targetlib, testFiles, usage, watchScript, writeJS, writeSourceMap, _ref, __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; fs = require('fs'); path = require('path'); optparse = require('./optparse'); color = require('./ansi-color').set; if (path.basename(process.argv[1]) === 'witter') { targetlib = 'coco'; target_ext = '.coco'; } else { targetlib = 'coffee-script'; target_ext = '.coffee'; } CoffeeScript = require(targetlib); exec = require('child_process').exec; _ref = (function() { try { return require('util'); } catch (_error) { e = _error; return require('sys'); } })(), puts = _ref.puts, print = _ref.print; q = require('sink').q; BANNER = 'Jitter takes a directory of *.coffee files and recursively compiles\nthem to *.js files, preserving the original directory structure.\n\nJitter also watches for changes and automatically recompiles as\nneeded. It even detects new files, unlike the coffee utility.\n\nIf passed a test directory, it will run each test through node on\neach change.\n\nUsage:\n jitter coffee-path js-path [test-path]'; options = {}; baseSource = baseTarget = baseTest = ''; optionParser = null; isWatched = {}; testFiles = []; exports.run = function() { options = parseOptions(); if (!baseTarget) { return usage(); } return compileScripts(options); }; compileScripts = function(options) { var dir, dirs, name; dirs = { Source: baseSource, Target: baseTarget }; if (baseTest) { dirs.Test = baseTest; } for (name in dirs) { dir = dirs[name]; q(fs.exists, dir, function(exists) { if (!exists) { return die("" + name + " directory '" + dir + "' does not exist."); } else if (!fs.statSync(dir).isDirectory()) { return die("" + name + " '" + dir + "' is a file; Jitter needs a directory."); } }); } q(function() { return rootCompile(options); }); q(runTests); return q(function() { puts('Watching for changes and new files. Press Ctrl+C to stop.\n'); return setInterval(function() { return rootCompile(options); }, 500); }); }; compile = function(source, target, options) { var item, sourcePath, _i, _len, _ref1, _results; _ref1 = fs.readdirSync(source); _results = []; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { item = _ref1[_i]; sourcePath = "" + source + "/" + item; if (item[0] === '.') { continue; } if (isWatched[sourcePath]) { continue; } try { if (path.extname(sourcePath) === target_ext) { _results.push(readScript(sourcePath, target, options)); } else if (fs.statSync(sourcePath).isDirectory()) { _results.push(compile(sourcePath, target, options)); } else { _results.push(void 0); } } catch (_error) { e = _error; } } return _results; }; rootCompile = function(options) { compile(baseSource, baseTarget, options); if (baseTest) { return compile(baseTest, baseTest, options); } }; readScript = function(source, target, options) { compileScript(source, target, options); return watchScript(source, target, options); }; watchScript = function(source, target, options) { if (isWatched[source]) { return; } isWatched[source] = true; return fs.watchFile(source, { persistent: true, interval: 250 }, function(curr, prev) { if (curr.mtime.getTime() === prev.mtime.getTime()) { return; } compileScript(source, target, options); return q(runTests); }); }; compileScript = function(source, target, options) { var code, coffeePath, currentJS, err, js, jsFilename, mapFilename, mapPath, msg, numOfParentDirs, pathToRoot, slashedTargetPath, sourceMap, targetPath; targetPath = jsPath(source, target); try { code = fs.readFileSync(source).toString(); try { currentJS = fs.readFileSync(targetPath).toString(); } catch (_error) {} if (!(options != null ? options.map : void 0)) { if (options != null) { options.map = false; } } if (options.map) { slashedTargetPath = targetPath.split('\\').join('/'); numOfParentDirs = slashedTargetPath.split('/').length - 1; mapFilename = slashedTargetPath.replace('.js', '.map'); if (mapFilename.lastIndexOf('/') !== -1) { mapFilename = mapFilename.substr(mapFilename.lastIndexOf('/') + 1); } jsFilename = mapFilename.replace('.map', '.js'); pathToRoot = (function() { var i, _i, _path; _path = []; for (i = _i = 1; 1 <= numOfParentDirs ? _i <= numOfParentDirs : _i >= numOfParentDirs; i = 1 <= numOfParentDirs ? ++_i : --_i) { _path.push('..'); } return _path.join('\\'); })(); mapPath = slashedTargetPath.substr(0, slashedTargetPath.length - 2) + 'map'; coffeePath = source.split('\\').join('/').split('/').join('\\'); } js = CoffeeScript.compile(code, { source: source, bare: options != null ? options.bare : void 0, sourceMap: options.map, generatedFile: jsFilename, sourceRoot: pathToRoot, sourceFiles: [coffeePath] }); if (options.map) { sourceMap = js.v3SourceMap; js = js.js + '\n/*\n' + ("//@ sourceMappingURL=" + mapFilename + "\n") + '*/\n'; } if (js === currentJS) { return; } writeJS(js, targetPath); if (sourceMap) { writeSourceMap(sourceMap, mapPath); } if (currentJS != null) { return puts('Recompiled ' + source + '\n'); } else { return puts('Compiled ' + source + '\n'); } } catch (_error) { err = _error; msg = (function() { var name, ret; name = path.basename(source); ret = 'Error: '; ret += source.substr(0, source.length - name.length); if (err.location != null) { ret += color(name + ':' + (err.location.first_line + 1), 'bold'); } ret = color(ret, 'red'); ret += '\n\n ' + err.message; return ret; })(); puts(msg); if (!(options != null ? options.beep : void 0)) { console.log("\007"); } return notify(source, err.message); } }; jsPath = function(source, target) { var base, dir, filename; base = target === baseTest ? baseTest : baseSource; filename = path.basename(source, path.extname(source)) + '.js'; dir = target + path.dirname(source).substring(base.length); return path.join(dir, filename); }; writeJS = function(js, targetPath) { return q(exec, "mkdir -p " + (path.dirname(targetPath)), function() { fs.writeFileSync(targetPath, js); if (baseTest && isSubpath(baseTest, targetPath) && (__indexOf.call(testFiles, targetPath) < 0)) { return testFiles.push(targetPath); } }); }; writeSourceMap = function(content, targetPath) { return q(exec, "mkdir -p " + (path.dirname(targetPath)), function() { return fs.writeFileSync(targetPath, content); }); }; notify = function(source, errMessage) { var args, basename, m, message; basename = source.replace(/^.*[\/\\]/, ''); if (m = errMessage.match(/Parse error on line (\d+)/)) { message = "Parse error in " + basename + "\non line " + m[1] + "."; } else { message = "Error in " + basename + "."; } if (process.platform === 'darwin') { args = ['growlnotify', '-n', 'CoffeeScript', '-p', '2', '-t', "\"Compilation failed\"", '-m', "\"" + message + "\n\'" + errMessage + "\'\""]; return exec(args.join(' ')); } else { args = ['notify-send', '-c', 'CoffeeScript', '-t', '5000', "\"Compilation failed\"", "\"" + message + "\n\'" + errMessage + "\'\""]; return exec(args.join(' ')); } }; runTests = function() { var test, _i, _len, _results; _results = []; for (_i = 0, _len = testFiles.length; _i < _len; _i++) { test = testFiles[_i]; _results.push((function() { var output; output = " Running " + ("" + test); return exec("node " + test, function(error, stdout, stderr) { if (stderr) { notify(test, stderr); } if (stderr) { if (!(options != null ? options.silent : void 0)) { console.log("\007"); } output += color(' ' + 'FAILED ->', 'red'); output += '\n' + stdout + stderr + '\n'; } else { output += color(' ' + 'PASSED\n', 'green'); if (stdout) { output += '\n' + stdout + '\n'; } } return puts(output); }); })()); } return _results; }; parseOptions = function() { var arg, _ref1; optionParser = new optparse.OptionParser([['-b', '--bare', 'compile without the top-level function wrapper'], ['-m', '--map', 'compile with source maps'], ['-s', '--silent', 'don\'t make beep sounds on errors']], BANNER); options = optionParser.parse(process.argv); _ref1 = (function() { var _i, _results; _results = []; for (arg = _i = 2; _i <= 4; arg = ++_i) { _results.push(options["arguments"][arg] || ''); } return _results; })(), baseSource = _ref1[0], baseTarget = _ref1[1], baseTest = _ref1[2]; if (/\/$/.test(baseSource)) { baseSource = baseSource.substr(0, baseSource.length - 1); } if (/\/$/.test(baseTarget)) { baseTarget = baseTarget.substr(0, baseTarget.length - 1); } return options; }; usage = function() { puts(optionParser.help()); return process.exit(0); }; die = function(message) { puts(message); return process.exit(1); }; isSubpath = function(parent, sub) { parent = fs.realpathSync(parent); sub = fs.realpathSync(sub); return sub.indexOf(parent) === 0; }; }).call(this);