Add Hash util and split out file helper.

This commit is contained in:
John-David Dalton
2016-04-06 23:22:22 -07:00
parent 58afd8c364
commit 0588dcb3e9
9 changed files with 94 additions and 50 deletions

36
lib/common/file.js Normal file
View File

@@ -0,0 +1,36 @@
'use strict';
var _ = require('lodash'),
fs = require('fs-extra'),
glob = require('glob'),
path = require('path');
var minify = require('../common/minify.js');
/*----------------------------------------------------------------------------*/
function copy(srcPath, destPath) {
return _.partial(fs.copy, srcPath, destPath);
}
function globTemplate(pattern) {
return _.transform(glob.sync(pattern), function(result, filePath) {
var key = path.basename(filePath, path.extname(filePath));
result[key] = _.template(fs.readFileSync(filePath, 'utf8'));
}, {});
}
function min(srcPath, destPath) {
return _.partial(minify, srcPath, destPath);
}
function write(filePath, data) {
return _.partial(fs.writeFile, filePath, data);
}
module.exports = {
'copy': copy,
'globTemplate': globTemplate,
'min': min,
'write': write
};

9
lib/common/mapping.js Normal file
View File

@@ -0,0 +1,9 @@
'use strict';
var _mapping = require('../../fp/_mapping'),
util = require('./util'),
Hash = util.Hash;
/*----------------------------------------------------------------------------*/
module.exports = new Hash(_mapping);

View File

@@ -1,4 +1,11 @@
module.exports = {
'use strict';
var util = require('./util'),
Hash = util.Hash;
/*----------------------------------------------------------------------------*/
module.exports = new Hash({
'compress': {
'pure_getters': true,
'unsafe': true,
@@ -13,4 +20,4 @@ module.exports = {
'comments': /^!|@cc_on|@license|@preserve/i,
'max_line_len': 500
}
};
});

View File

@@ -1,36 +1,27 @@
'use strict';
var _ = require('lodash'),
fs = require('fs-extra'),
glob = require('glob'),
path = require('path');
var minify = require('../common/minify.js');
var _ = require('lodash');
/*----------------------------------------------------------------------------*/
function copyFile(srcPath, destPath) {
return _.partial(fs.copy, srcPath, destPath);
/**
* Creates a hash object. If a `properties` object is provided, its own
* enumerable properties are assigned to the created object.
*
* @memberOf util
* @param {Object} [properties] The properties to assign to the object.
* @returns {Object} Returns the new hash object.
*/
function Hash(properties) {
return _.transform(properties, function(result, value, key) {
result[key] = (_.isPlainObject(value) && !(value instanceof Hash))
? new Hash(value)
: value;
}, this);
}
function globTemplate(pattern) {
return _.transform(glob.sync(pattern), function(result, filePath) {
var key = path.basename(filePath, path.extname(filePath));
result[key] = _.template(fs.readFileSync(filePath, 'utf8'));
}, {});
}
function minFile(srcPath, destPath) {
return _.partial(minify, srcPath, destPath);
}
function writeFile(filePath, data) {
return _.partial(fs.writeFile, filePath, data);
}
Hash.prototype = Object.create(null);
module.exports = {
'copyFile': copyFile,
'globTemplate': globTemplate,
'minFile': minFile,
'writeFile': writeFile
'Hash': Hash
};