ffffng/app/bower_components/lodash/lib/common/file.js

72 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2016-05-16 13:33:49 +02:00
'use strict';
2017-05-13 13:25:33 +02:00
const _ = require('lodash');
const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');
2016-05-16 13:33:49 +02:00
2017-05-13 13:25:33 +02:00
const minify = require('../common/minify.js');
2016-05-16 13:33:49 +02:00
/*----------------------------------------------------------------------------*/
/**
* Creates a [fs.copy](https://github.com/jprichardson/node-fs-extra#copy)
* function with `srcPath` and `destPath` partially applied.
*
* @memberOf file
* @param {string} srcPath The path of the file to copy.
* @param {string} destPath The path to copy the file to.
* @returns {Function} Returns the partially applied function.
*/
function copy(srcPath, destPath) {
return _.partial(fs.copy, srcPath, destPath);
}
/**
2017-05-13 13:25:33 +02:00
* Creates an object of base name and compiled template pairs that match `pattern`.
2016-05-16 13:33:49 +02:00
*
* @memberOf file
* @param {string} pattern The glob pattern to be match.
* @returns {Object} Returns the object of compiled templates.
*/
function globTemplate(pattern) {
2017-05-13 13:25:33 +02:00
return _.transform(glob.sync(pattern), (result, filePath) => {
const key = path.basename(filePath, path.extname(filePath));
2016-05-16 13:33:49 +02:00
result[key] = _.template(fs.readFileSync(filePath, 'utf8'));
}, {});
}
/**
* Creates a `minify` function with `srcPath` and `destPath` partially applied.
*
* @memberOf file
* @param {string} srcPath The path of the file to minify.
* @param {string} destPath The path to write the file to.
* @returns {Function} Returns the partially applied function.
*/
function min(srcPath, destPath) {
return _.partial(minify, srcPath, destPath);
}
/**
* Creates a [fs.writeFile](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback)
* function with `filePath` and `data` partially applied.
*
* @memberOf file
* @param {string} destPath The path to write the file to.
* @param {string} data The data to write to the file.
* @returns {Function} Returns the partially applied function.
*/
function write(destPath, data) {
return _.partial(fs.writeFile, destPath, data);
}
/*----------------------------------------------------------------------------*/
module.exports = {
2017-05-13 13:25:33 +02:00
copy,
globTemplate,
min,
write
2016-05-16 13:33:49 +02:00
};