Skip to content

Commit

Permalink
allow watch of copy files. Fixes #33.
Browse files Browse the repository at this point in the history
  • Loading branch information
chmontgomery committed Sep 29, 2014
1 parent bba66bf commit 880aa82
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 71 deletions.
6 changes: 5 additions & 1 deletion examples/full/bundle.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ module.exports = {
copy: [
{
src: './bower_components/bootstrap/dist/fonts/**/*.*',
base: './bower_components/bootstrap/dist/'
base: './bower_components/bootstrap/dist/',
watch: false
},
{
src: './partials/**/*.*'
},
'./images/**/*.*'
]
Expand Down
3 changes: 3 additions & 0 deletions examples/full/partials/a-partial-file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
yo
</div>
3 changes: 3 additions & 0 deletions examples/full/public/partials/a-partial-file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
yo
</div>
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ gulp.task('test-cover', 'Unit tests and coverage', function (cb) {
thresholds : {
statements : 95,
branches : 92,
functions : 95,
functions : 94,
lines : 95
},
coverageDirectory : 'coverage',
Expand Down
1 change: 1 addition & 0 deletions lib/service/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function Logger() {
Logger.prototype.log = function () {
var config = cache.get('config');
if (!config || !config.options || config.options.quietMode !== true) {
// replace with gulp logger once it's done? https://github.com/gulpjs/gulp-util/issues/33
gutil.log.apply(gutil.log, arguments);
}
};
Expand Down
16 changes: 16 additions & 0 deletions lib/stream-bundles-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var logger = require('./service/logger'),
gutil = require('gulp-util'),
warnPrefix = gutil.colors.bgYellow.black('WARN');

function StreamBundlesUtil() {
}

StreamBundlesUtil.prototype.warnIfNoBundleProperty = function (config) {
if (config && !config.bundle && config.file && config.file.relative) { // can guarantee !!file b/c (config instanceof Config)
logger.log(warnPrefix, "No '" + gutil.colors.cyan('bundle') +
"' property found in " + gutil.colors.magenta(config.file.relative) + ". Did you mean to define one?");
}
};

// naturally a singleton because node's require caches the value assigned to module.exports
module.exports = new StreamBundlesUtil();
54 changes: 51 additions & 3 deletions lib/stream-bundles-watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ var through = require('through2'),
bundleDone = require('./watch/bundle-done'),
initOptionDefaults = require('./init-option-defaults'),
results = require('./results').incremental,
streamFiles = require('./stream-files');
streamFiles = require('./stream-files'),
streamCopy = require('./stream-copy'),
streamBundlesUtil = require('./stream-bundles-util');

function _bundle(config, env) {
var bundles = config.bundle,
Expand Down Expand Up @@ -126,15 +128,61 @@ function _bundle(config, env) {

}

function watchStringCopyStream(config, item, base) {
gulp.watch(item)
.on('change', function (file) { // log changed file?
streamCopy.getStringCopyStream(item, base)
.pipe(gulp.dest(config.options.dest));
});
}

function watchObjectCopyStream(config, item, base) {
var watchPath = pathifySrc(item.src, base);
gulp.watch(watchPath)
.on('change', function (file) { // log changed file?
streamCopy.getObjectCopyStream(item, base)
.pipe(gulp.dest(config.options.dest));
});
}

function _copy(config) {
var base = (config.options) ? config.options.base : '.'; // can guarantee !!options b/c (config instanceof Config)

logger.log("Starting '" + gutil.colors.cyan("watch") + "' for files to copy...");

if (typeof config.copy === 'string') {
watchStringCopyStream(config, config.copy, base);
} else if (util.isArray(config.copy)) {
_.forEach(config.copy, function (item) {
if (typeof item === 'string') {
return watchStringCopyStream(config, item, base);
} else if (typeof item === 'object' && !util.isArray(item) &&
config.copy.watch !== false) {
return watchObjectCopyStream(config, item, base);
}
streamCopy.throwUnsupportedSyntaxError();
});
} else if (typeof config.copy === 'object' && config.copy.watch !== false) {
watchObjectCopyStream(config, config.copy, base);
} else {
streamCopy.throwUnsupportedSyntaxError();
}
}

function bundle(config) {

if (config.bundle) {
if (config.options && config.options.bundleAllEnvironments) { // can guarantee !!options b/c (config instanceof Config)
bundleAllEnvironments(config, _bundle);
} else {
_bundle(config, process.env.NODE_ENV);
}
} else {
throw new gutil.PluginError('gulp-bundle-assets', 'Missing required config property "bundle"');
}

streamBundlesUtil.warnIfNoBundleProperty(config);

if (config.copy) {
_copy(config);
}
}

Expand Down
89 changes: 26 additions & 63 deletions lib/stream-bundles.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
var path = require('path'),
util = require('util'),
var util = require('util'),
BundleKeys = require('./model/bundle-keys'),
gulp = require('gulp'),
using = require('./using'),
gutil = require('gulp-util'),
logger = require('./service/logger'),
_ = require('lodash'),
Expand All @@ -11,46 +8,8 @@ var path = require('path'),
bundleAllEnvironments = require('./bundle-all-environments'),
initOptionDefaults = require('./init-option-defaults'),
streamFiles = require('./stream-files'),
warnPrefix = gutil.colors.bgYellow.black('WARN');

// assume configBase will ALWAYS be defined (and defaulted to '.')
function getCustomBase(configBase, relativeBase) {
if (!relativeBase) {
return configBase;
}
return path.join(configBase, relativeBase);
}

/**
*
* @param {String} item
* @param base
* @returns {*}
*/
function getStringCopyStream(item, base) {
return gulp.src(pathifySrc(item, base), { base: base })
.pipe(using.copy(base));
}

/**
* @param {Object} item
* @param base
* @returns {*}
*/
function getObjectCopyStream(item, base) {
return gulp.src(pathifySrc(item.src, base), { base: getCustomBase(base, item.base) })
.pipe(using.copy(base));
}

function getCopyStream(item, base) {
if (typeof item === 'string') {
return getStringCopyStream(item, base);
} else if (typeof item === 'object' && !util.isArray(item)) {
return getObjectCopyStream(item, base);
}
throw new gutil.PluginError('gulp-bundle-assets', 'Unsupported syntax for copy. See here for supported variations: ' +
'https://github.com/chmontgomery/gulp-bundle-assets/blob/master/examples/copy/bundle.config.js');
}
streamCopy = require('./stream-copy'),
streamBundlesUtil = require('./stream-bundles-util');

function _bundle(config, env) {
var streams = [],
Expand Down Expand Up @@ -98,38 +57,42 @@ function _bundle(config, env) {
return streams;
}


function bundle(config) {
function _copy(config) {
var streams = [],
copy = config.copy,
base = (config.options) ? config.options.base : '.'; // can guarantee !!options b/c (config instanceof Config)

if (typeof copy === 'string') {
streams.push(streamCopy.getStringCopyStream(copy, base));
} else if (util.isArray(copy)) {
_.forEach(copy, function (item) {
streams.push(streamCopy.getCopyStream(item, base));
});
} else if (typeof copy === 'object') {
streams.push(streamCopy.getObjectCopyStream(copy, base));
} else {
streamCopy.throwUnsupportedSyntaxError();
}

return streams;
}


function bundle(config) {
var streams = [];

if (config.bundle) {
if (config.options && config.options.bundleAllEnvironments) { // can guarantee !!options b/c (config instanceof Config)
streams = streams.concat(bundleAllEnvironments(config, _bundle));
} else {
streams = streams.concat(_bundle(config, process.env.NODE_ENV));
}
} else if (config && config.file && config.file.relative) { // can guarantee !!file b/c (config instanceof Config)
// replace with gulp logger once they're done with it https://github.com/gulpjs/gulp-util/issues/33
logger.log(warnPrefix, "No '" + gutil.colors.cyan('bundle') +
"' property found in " + gutil.colors.magenta(config.file.relative) + ". Did you mean to define one?");
}

if (copy) {

if (typeof copy === 'string') {
streams.push(getStringCopyStream(copy, base));
} else if (util.isArray(copy)) {
_.forEach(copy, function (item) {
streams.push(getCopyStream(item, base));
});
} else if (typeof copy === 'object') {
streams.push(getObjectCopyStream(copy, base));
} else {
throw new gutil.PluginError('gulp-bundle-assets', 'Unsupported syntax for copy. Should be a string, array or object.');
}
streamBundlesUtil.warnIfNoBundleProperty(config);

if (config.copy) {
streams = streams.concat(_copy(config));
}

return streams;
Expand Down
54 changes: 54 additions & 0 deletions lib/stream-copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var gulp = require('gulp'),
gutil = require('gulp-util'),
util = require('util'),
path = require('path'),
using = require('./using'),
pathifySrc = require('./pathify-config-src');

function StreamCopy() {
}

// assume configBase will ALWAYS be defined (and defaulted to '.')
StreamCopy.prototype.getCustomBase = function (configBase, relativeBase) {
if (!relativeBase) {
return configBase;
}
return path.join(configBase, relativeBase);
};

/**
* @param {String} item
* @param base
* @returns {*}
*/
StreamCopy.prototype.getStringCopyStream = function (item, base) {
return gulp.src(pathifySrc(item, base), { base: base })
.pipe(using.copy(base));
};

/**
* @param {Object} item
* @param base
* @returns {*}
*/
StreamCopy.prototype.getObjectCopyStream = function (item, base) {
return gulp.src(pathifySrc(item.src, base), { base: this.getCustomBase(base, item.base) })
.pipe(using.copy(base));
};

StreamCopy.prototype.getCopyStream = function (item, base) {
if (typeof item === 'string') {
return this.getStringCopyStream(item, base);
} else if (typeof item === 'object' && !util.isArray(item)) {
return this.getObjectCopyStream(item, base);
}
this.throwUnsupportedSyntaxError();
};

StreamCopy.prototype.throwUnsupportedSyntaxError = function () {
throw new gutil.PluginError('gulp-bundle-assets', 'Unsupported syntax for copy. See here for supported variations: ' +
'https://github.com/chmontgomery/gulp-bundle-assets/blob/master/examples/copy/bundle.config.js');
};

// naturally a singleton because node's require caches the value assigned to module.exports
module.exports = new StreamCopy();
7 changes: 4 additions & 3 deletions test/integ/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,16 @@ describe('integration tests', function () {
file.relative === 'fonts/glyphicons-halflings-regular.ttf' ||
file.relative === 'fonts/glyphicons-halflings-regular.woff' ||
file.relative === 'images/empire_icon.png' ||
file.relative === 'images/rebel_icon.png') {
file.relative === 'images/rebel_icon.png' ||
file.relative === 'partials/a-partial-file.html' ) {
staticFileCount++;
} else {
helpers.errorUnexpectedFileInStream(file);
}
fileCount++;
}, function () {
(fileCount).should.eql(22);
(staticFileCount).should.eql(14);
(fileCount).should.eql(23);
(staticFileCount).should.eql(15);
});

});
Expand Down
Loading

0 comments on commit 880aa82

Please sign in to comment.