1'use strict';
2
3const _ = require('lodash');
4const fs = require('fs-extra');
5const glob = require('glob');
6const path = require('path');
7
8const minify = require('../common/minify.js');
9
10/*----------------------------------------------------------------------------*/
11
12/**
13 * Creates a [fs.copy](https://github.com/jprichardson/node-fs-extra#copy)
14 * function with `srcPath` and `destPath` partially applied.
15 *
16 * @memberOf file
17 * @param {string} srcPath The path of the file to copy.
18 * @param {string} destPath The path to copy the file to.
19 * @returns {Function} Returns the partially applied function.
20 */
21function copy(srcPath, destPath) {
22  return _.partial(fs.copy, srcPath, destPath);
23}
24
25/**
26 * Creates an object of base name and compiled template pairs that match `pattern`.
27 *
28 * @memberOf file
29 * @param {string} pattern The glob pattern to be match.
30 * @returns {Object} Returns the object of compiled templates.
31 */
32function globTemplate(pattern) {
33  return _.transform(glob.sync(pattern), (result, filePath) => {
34    const key = path.basename(filePath, path.extname(filePath));
35    result[key] = _.template(fs.readFileSync(filePath, 'utf8'));
36  }, {});
37}
38
39/**
40 * Creates a `minify` function with `srcPath` and `destPath` partially applied.
41 *
42 * @memberOf file
43 * @param {string} srcPath The path of the file to minify.
44 * @param {string} destPath The path to write the file to.
45 * @returns {Function} Returns the partially applied function.
46 */
47function min(srcPath, destPath) {
48  return _.partial(minify, srcPath, destPath);
49}
50
51/**
52 * Creates a [fs.writeFile](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback)
53 * function with `filePath` and `data` partially applied.
54 *
55 * @memberOf file
56 * @param {string} destPath The path to write the file to.
57 * @param {string} data The data to write to the file.
58 * @returns {Function} Returns the partially applied function.
59 */
60function write(destPath, data) {
61  return _.partial(fs.writeFile, destPath, data);
62}
63
64/*----------------------------------------------------------------------------*/
65
66module.exports = {
67  copy,
68  globTemplate,
69  min,
70  write
71};
72