From c49ace55873877c368cbf79d91b7a9c45858d059 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 25 Jan 2016 00:16:23 -0800 Subject: [PATCH] Add back fp build-modules files. --- fp/_baseConvert.js | 2 +- fp/_mapping.js | 62 +++++++++---------- lib/fp/build-dist.js | 14 +++-- lib/fp/build-modules.js | 112 +++++++++++++++++++++++++++++++++++ lib/fp/template/_util.jst | 4 +- lib/fp/template/alias.jst | 1 + lib/fp/template/category.jst | 2 + lib/fp/template/fp.jst | 2 + lib/fp/template/module.jst | 2 + lib/fp/template/thru.jst | 1 + lib/main/build-dist.js | 12 ++-- package.json | 1 + 12 files changed, 172 insertions(+), 43 deletions(-) create mode 100644 lib/fp/build-modules.js create mode 100644 lib/fp/template/alias.jst create mode 100644 lib/fp/template/category.jst create mode 100644 lib/fp/template/fp.jst create mode 100644 lib/fp/template/module.jst create mode 100644 lib/fp/template/thru.jst diff --git a/fp/_baseConvert.js b/fp/_baseConvert.js index 10a7d70bc..bb506f333 100644 --- a/fp/_baseConvert.js +++ b/fp/_baseConvert.js @@ -215,7 +215,7 @@ function baseConvert(util, name, func) { // Wrap the lodash method and its aliases. each(keys(_), function(key) { - each(mapping.alias[key] || [], function(alias) { + each(mapping.realToAlias[key] || [], function(alias) { _[alias] = _[key]; }); }); diff --git a/fp/_mapping.js b/fp/_mapping.js index 4f1e607ee..e8e763799 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -1,36 +1,5 @@ module.exports = { - /** Used to map method names to their aliases. */ - 'alias': { - 'ary': ['nAry'], - 'assignIn': ['extend'], - 'assignInWith': ['extendWith'], - 'filter': ['whereEq'], - 'flatten': ['unnest'], - 'flow': ['pipe'], - 'flowRight': ['compose'], - 'forEach': ['each'], - 'forEachRight': ['eachRight'], - 'get': ['path', 'prop'], - 'getOr': ['pathOr', 'propOr'], - 'head': ['first'], - 'includes': ['contains'], - 'initial': ['init'], - 'isEqual': ['equals'], - 'mapValues': ['mapObj'], - 'matchesProperty': ['pathEq'], - 'omit': ['dissoc', 'omitAll'], - 'overArgs': ['useWith'], - 'overEvery': ['allPass'], - 'overSome': ['somePass'], - 'pick': ['pickAll'], - 'propertyOf': ['propOf'], - 'rest': ['unapply'], - 'some': ['all'], - 'spread': ['apply'], - 'zipObject': ['zipObj'] - }, - /** Used to map method names to their iteratee ary. */ 'aryIteratee': { 'assignWith': 2, @@ -183,6 +152,37 @@ module.exports = { 'partialRight': true }, + /** Used to map real names to their aliases. */ + 'realToAlias': { + 'ary': ['nAry'], + 'assignIn': ['extend'], + 'assignInWith': ['extendWith'], + 'filter': ['whereEq'], + 'flatten': ['unnest'], + 'flow': ['pipe'], + 'flowRight': ['compose'], + 'forEach': ['each'], + 'forEachRight': ['eachRight'], + 'get': ['path', 'prop'], + 'getOr': ['pathOr', 'propOr'], + 'head': ['first'], + 'includes': ['contains'], + 'initial': ['init'], + 'isEqual': ['equals'], + 'mapValues': ['mapObj'], + 'matchesProperty': ['pathEq'], + 'omit': ['dissoc', 'omitAll'], + 'overArgs': ['useWith'], + 'overEvery': ['allPass'], + 'overSome': ['somePass'], + 'pick': ['pickAll'], + 'propertyOf': ['propOf'], + 'rest': ['unapply'], + 'some': ['all'], + 'spread': ['apply'], + 'zipObject': ['zipObj'] + }, + /** Used to track methods that skip `_.rearg`. */ 'skipRearg': { 'assign': true, diff --git a/lib/fp/build-dist.js b/lib/fp/build-dist.js index c88dd4e5c..7ca89e41d 100644 --- a/lib/fp/build-dist.js +++ b/lib/fp/build-dist.js @@ -44,8 +44,12 @@ function onComplete(error) { } } -async.series([ - _.partial(webpack, mappingConfig), - _.partial(webpack, fpConfig), - _.partial(minify, path.join(distPath, filename)) -], onComplete); +function build() { + async.series([ + _.partial(webpack, mappingConfig), + _.partial(webpack, fpConfig), + _.partial(minify, path.join(distPath, filename)) + ], onComplete); +} + +build(); diff --git a/lib/fp/build-modules.js b/lib/fp/build-modules.js new file mode 100644 index 000000000..955432a3f --- /dev/null +++ b/lib/fp/build-modules.js @@ -0,0 +1,112 @@ +'use strict'; + +var _ = require('lodash'), + async = require('async'), + fs = require('fs-extra'), + glob = require('glob'), + Module = require('module'), + path = require('path'); + +var mapping = require('../../fp/_mapping'); + +var templatePath = path.join(__dirname, 'template'); + +var aliasToReal = _.transform(mapping.realToAlias, function(result, aliases, realName) { + _.each(aliases, function(alias) { + result[alias] = realName; + }); +}); + +var template = _.transform(glob.sync(path.join(templatePath, '*.jst')), function(result, filePath) { + result[path.basename(filePath, '.jst')] = _.template(fs.readFileSync(filePath)); +}, {}); + +var aryMethods = _.union( + mapping.aryMethod[1], + mapping.aryMethod[2], + mapping.aryMethod[3], + mapping.aryMethod[4] +); + +var categories = [ + 'array', + 'collection', + 'date', + 'function', + 'lang', + 'math', + 'number', + 'object', + 'seq', + 'string', + 'util' +]; + +function isAlias(funcName) { + return _.has(aliasToReal, funcName); +} + +function isCategory(funcName) { + return _.includes(categories, funcName); +} + +function isThru(funcName) { + return !_.includes(aryMethods, funcName); +} + +function getTemplate(moduleName) { + var data = { + 'key': mapping.key, + 'name': _.result(aliasToReal, moduleName, moduleName) + }; + + if (isAlias(moduleName)) { + return template.alias(data); + } + if (isCategory(moduleName)) { + return template.category(data); + } + if (isThru(moduleName)) { + return template.thru(data); + } + return template.module(data); +} + +/*----------------------------------------------------------------------------*/ + +function onComplete(error) { + if (error) { + throw error; + } +} + +function build(target) { + var fpPath = path.join(target, 'fp'); + + // Glob existing lodash module paths. + var modulePaths = glob.sync(path.join(target, '*.js'), { + 'nodir': true, + 'ignore': path.join(target, '_*.js') + }); + + // Add FP alias module paths. + _.forOwn(aliasToReal, function(realName, alias) { + modulePaths.push(path.join(target, alias + '.js')); + }); + + modulePaths = _.uniq(modulePaths); + + var actions = modulePaths.map(function(modulePath) { + var moduleName = path.basename(modulePath, '.js'); + return _.partial(fs.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())); + + async.series(actions, onComplete); +} + +build(_.last(process.argv)); diff --git a/lib/fp/template/_util.jst b/lib/fp/template/_util.jst index 1fb05e435..d076f4643 100644 --- a/lib/fp/template/_util.jst +++ b/lib/fp/template/_util.jst @@ -2,9 +2,9 @@ module.exports = { 'ary': require('../ary'), 'cloneDeep': require('../cloneDeep'), 'curry': require('../curry'), - 'forEach': require('../internal/arrayEach'), + 'forEach': require('../_arrayEach'), 'isFunction': require('../isFunction'), 'iteratee': require('../iteratee'), - 'keys': require('../internal/baseKeys'), + 'keys': require('../_baseKeys'), 'rearg': require('../rearg') }; diff --git a/lib/fp/template/alias.jst b/lib/fp/template/alias.jst new file mode 100644 index 000000000..6d72710a3 --- /dev/null +++ b/lib/fp/template/alias.jst @@ -0,0 +1 @@ +module.exports = require('./<%= name %>'); diff --git a/lib/fp/template/category.jst b/lib/fp/template/category.jst new file mode 100644 index 000000000..62c2db8a1 --- /dev/null +++ b/lib/fp/template/category.jst @@ -0,0 +1,2 @@ +var convert = require('./convert'); +module.exports = convert(require('../<%= name %>')); diff --git a/lib/fp/template/fp.jst b/lib/fp/template/fp.jst new file mode 100644 index 000000000..d8887e0fe --- /dev/null +++ b/lib/fp/template/fp.jst @@ -0,0 +1,2 @@ +var _ = require('./lodash').noConflict().runInContext(); +module.exports = require('./fp/convert')(_); diff --git a/lib/fp/template/module.jst b/lib/fp/template/module.jst new file mode 100644 index 000000000..6a06f2015 --- /dev/null +++ b/lib/fp/template/module.jst @@ -0,0 +1,2 @@ +var convert = require('./convert'); +module.exports = convert('<%= name %>', require('../<%= _.result(key, name, name) %>')); diff --git a/lib/fp/template/thru.jst b/lib/fp/template/thru.jst new file mode 100644 index 000000000..de0b105e7 --- /dev/null +++ b/lib/fp/template/thru.jst @@ -0,0 +1 @@ +module.exports = require('../<%= name %>'); diff --git a/lib/main/build-dist.js b/lib/main/build-dist.js index 1f5a88359..338dfe9eb 100644 --- a/lib/main/build-dist.js +++ b/lib/main/build-dist.js @@ -22,7 +22,11 @@ function onComplete(error) { } } -async.series([ - _.partial(fs.copy, baseLodash, distLodash), - _.partial(minify, distLodash) -], onComplete); +function build() { + async.series([ + _.partial(fs.copy, baseLodash, distLodash), + _.partial(minify, distLodash) + ], onComplete); +} + +build(); diff --git a/package.json b/package.json index c142c8573..4f263bcbf 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ }, "scripts": { "build": "node lib/main/build-dist.js && node lib/fp/build-dist.js", + "build-fp": "node lib/fp/build-modules.js", "style": "jscs lodash.js lib/**/*.js", "test": "npm run build && node test/test && node test/test-fp" }