Skip to content

Commit

Permalink
after new jshint download update production dependencies
Browse files Browse the repository at this point in the history
If autoupdate downloads jshint.js because it is out-of-date
it now also updates package.json and runs:
  npm install --production
  • Loading branch information
stepheneb committed Sep 7, 2013
1 parent 6a7676a commit ad5de0c
Showing 1 changed file with 70 additions and 4 deletions.
74 changes: 70 additions & 4 deletions Support/jshint-tm.js
Expand Up @@ -2,6 +2,9 @@ var fs = require('fs');
var https = require('https'); var https = require('https');
var env = process.env || process.ENV; var env = process.env || process.ENV;
var jshintPath = __dirname + '/jshint.js'; var jshintPath = __dirname + '/jshint.js';
var jshintPackagePath = __dirname + '/package.json';
var exec = require('child_process').exec;
var child;
var entities = { var entities = {
'&': '&', '&': '&',
'"': '"', '"': '"',
Expand All @@ -15,10 +18,11 @@ function html(s) {


/** /**
* Downloads the latest JSHint version from GitHub and invokes the callback when done. * Downloads the latest JSHint version from GitHub and invokes the callback when done.
* https://raw.github.com/jshint/jshint/master/src/jshint.js
*/ */
function download(ready) { function download_jshint(ready) {
var req = https.get({host: 'raw.github.com', port: 443, path: '/jshint/jshint/master/jshint.js'}, function(res) { var req = https.get({host: 'raw.github.com', port: 443, path: '/jshint/jshint/master/src/jshint.js'}, function(res) {
if (res.statusCode == 200) { if (res.statusCode === 200) {
res.setEncoding('utf8'); res.setEncoding('utf8');
var data = ''; var data = '';
res.on('data', function(chunk) { res.on('data', function(chunk) {
Expand All @@ -36,15 +40,77 @@ function download(ready) {
}); });
} }


/**
* Downloads the latest package.json for JSHint from GitHub and invokes the callback when done.
* https://raw.github.com/jshint/jshint/master/package.json
*/
function download_jshint_package_json(ready) {
var req = https.get({host: 'raw.github.com', port: 443, path: '/jshint/jshint/master/package.json'}, function(res) {
if (res.statusCode === 200) {
res.setEncoding('utf8');
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
fs.writeFile(jshintPackagePath, data, ready);
});
}
else {
ready('Download of package.json for jshint.js failed. HTTP status code: ' + res.statusCode);
}
}).on('error', function(err) {
ready('Download of package.json for jshint.js failed: ' + html(err.message));
});
}

/**
* Runs npm install --production for JSHint in Support dir and invokes the callback when done.
* https://raw.github.com/jshint/jshint/master/src/jshint.js
*/
function run_npm_install_for_jshint(ready) {
var install = exec('npm install --production',
{ encoding: 'utf8', cwd: __dirname },
function (error, stdout, stderr) {
if (error) {
ready('npm install of production dependencies for jshint.js failed: ' + stderr);
} else {
ready();
}
}).on('error', function(err) {
ready('Download of package.json for jshint.js failed: ' + html(err.message));
});
}

function download(callback) {
function done(err) {
callback(err);
}
download_jshint(function (err) {
if (err) {
done(err);
} else {
download_jshint_package_json(function (err) {
if (err) {
done(err);
} else {
run_npm_install_for_jshint(done);
}
});
}
});
}

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

0 comments on commit ad5de0c

Please sign in to comment.