blob: 43902e01c6b09a7817c41a7d19c3b11cb6cbcf36 [file] [log] [blame]
Matteo Scandolo280dcd32016-05-16 09:59:38 -07001'use strict';
2
3var _ = require('lodash'),
4 async = require('async'),
5 glob = require('glob'),
6 path = require('path');
7
8var file = require('../common/file'),
9 mapping = require('../common/mapping');
10
11var templatePath = path.join(__dirname, 'template/modules'),
12 template = file.globTemplate(path.join(templatePath, '*.jst'));
13
14var aryMethods = _.union(
15 mapping.aryMethod[1],
16 mapping.aryMethod[2],
17 mapping.aryMethod[3],
18 mapping.aryMethod[4]
19);
20
21var 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
35var ignored = [
36 '_*.js',
37 'core.js',
38 'core.min.js',
39 'fp.js',
40 'index.js',
41 'lodash.js',
42 'lodash.min.js'
43];
44
45function isAlias(funcName) {
46 return _.has(mapping.aliasToReal, funcName);
47}
48
49function isCategory(funcName) {
50 return _.includes(categories, funcName);
51}
52
53function isThru(funcName) {
54 return !_.includes(aryMethods, funcName);
55}
56
57function getTemplate(moduleName) {
58 var data = {
59 'name': _.result(mapping.aliasToReal, moduleName, moduleName),
60 'mapping': mapping
61 };
62
63 if (isAlias(moduleName)) {
64 return template.alias(data);
65 }
66 if (isCategory(moduleName)) {
67 return template.category(data);
68 }
69 if (isThru(moduleName)) {
70 return template.thru(data);
71 }
72 return template.module(data);
73}
74
75/*----------------------------------------------------------------------------*/
76
77function onComplete(error) {
78 if (error) {
79 throw error;
80 }
81}
82
83function build(target) {
84 target = path.resolve(target);
85
86 var fpPath = path.join(target, 'fp');
87
88 // Glob existing lodash module paths.
89 var modulePaths = glob.sync(path.join(target, '*.js'), {
90 'nodir': true,
91 'ignore': ignored.map(function(filename) {
92 return path.join(target, filename);
93 })
94 });
95
96 // Add FP alias and remapped module paths.
97 _.each([mapping.aliasToReal, mapping.remap], function(data) {
98 _.forOwn(data, function(realName, alias) {
99 var modulePath = path.join(target, alias + '.js');
100 if (!_.includes(modulePaths, modulePath)) {
101 modulePaths.push(modulePath);
102 }
103 });
104 });
105
106 var actions = modulePaths.map(function(modulePath) {
107 var moduleName = path.basename(modulePath, '.js');
108 return file.write(path.join(fpPath, moduleName + '.js'), getTemplate(moduleName));
109 });
110
111 actions.unshift(file.copy(path.join(__dirname, '../../fp'), fpPath));
112 actions.push(file.write(path.join(fpPath, '_falseOptions.js'), template._falseOptions()));
113 actions.push(file.write(path.join(fpPath, '_util.js'), template._util()));
114 actions.push(file.write(path.join(target, 'fp.js'), template.fp()));
115 actions.push(file.write(path.join(fpPath, 'convert.js'), template.convert()));
116
117 async.series(actions, onComplete);
118}
119
120build(_.last(process.argv));