Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dandean committed Jun 10, 2011
2 parents ddb836b + f68fc35 commit 295655a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Features:
* Can be bypassed by pressing ⇧⌘S
* Output is only shown when errors are found
* Window is automatically closed when it looses focus
* Auto-updates itself to the latest version of JSHint
* Based on Node.js

## Installation
Expand Down
125 changes: 89 additions & 36 deletions Support/jshint-tm.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,98 @@
var sys = require("sys"),
fs = require("fs"),
env = process.env || process.ENV,
JSHINT = require('./jshint.js').JSHINT,
entities = {
'&': '&',
'"': '"',
'<': '&lt;',
'>': '&gt;'
};
var sys = require('sys');
var fs = require('fs');
var http = require('http');
var env = process.env || process.ENV;
var jshintPath = __dirname + '/jshint.js';
var entities = {
'&': '&amp;',
'"': '&quot;',
'<': '&lt;',
'>': '&gt;'
};

function html(s) {
return (s || '').replace(/[&"<>]/g, function(c) {return entities[c] || c;});
}

module.exports = function (options) {
var file = env.TM_FILEPATH,
input = fs.readFileSync(file, 'utf8'),
body = '';
/**
* Downloads the latest JSHint version from jshint.com and invokes the callback when done.
*/
function download(ready) {
var req = http.get({host: 'jshint.com', port: 80, path: '/jshint.js'}, function(res) {
if (res.statusCode == 200) {
res.setEncoding('utf8');
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
fs.writeFile(jshintPath, data, ready);
});
}
else {
ready('Download of jshint.js failed. HTTP status code: ' + res.statusCode);
}
}).on('error', function(err) {
ready('Download of jshint.js failed: ' + html(err.message));
});
}

/**
* Updates the local copy of jshint.js (if it is older than one day) and
* invokes the given callback, passing the JSHINT object.
*/
function autoupdate(callback) {
var fileExists;
function done(err) {
callback(err, (!err || fileExists) && require(jshintPath).JSHINT);
}
fs.stat(jshintPath, function(err, stats) {
fileExists = !err;
if (err || (Date.now() - Date.parse(stats.mtime)) / 1000 / 60 / 60 / 24 >= 1) {
return download(done);
}
done();
});
}

module.exports = function(options) {
autoupdate(function(err, jshint) {
var body = '';
if (err) {
body += '<div class="error">' + err + '</div>';
}
if (jshint) {

//remove shebang
input = input.replace(/^\#\!.*/, '');
var file = env.TM_FILEPATH;
var input = fs.readFileSync(file, 'utf8');

if (!JSHINT(input, options)) {
JSHINT.errors.forEach(function(e) {
if (e) {
body += ('<a href="txmt://open?url=file://' + escape(file) + '&line=' + e.line + '&column=' + e.character + '">' + e.reason);
if (e.evidence && !isNaN(e.character)) {
body += '<tt>';
body += html(e.evidence.substring(0, e.character-1));
body += '<em>';
body += (e.character <= e.evidence.length) ? html(e.evidence.substring(e.character-1, e.character)) : '&nbsp;';
body += '</em>';
body += html(e.evidence.substring(e.character));
body += '</tt>';
}
body += '</a>';
//remove shebang
input = input.replace(/^\#\!.*/, '');

if (!jshint(input, options)) {
jshint.errors.forEach(function(e) {
if (e) {
var link = 'txmt://open?url=file://' + escape(file) + '&line=' + e.line + '&column=' + e.character;
body += ('<a class="txmt" href="' + link + '">' + e.reason);
if (e.evidence && !isNaN(e.character)) {
body += '<tt>';
body += html(e.evidence.substring(0, e.character-1));
body += '<em>';
body += (e.character <= e.evidence.length) ? html(e.evidence.substring(e.character-1, e.character)) : '&nbsp;';
body += '</em>';
body += html(e.evidence.substring(e.character));
body += '</tt>';
}
body += '</a>';
}
});
}
});
fs.readFile(__dirname + '/output.html', 'utf8', function(e, html) {
sys.puts(html.replace('{body}', body));
process.exit(205); //show_html
});
}
}
if (body.length > 0) {
fs.readFile(__dirname + '/output.html', 'utf8', function(e, html) {
sys.puts(html.replace('{body}', body));
process.exit(205); //show_html
});
}
});
};
10 changes: 8 additions & 2 deletions Support/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
padding: 0;
margin: 0;
}
a {
a.txmt {
display: block;
text-decoration: none;
color: #444;
Expand All @@ -17,7 +17,7 @@
padding: 10px 20px;
text-shadow: 0 1px #fff;
}
a:hover {
a.txmt:hover {
background: -webkit-gradient(linear, 0 100%, 0 0, from(#8191B0), to(#B5C0D5));
color: #fff;
text-shadow: none;
Expand All @@ -32,6 +32,12 @@
font-style: normal;
border-bottom: 2px solid red;
}
.error {
padding: 10px 20px;
background: -webkit-gradient(linear, 0 100%, 0 0, from(#f9eba5), to(#fdf4c5));
border-bottom: 1px solid #999;
font-size: 13px;
}
</style>
</head>
<body onload="window.resizeTo(800,document.getElementById('output').offsetHeight+22)" onblur="window.close()">
Expand Down

0 comments on commit 295655a

Please sign in to comment.