e66b7cdd9761d5541a87f2047889ed2463815098
[motion.git] / public / bower_components / lodash / lib / fp / build-modules.js
1 'use strict';
2
3 var _ = require('lodash'),
4     async = require('async'),
5     glob = require('glob'),
6     path = require('path');
7
8 var util = require('../common/util');
9
10 var mapping = require('../../fp/_mapping'),
11     templatePath = path.join(__dirname, 'template/modules'),
12     template = util.globTemplate(path.join(templatePath, '*.jst'));
13
14 var aryMethods = _.union(
15   mapping.aryMethod[1],
16   mapping.aryMethod[2],
17   mapping.aryMethod[3],
18   mapping.aryMethod[4]
19 );
20
21 var categories = [
22   'array',
23   'collection',
24   'date',
25   'function',
26   'lang',
27   'math',
28   'number',
29   'object',
30   'seq',
31   'string',
32   'util'
33 ];
34
35 var ignored = [
36   '_*.js',
37   'core.js',
38   'fp.js',
39   'index.js',
40   'lodash.js'
41 ];
42
43 function isAlias(funcName) {
44   return _.has(mapping.aliasToReal, funcName);
45 }
46
47 function isCategory(funcName) {
48   return _.includes(categories, funcName);
49 }
50
51 function isThru(funcName) {
52   return !_.includes(aryMethods, funcName);
53 }
54
55 function getTemplate(moduleName) {
56   var data = {
57     'name': _.result(mapping.aliasToReal, moduleName, moduleName),
58     'mapping': mapping
59   };
60
61   if (isAlias(moduleName)) {
62     return template.alias(data);
63   }
64   if (isCategory(moduleName)) {
65     return template.category(data);
66   }
67   if (isThru(moduleName)) {
68     return template.thru(data);
69   }
70   return template.module(data);
71 }
72
73 /*----------------------------------------------------------------------------*/
74
75 function onComplete(error) {
76   if (error) {
77     throw error;
78   }
79 }
80
81 function build(target) {
82   target = path.resolve(target);
83
84   var fpPath = path.join(target, 'fp');
85
86   // Glob existing lodash module paths.
87   var modulePaths = glob.sync(path.join(target, '*.js'), {
88     'nodir': true,
89     'ignore': ignored.map(function(filename) {
90       return path.join(target, filename);
91     })
92   });
93
94   // Add FP alias and remapped module paths.
95   _.each([mapping.aliasToReal, mapping.remap], function(data) {
96     _.forOwn(data, function(realName, alias) {
97       var modulePath = path.join(target, alias + '.js');
98       if (!_.startsWith(alias, '_') &&
99           !_.includes(modulePaths, modulePath)) {
100         modulePaths.push(modulePath);
101       }
102     });
103   });
104
105   var actions = modulePaths.map(function(modulePath) {
106     var moduleName = path.basename(modulePath, '.js');
107     return util.writeFile(path.join(fpPath, moduleName + '.js'), getTemplate(moduleName));
108   });
109
110   actions.unshift(util.copyFile(path.join(__dirname, '../../fp'), fpPath));
111   actions.push(util.writeFile(path.join(target, 'fp.js'), template.fp()));
112   actions.push(util.writeFile(path.join(fpPath, 'convert.js'), template.convert()));
113   actions.push(util.writeFile(path.join(fpPath, '_util.js'), template._util()));
114
115   async.series(actions, onComplete);
116 }
117
118 build(_.last(process.argv));