Permalink
Browse files

In case the auto-update fails the bundle now displays an error message

  • Loading branch information...
1 parent cb04360 commit 9b4666952436d5f4080727cd3069d9d588842746 @fgnass committed Apr 20, 2011
Showing with 63 additions and 49 deletions.
  1. +55 −47 Support/jshint-tm.js
  2. +8 −2 Support/output.html
View
@@ -10,78 +10,86 @@ var entities = {
'>': '>'
};
+function html(s) {
+ return (s || '').replace(/[&"<>]/g, function(c) {return entities[c] || c;});
+}
+
/**
* Downloads the latest JSHint version from jshint.com and invokes the callback when done.
*/
function download(ready) {
- try {
- var req = http.get({host: 'jshint.com', port: 80, path: '/jshint.js'}, function(res) {
- if (res.statusCode == 200) {
- res.setEncoding('utf8');
- var file = fs.createWriteStream(jshintPath);
- res.on('data', function(chunk) {
- file.write(chunk);
- });
- res.on('end', function() {
- file.end();
- ready();
- });
- }
- else {
+ var req = http.get({host: 'jshint.com', port: 80, path: '/jshint.js'}, function(res) {
+ if (res.statusCode == 200) {
+ res.setEncoding('utf8');
+ var file = fs.createWriteStream(jshintPath);
+ res.on('data', function(chunk) {
+ file.write(chunk);
+ });
+ res.on('end', function() {
+ file.end();
ready();
- }
- });
- }
- catch(err) {
- 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) {
- function done() {
- callback(require(jshintPath).JSHINT);
+ 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();
});
}
-function html(s) {
- return (s || '').replace(/[&"<>]/g, function(c) {return entities[c] || c;});
-}
-
module.exports = function(options) {
- autoupdate(function(jshint) {
- var file = env.TM_FILEPATH,
- input = fs.readFileSync(file, 'utf8'),
- body = '';
+ autoupdate(function(err, jshint) {
+ var body = '';
+ if (err) {
+ body += '<div class="error">' + err + '</div>';
+ }
+ if (jshint) {
+
+ var file = env.TM_FILEPATH;
+ var input = fs.readFileSync(file, 'utf8');
- //remove shebang
- input = input.replace(/^\#\!.*/, '');
+ //remove shebang
+ input = input.replace(/^\#\!.*/, '');
- 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>';
+ 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>';
}
- body += '</a>';
- }
- });
+ });
+ }
+ }
+ if (body.length > 0) {
fs.readFile(__dirname + '/output.html', 'utf8', function(e, html) {
sys.puts(html.replace('{body}', body));
process.exit(205); //show_html
View
@@ -6,7 +6,7 @@
padding: 0;
margin: 0;
}
- a {
+ a.txmt {
display: block;
text-decoration: none;
color: #444;
@@ -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;
@@ -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()">

0 comments on commit 9b46669

Please sign in to comment.