Skip to content

Instantly share code, notes, and snippets.

@whroman
Last active August 29, 2015 14:00
Show Gist options
  • Save whroman/11160055 to your computer and use it in GitHub Desktop.
Save whroman/11160055 to your computer and use it in GitHub Desktop.
DRY up your file-include-intensive scripts with batchPaths.js. Use cases include Grunt and Gulp task declarations.
var batchPaths = function() {
var _prefix = '';
var _suffix = '';
// _all (Array)
// Populated by calling batchPaths.add(Array).
var _all = [];
// Method: prefix
// Args: toPrefix (String)
// Sets internal value that will be preixed onto each string of Array
// passed to batchPaths.add() before being stored in batchPaths.files
function prefix (toPrefix) {
_prefix = toPrefix;
return this;
};
// Method: suffix
// Args: _suffix (String)
// Sets internal value that will be suffixed onto each string of Array
// passed to batchPaths.add() before being stored in batchPaths.files
function suffix (toSuffix) {
_suffix = toSuffix
return this
};
// Method: add
// Args: items (Array of strings)
// Appends each array item to batchPaths._all after being prefixed and suffixed with
// most recently declared batchPaths.prefix and batchPaths.suffix, respectively.
function add (items) {
for (var i = 0; i < items.length; i++) {
_all.push(_prefix + items[i] + _suffix)
}
return this
};
// Method: all
// Args: none
// Returns Array comprised of all strings that have been stored in current batchPaths Object via method add()
function all () {
return _all
};
return {
prefix : prefix,
suffix : suffix,
add : add,
all : all,
}
}
module.exports = batchPaths();
var batchPaths=function(){function t(t){return i=t,this}function n(t){return a=t,this}function r(t){for(var n=0;n<t.length;n++)e.push(i+t[n]+a);return this}function u(){return e}var i="",a="",e=[];return{prefix:t,suffix:n,add:r,all:u}};module.exports=batchPaths();
// Sample Gulpfile of a somewhat complex Angular app, including Bower components.
//
// batchPaths.js is used in gulpPaths.js to DRY-ly declare file paths and increase
// cleanliness and readability of Task declarations.
var gulp = require('gulp'),
gConcat = require('gulp-concat'),
gUglify = require('gulp-uglify');
var config = require('./gulpPaths.js');
var path = config.path;
var js = config.js;
gulp.task(
'build-scripts',
function() {
gulp
.src(
js.src
)
.pipe(
gConcat(js.build)
)
.pipe(
gUglify({
mangle: false
})
)
.pipe(
gulp.dest('./')
)
}
)
// Declares all file paths to be used in gulp task declarations.
var batchPaths = require('./batchPaths.js');
var path = {},
js = {};
path.root = '../';
path.resources = path.root + 'Resources/';
path.build = path.resources + 'production/';
path.bower = path.resources + 'bower_components/';
js.root = path.resources + 'js/';
js.models = js.root + 'app/models/';
js.controllers = js.root + 'app/controllers/';
js.collections = js.root + 'app/collections/';
js.filters = js.root + 'app/filters/';
js.directives = js.root + 'app/directives/';
js.src = batchPaths.prefix('.js')
.suffix(path.bower)
.add([
'modernizr/modernizr',
'angular/angular',
'angular-route/angular-route.min',
'jquery/dist/jquery',
'lodash/dist/lodash.underscore.min',
'restangular/dist/restangular.min',
'angular-foundation/mm-foundation-tpls.min',
'foundation/js/foundation.min'
])
.suffix(js.root)
.add([
'app/app'
])
.suffix(js.controllers)
.add([
'productList',
'customerList',
'orderList',
'invoiceItemModal',
'paymentModal',
])
.suffix(js.collections)
.add([
'lineItems/lineItems',
'fees/fees',
'customers/customers',
'products/products'
])
.suffix(js.models)
.add([
'invoice/invoice',
'customer/customer',
'quoteId/quoteId'
])
.suffix(js.filters)
.add([
'truncate'
])
.suffix(js.directives)
.add([
'focusWhen'
])
.all()
module.exports = {
path: path,
js : js
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment