more refactors

This commit is contained in:
Tommy Parnell
2016-03-12 11:49:22 -05:00
parent 03971fd151
commit e2ad38a581
5 changed files with 84 additions and 115 deletions

View File

@@ -5,7 +5,7 @@ var fs = require('fs');
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var isparta = require('isparta');
require('babel-core/register');
gulp.task('download', function () {
return request('http://wixtoolset.org/downloads/v3.11.0.129/wix311-binaries.zip').pipe(fs.createWriteStream('wixToolset.zip'));
@@ -16,12 +16,39 @@ gulp.task('getwix',['download'], function(){
.pipe(unzip())
.pipe(gulp.dest('./lib/wixFiles'));
});
var paths = {
server: {
scripts: ['src/**/*.js'],
tests: ['test/**/*.js'],
coverage: 'coverage/'
}
};
gulp.task('test-coverage-server', function(cb) {
var coverageDir = paths.server.coverage;
gulp.src(paths.server.scripts)
.pipe(istanbul({ // Covering files
instrumenter: isparta.Instrumenter,
includeUntested: true
}))
.pipe(istanbul.hookRequire()) // Force `require` to return covered files
.on('finish', function() {
gulp.src(paths.server.tests, {read: false})
.pipe(mocha({reporter: 'spec'}))
.pipe(istanbul.writeReports({
dir: coverageDir,
reportOpts: {dir: coverageDir},
reporters: ['text', 'text-summary', 'json', 'html']
}))
.on('end', cb);
});
});
gulp.task('pre-test', function () {
return gulp.src('src/**/*.js')
return gulp.src(['src/CommandBuilder.js'])
// Covering files
.pipe(istanbul({
instrumenter: isparta.Instrumenter,
// instrumenter: isparta.Instrumenter,
includeUntested: true}
))
@@ -29,7 +56,7 @@ gulp.task('pre-test', function () {
.pipe(istanbul.hookRequire());
});
gulp.task('test', function(){
gulp.task('test',['pre-test'], function(){
return gulp.src('test/unit/*.js')
.pipe(mocha())
.pipe(istanbul.writeReports());

View File

@@ -9,9 +9,11 @@
"scripts": {
"compile": "babel src --out-dir lib",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"prepublish": "npm run compile"
"prepublish": "npm run compile",
"test": "./node_modules/.bin/isparta cover node_modules/mocha/bin/_mocha"
},
"dependencies": {
"child-process-promise": "^1.1.0",
"lodash": "^4.6.1"
},
"devDependencies": {

View File

@@ -3,12 +3,11 @@ var fs = require('fs'),
_ = require('lodash'),
path = require('path');
var calculateCommands = function(options){
var commands = {
heatPath: options.heatPath || __dirname + "/wixFiles/heat.exe",
lightPath: options.lightPath || __dirname + "/wixFiles/light.exe",
candlePath: options.candlePath || __dirname + "/wixFiles/candle.exe"
heatPath: options.heatPath || path.normalize(__dirname + "/wixFiles/heat.exe"),
lightPath: options.lightPath || path.normalize(__dirname + "/wixFiles/light.exe"),
candlePath: options.candlePath || path.normalize(__dirname + "/wixFiles/candle.exe")
}
if(options.version){
process.env.BUILD_VERSION = version;

View File

@@ -5,9 +5,10 @@
var fs = require('fs'),
_ = require('lodash'),
path = require('path'),
child_process = require('child_process');
commandBuilder = require('./CommandBuilder.js'),
spawn = require('child-process-promise').spawn;
var processResults = function (stdout, stderr) {
var processResults = function (stdout) {
if(stdout && _.isArray(stdout)){
_.chain(stdout)
.filter((item)=>item && item.length > 0)
@@ -17,114 +18,54 @@ var processResults = function (stdout, stderr) {
else if(stdout && _.isString(stdout) && stdout.length > 0){
console.log(stdout);
}
if(stderr && _.isString(stderr) && stderr.length > 0){
console.log(stderr);
}
};
var processError = function(err, cb){
console.log(err);
if(cb && _.isFunction(cb) && err){
cb(err)
}
else if(err){
throw err
}
}
var processConsole = function processConsole(childProcess) {
if(childProcess && childProcess.stdout){
childProcess.stdout.on('data', (data)=>processResults(data.toString()));
}
if(childProcess && childProcess.stderr){
childProcess.stderr.on('data', (data)=>processResults(data.toString()));
}
};
var main = function (options, callback) {
var heatFiles = options.heatFiles;
var candleFiles = options.candleFiles;
var lightFiles = options.lightFiles;
var heatCommands = options.heatCommands || null;
var candleCommands = options.candleCommands || null;
var lightCommands = options.lightCommands || null;
var heatPath = options.heatPath || __dirname + "/wixFiles/heat.exe";
var lightPath = options.lightPath || __dirname + "/wixFiles/light.exe";
var candlePath = options.candlePath || __dirname + "/wixFiles/candle.exe";
var cb = callback;
var version = options.version;
if(version){
process.env.BUILD_VERSION = version;
}
if(!heatCommands){
if(!heatFiles || !_.isArray(heatFiles) || heatFiles.length < 1 ){
throw "heat files are required if no commands are passed";
}
checkFiles(heatFiles);
}
if(!candleCommands){
if(!candleFiles || !_.isArray(candleFiles) || candleFiles.length < 1 ){
throw "candle files are required";
var commands = commandBuilder(options);
//todo redo this a little...
if(commands.heatCommands){
return spawn(commands.heatPath, commands.heatCommands)
.progress(processConsole)
.then(()=>spawn(commands.candlePath, commands.candleCommands))
.progress(processConsole)
.then(()=>spawn(commands.lightPath, commands.lightCommands))
.progress(processConsole)
.then(()=>{
if(callback && _.isFunction(callback)){
callback();
}
checkFiles(candleCommands);
}
if(!lightCommands){
if(!lightFiles || !_.isArray(lightFiles) || lightFiles.length < 1 ){
throw "light files are required";
}
checkFiles(lightFiles);
}
return child_process.execFile(path.normalize(heatPath), heatCommands? heatCommands: _.map(heatFiles, (file)=>`@${path.normalize(file)}`), (err, stdout, stderr)=>{
processResults(stdout, stderr);
if(err){
if(cb){
return cb(err);
}else{
throw err;
}
}
return child_process.execFile(path.normalize(candlePath), candleCommands? candleCommands: _.map(candleFiles, (file)=>`@${path.normalize(file)}`), (err, stdout, stderr)=>{
processResults(stdout, stderr);
if(err){
if(cb){
return cb(err);
}else{
throw err;
}
}
return child_process.execFile(path.normalize(lightPath), lightCommands? lightCommands: _.map(lightFiles, (file)=>`@${path.normalize(file)}`), (err, stdout, stderr)=>{
processResults(stdout, stderr);
if(err){
if(cb){
return cb(err);
}else{
throw err;
}
}
if(cb){
return cb();
}
});
});
}
return spawn(commands.candlePath, commands.candleCommands)
.progress(processConsole)
.then(()=>spawn(commands.lightPath, commands.lightCommands))
.progress(processConsole)
.then(()=>{
if(callback && _.isFunction(callback)){
callback();
}
});
});
};
var checkFiles = function(files){
_.each(files, (file)=>{
if(!checkFile(file)){
throw "error finding file" + file;
}
});
};
var checkFile = function (file) {
if (!file || file.length < 1) {
return false;
}
try {
fs.accessSync(path.normalize(file), fs.R_OK); //will error if doesnt exist
//todo async?
return true;
} catch (error) {
return false;
}
};
module.exports = main;

View File

@@ -1,6 +1,6 @@
var assert = require('chai').assert;
var expect = require('chai').expect;
var commandBuilder = require('../../src/CommandBuilder');
var commandBuilder = require('../../src/CommandBuilder.js');
describe('CommandBuilderWorks', function(){