|
'use strict'; |
|
|
|
var gulp = require('gulp'); |
|
var plumber = require('gulp-plumber'); |
|
var jshint = require('gulp-jshint'); |
|
var istanbul = require('gulp-istanbul'); |
|
var mocha = require('gulp-mocha'); |
|
var srv = require('node-static'); |
|
var http = require('http'); |
|
var del = require('del'); |
|
var fs = require('fs'); |
|
var mkdirp = require('mkdirp'); |
|
var getDirName = require('path').dirname; |
|
var ps = require('ps-node'); |
|
var spawn = require('child_process').spawn; |
|
var sequence = require('run-sequence'); |
|
var cat = require('gulp-cat'); |
|
var watch = require('gulp-watch'); |
|
|
|
var istDir = 'coverage'; |
|
var istSrvName = 'Istanbul.js Coverage Server'; |
|
var istSrvPort = 8000; |
|
var istSrvPidFile = '.istanbul-coverage-server.pid'; |
|
var istSrvOutFile = 'log/istanbul/out.log'; |
|
var istSrvErrFile = 'log/istanbul/err.log'; |
|
|
|
var lintDir = 'linter'; |
|
var lintSrvName = 'JSHint Server'; |
|
var lintSrvPort = 8001; |
|
var lintSrvPidFile = '.lint-server.pid'; |
|
var lintSrvOutFile = 'log/linter/out.log'; |
|
var lintSrvErrFile = 'log/linter/err.log'; |
|
|
|
function openSync(path, flags, mode) { |
|
mkdirp.sync(getDirName(path)); |
|
return fs.openSync(path, flags, mode); |
|
} |
|
|
|
function writeFile(path, contents, cb) { |
|
mkdirp(getDirName(path), function(e) { |
|
if (e) { |
|
return cb(e); |
|
} |
|
|
|
fs.writeFile(path, contents, cb); |
|
}); |
|
} |
|
|
|
function kill(name, pidFile, cb) { |
|
fs.readFile(pidFile, { |
|
encoding: 'utf8' |
|
}, function(e, data) { |
|
var exists = true; |
|
if (e) { |
|
if (e.code === 'ENOENT') { |
|
exists = false; |
|
} else { |
|
throw e; |
|
} |
|
} |
|
|
|
if (!exists) { |
|
|
|
console.log('%s is not running', name); |
|
|
|
if (cb) { |
|
cb(); |
|
} |
|
return; |
|
} |
|
|
|
var pid = parseInt(data); |
|
|
|
ps.kill(pid, function() { |
|
|
|
console.log('Killed %s (%s)', name, pid); |
|
|
|
del(pidFile).then(function() { |
|
|
|
console.log('Deleted %s', pidFile); |
|
|
|
if (cb) { |
|
cb(); |
|
} |
|
}); |
|
}); |
|
}); |
|
} |
|
|
|
function serve(name, dir, port, outFile, errFile, pidFile) { |
|
var srv = function(name, dir, port, outFile, errFile, pidFile) { |
|
var out = openSync(outFile, 'a'); |
|
var err = openSync(errFile, 'a'); |
|
|
|
var child = spawn('node', ['./server.js', dir, port], { |
|
detached: true, |
|
stdio: ['ignore', out, err] |
|
}); |
|
|
|
var pid = child.pid; |
|
|
|
console.log('Launching %s (%s)', name, pid); |
|
|
|
child.unref(); |
|
|
|
writeFile(pidFile, pid); |
|
|
|
console.log('Created %s', pidFile); |
|
}; |
|
|
|
kill(name, pidFile, function() { |
|
srv(name, dir, port, outFile, errFile, pidFile); |
|
}); |
|
} |
|
|
|
function lint() { |
|
return gulp.src(['!node_modules/**', '!server.js', '!coverage/**', '!linter/**', '!.jshintrc', '**/*.js']) |
|
.pipe(plumber()) |
|
.pipe(jshint({linter: 'jshint'})) |
|
.pipe(jshint.reporter('gulp-jshint-html-reporter', { |
|
filename: lintDir + '/index.html', |
|
createMissingFolders: true |
|
})).on('end', function() { |
|
serve(lintSrvName, lintDir, lintSrvPort, lintSrvOutFile, lintSrvErrFile, lintSrvPidFile); |
|
}); |
|
} |
|
|
|
gulp.task('lint', lint); |
|
|
|
function test() { |
|
return gulp.src(['!node_modules/**', '!server.js', '!coverage/**', '!linter/**', '!.jshintrc', '!test/**', '**/*.js']) |
|
.pipe(plumber()) |
|
.pipe(istanbul()) |
|
.pipe(istanbul.hookRequire()) |
|
.on('finish', function() { |
|
gulp.src(['test/*.js']) |
|
.pipe(plumber()) |
|
.pipe(mocha()) |
|
.pipe(istanbul.writeReports({ |
|
dir: istDir, |
|
reporters: [ |
|
'text', |
|
'html' |
|
], |
|
reportOpts: { |
|
dir: istDir |
|
} |
|
})) |
|
.on('end', function() { |
|
serve(istSrvName, istDir, istSrvPort, istSrvOutFile, istSrvErrFile, istSrvPidFile); |
|
}); |
|
}); |
|
} |
|
|
|
gulp.task('test', test); |
|
|
|
function lintAndTest() { |
|
sequence( |
|
'lint', |
|
'test' |
|
); |
|
} |
|
|
|
function logs() { |
|
return gulp.src(['log/**/*.log']) |
|
.pipe(cat()); |
|
} |
|
|
|
gulp.task('logs', logs); |
|
|
|
gulp.task('watch', function() { |
|
lintAndTest(); |
|
watch(['!node_modules/**', '!server.js', '!coverage/**', '!linter/**', '.jshintrc', 'test/**', '**/*.js'], lintAndTest); |
|
}) |
|
|
|
gulp.task('clean', function() { |
|
del(['coverage', 'log']).then(function(paths) { |
|
paths.forEach(function(e) { |
|
console.log('Deleted %s', e); |
|
}); |
|
}); |
|
|
|
kill(istSrvName, istSrvPidFile); |
|
kill(lintSrvName, lintSrvPidFile); |
|
}); |
|
|
|
gulp.task('kill', function() { |
|
kill(istSrvName, istSrvPidFile); |
|
kill(lintSrvName, lintSrvPidFile); |
|
}); |
|
|
|
gulp.task('default', lintAndTest); |