2015-08-03 20:45:58 -04:00
|
|
|
|
var gulp = require('gulp');
|
|
|
|
|
var watch = require('gulp-watch');
|
|
|
|
|
var gutil = require('gulp-util');
|
|
|
|
|
var path = require('path');
|
|
|
|
|
var del = require('del');
|
|
|
|
|
|
|
|
|
|
var docShredder = require('./public/doc-shredder/doc-shredder');
|
|
|
|
|
|
2015-08-08 16:55:53 -04:00
|
|
|
|
var _shredOptions = {
|
2015-08-03 20:45:58 -04:00
|
|
|
|
basePath: path.resolve('./public/docs'),
|
|
|
|
|
sourceDir: "_examples",
|
|
|
|
|
destDir: "_fragments"
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-06 15:45:50 -04:00
|
|
|
|
/*
|
|
|
|
|
Within this repo generated files are checked in so that we can avoid running the
|
|
|
|
|
shredder over the entire _examples dir each time someone refreshes the repo
|
|
|
|
|
( the ‘shred-full’ gulp task). The gulp ‘serve-and-watch’ shredder is only
|
|
|
|
|
a ‘partial’ shredder. It only shred’s files in directories changed during
|
|
|
|
|
the current session.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-08-08 16:55:53 -04:00
|
|
|
|
gulp.task('serve-and-sync', function (cb) {
|
|
|
|
|
execCommands(['harp server'], {}, cb);
|
2015-08-06 15:45:50 -04:00
|
|
|
|
|
2015-08-08 16:55:53 -04:00
|
|
|
|
var browserSync = require('browser-sync').create();
|
|
|
|
|
browserSync.init({
|
|
|
|
|
proxy: 'localhost:9000',
|
|
|
|
|
files: "public/docs/**/*/**/*",
|
|
|
|
|
logFileChanges: true,
|
|
|
|
|
reloadDelay: 500
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
shredWatch(_shredOptions, function() {
|
|
|
|
|
browserSync.reload();
|
|
|
|
|
});
|
2015-08-03 20:45:58 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
gulp.task('serve-and-watch', function (cb) {
|
2015-08-08 16:55:53 -04:00
|
|
|
|
execCommands(['harp server'], {}, cb);
|
|
|
|
|
shredWatch(_shredOptions);
|
|
|
|
|
});
|
2015-08-03 20:45:58 -04:00
|
|
|
|
|
2015-08-08 16:55:53 -04:00
|
|
|
|
gulp.task('shred-full', ['shred-clean'], function() {
|
|
|
|
|
docShredder.shred( _shredOptions);
|
2015-08-03 20:45:58 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
gulp.task('shred-clean', function(cb) {
|
2015-08-08 16:55:53 -04:00
|
|
|
|
var cleanPath = path.join(_shredOptions.basePath, _shredOptions.destDir, '**/*.*')
|
2015-08-03 20:45:58 -04:00
|
|
|
|
del([ cleanPath, '!**/*.ovr.*'], function (err, paths) {
|
|
|
|
|
// console.log('Deleted files/folders:\n', paths.join('\n'));
|
|
|
|
|
cb();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-08-08 16:55:53 -04:00
|
|
|
|
function shredWatch(shredOptions, postShredAction) {
|
|
|
|
|
var pattern = path.join(shredOptions.basePath, shredOptions.sourceDir, "**/*.*");
|
|
|
|
|
watch([pattern], function (event, done) {
|
|
|
|
|
console.log('Event type: ' + event.event); // added, changed, or deleted
|
|
|
|
|
console.log('Event path: ' + event.path); // The path of the modified file
|
|
|
|
|
docShredder.shredSingleDir(shredOptions, event.path).then(function () {
|
|
|
|
|
postShredAction && postShredAction();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-08-03 20:45:58 -04:00
|
|
|
|
|
|
|
|
|
// added options are: shouldLog
|
|
|
|
|
// cb is function(err, stdout, stderr);
|
|
|
|
|
function execCommands(cmds, options, cb) {
|
|
|
|
|
options = options || {};
|
|
|
|
|
options.shouldThrow = options.shouldThrow == null ? true : options.shouldThrow;
|
|
|
|
|
options.shouldLog = options.shouldLog == null ? true : options.shouldLog;
|
|
|
|
|
if (!cmds || cmds.length == 0) cb(null, null, null);
|
|
|
|
|
var exec = require('child_process').exec; // just to make it more portable.
|
|
|
|
|
exec(cmds[0], options, function(err, stdout, stderr) {
|
|
|
|
|
if (err == null) {
|
|
|
|
|
if (options.shouldLog) {
|
|
|
|
|
gutil.log('cmd: ' + cmds[0]);
|
|
|
|
|
gutil.log('stdout: ' + stdout);
|
|
|
|
|
}
|
|
|
|
|
if (cmds.length == 1) {
|
|
|
|
|
cb(err, stdout, stderr);
|
|
|
|
|
} else {
|
|
|
|
|
execCommands(cmds.slice(1), options, cb);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (options.shouldLog) {
|
|
|
|
|
gutil.log('exec error on cmd: ' + cmds[0]);
|
|
|
|
|
gutil.log('exec error: ' + err);
|
|
|
|
|
if (stdout) gutil.log('stdout: ' + stdout);
|
|
|
|
|
if (stderr) gutil.log('stderr: ' + stderr);
|
|
|
|
|
}
|
|
|
|
|
if (err && options.shouldThrow) throw err;
|
|
|
|
|
cb(err, stdout, stderr);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-06 15:45:50 -04:00
|
|
|
|
gulp.task('default', ['shred-full']);
|