From c006c28f553b9f9c548f994e0c225a8a2e85e111 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 14 Feb 2016 12:57:34 -0800 Subject: [PATCH] Add lib/common/util. --- lib/common/util.js | 18 ++++++++++++++++ lib/fp/build-modules.js | 47 +++++++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 lib/common/util.js diff --git a/lib/common/util.js b/lib/common/util.js new file mode 100644 index 000000000..056b760d5 --- /dev/null +++ b/lib/common/util.js @@ -0,0 +1,18 @@ +'use strict'; + +var _ = require('lodash'), + fs = require('fs-extra'), + glob = require('glob'), + path = require('path'); + +/*----------------------------------------------------------------------------*/ + +function globTemplate(pattern) { + return _.transform(glob.sync(pattern), function(result, filePath) { + result[path.basename(filePath, path.extname(filePath))] = _.template(fs.readFileSync(filePath)); + }, {}); +} + +module.exports = { + 'globTemplate': globTemplate +}; diff --git a/lib/fp/build-modules.js b/lib/fp/build-modules.js index 4462647da..bc268f88d 100644 --- a/lib/fp/build-modules.js +++ b/lib/fp/build-modules.js @@ -4,15 +4,12 @@ var _ = require('lodash'), async = require('async'), fs = require('fs-extra'), glob = require('glob'), - path = require('path'); + path = require('path'), + util = require('../common/util'); -var mapping = require('../../fp/_mapping'); - -var templatePath = path.join(__dirname, 'template/modules'); - -var template = _.transform(glob.sync(path.join(templatePath, '*.jst')), function(result, filePath) { - result[path.basename(filePath, '.jst')] = _.template(fs.readFileSync(filePath)); -}, {}); +var mapping = require('../../fp/_mapping'), + templatePath = path.join(__dirname, 'template/modules'), + template = util.globTemplate(path.join(templatePath, '*.jst')); var aryMethods = _.union( mapping.aryMethod[1], @@ -35,6 +32,18 @@ var categories = [ 'util' ]; +var ignored = [ + '_*.js', + 'core.js', + 'fp.js', + 'index.js', + 'lodash.js' +]; + +function copyFile(srcPath, destPath) { + return _.partial(fs.copy, srcPath, destPath); +} + function isAlias(funcName) { return _.has(mapping.aliasToReal, funcName); } @@ -65,6 +74,10 @@ function getTemplate(moduleName) { return template.module(data); } +function writeFile(filePath, data) { + return _.partial(fs.writeFile, filePath, data); +} + /*----------------------------------------------------------------------------*/ function onComplete(error) { @@ -79,13 +92,7 @@ function build(target) { // Glob existing lodash module paths. var modulePaths = glob.sync(path.join(target, '*.js'), { 'nodir': true, - 'ignore': [ - '_*.js', - 'core.js', - 'fp.js', - 'index.js', - 'lodash.js' - ].map(function(filename) { + 'ignore': ignored.map(function(filename) { return path.join(target, filename); }) }); @@ -103,13 +110,13 @@ function build(target) { var actions = modulePaths.map(function(modulePath) { var moduleName = path.basename(modulePath, '.js'); - return _.partial(fs.writeFile, path.join(fpPath, moduleName + '.js'), getTemplate(moduleName)); + return writeFile(path.join(fpPath, moduleName + '.js'), getTemplate(moduleName)); }); - actions.unshift(_.partial(fs.copy, path.join(__dirname, '../../fp'), fpPath)); - actions.push(_.partial(fs.writeFile, path.join(target, 'fp.js'), template.fp())); - actions.push(_.partial(fs.writeFile, path.join(fpPath, 'convert.js'), template.convert())); - actions.push(_.partial(fs.writeFile, path.join(fpPath, '_util.js'), template._util())); + actions.unshift(copyFile(path.join(__dirname, '../../fp'), fpPath)); + actions.push(writeFile(path.join(target, 'fp.js'), template.fp())); + actions.push(writeFile(path.join(fpPath, 'convert.js'), template.convert())); + actions.push(writeFile(path.join(fpPath, '_util.js'), template._util())); async.series(actions, onComplete); }