ffffng/app/bower_components/lodash/lib/fp/build-modules.js

151 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2016-05-16 13:33:49 +02:00
'use strict';
2017-05-13 13:25:33 +02:00
const _ = require('lodash');
const async = require('async');
const glob = require('glob');
const path = require('path');
2016-05-16 13:33:49 +02:00
2017-05-13 13:25:33 +02:00
const file = require('../common/file');
const mapping = require('../common/mapping');
const util = require('../common/util');
2016-05-16 13:33:49 +02:00
2017-05-13 13:25:33 +02:00
const templatePath = path.join(__dirname, 'template/modules');
const template = file.globTemplate(path.join(templatePath, '*.jst'));
2016-05-16 13:33:49 +02:00
2017-05-13 13:25:33 +02:00
const aryMethods = _.union(
2016-05-16 13:33:49 +02:00
mapping.aryMethod[1],
mapping.aryMethod[2],
mapping.aryMethod[3],
mapping.aryMethod[4]
);
2017-05-13 13:25:33 +02:00
const categories = [
2016-05-16 13:33:49 +02:00
'array',
'collection',
'date',
'function',
'lang',
'math',
'number',
'object',
'seq',
'string',
'util'
];
2017-05-13 13:25:33 +02:00
const ignored = [
2016-05-16 13:33:49 +02:00
'_*.js',
'core.js',
'core.min.js',
'fp.js',
'index.js',
'lodash.js',
'lodash.min.js'
];
2017-05-13 13:25:33 +02:00
/**
* Checks if `name` is a method alias.
*
* @private
* @param {string} name The name to check.
* @returns {boolean} Returns `true` if `name` is a method alias, else `false`.
*/
function isAlias(name) {
return _.has(mapping.aliasToReal, name);
2016-05-16 13:33:49 +02:00
}
2017-05-13 13:25:33 +02:00
/**
* Checks if `name` is a category name.
*
* @private
* @param {string} name The name to check.
* @returns {boolean} Returns `true` if `name` is a category name, else `false`.
*/
function isCategory(name) {
return _.includes(categories, name);
2016-05-16 13:33:49 +02:00
}
2017-05-13 13:25:33 +02:00
/**
* Checks if `name` belongs to a method that's passed thru and not wrapped.
*
* @private
* @param {string} name The name to check.
* @returns {boolean} Returns `true` if `name` is of a pass thru method,
* else `false`.
*/
function isThru(name) {
return !_.includes(aryMethods, name);
2016-05-16 13:33:49 +02:00
}
2017-05-13 13:25:33 +02:00
/**
* Gets metadata for `func`.
*
* @private
* @param {Function} func The function to query.
* @returns {*} Returns the metadata for `func`.
*/
2016-05-16 13:33:49 +02:00
function getTemplate(moduleName) {
2017-05-13 13:25:33 +02:00
const data = {
'name': _.get(mapping.aliasToReal, moduleName, moduleName),
2016-05-16 13:33:49 +02:00
'mapping': mapping
};
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);
}
/*----------------------------------------------------------------------------*/
2017-05-13 13:25:33 +02:00
/**
* Creates FP modules at the `target` path.
*
* @private
* @param {string} target The output directory path.
*/
2016-05-16 13:33:49 +02:00
function build(target) {
target = path.resolve(target);
2017-05-13 13:25:33 +02:00
const fpPath = path.join(target, 'fp');
2016-05-16 13:33:49 +02:00
// Glob existing lodash module paths.
2017-05-13 13:25:33 +02:00
const modulePaths = glob.sync(path.join(target, '*.js'), {
2016-05-16 13:33:49 +02:00
'nodir': true,
2017-05-13 13:25:33 +02:00
'ignore': ignored.map(filename => {
2016-05-16 13:33:49 +02:00
return path.join(target, filename);
})
});
// Add FP alias and remapped module paths.
2017-05-13 13:25:33 +02:00
_.each([mapping.aliasToReal, mapping.remap], data => {
_.forOwn(data, (realName, alias) => {
const modulePath = path.join(target, alias + '.js');
2016-05-16 13:33:49 +02:00
if (!_.includes(modulePaths, modulePath)) {
modulePaths.push(modulePath);
}
});
});
2017-05-13 13:25:33 +02:00
const actions = modulePaths.map(modulePath => {
const moduleName = path.basename(modulePath, '.js');
2016-05-16 13:33:49 +02:00
return file.write(path.join(fpPath, moduleName + '.js'), getTemplate(moduleName));
});
actions.unshift(file.copy(path.join(__dirname, '../../fp'), fpPath));
actions.push(file.write(path.join(fpPath, '_falseOptions.js'), template._falseOptions()));
actions.push(file.write(path.join(fpPath, '_util.js'), template._util()));
actions.push(file.write(path.join(target, 'fp.js'), template.fp()));
actions.push(file.write(path.join(fpPath, 'convert.js'), template.convert()));
2017-05-13 13:25:33 +02:00
async.series(actions, util.pitch);
2016-05-16 13:33:49 +02:00
}
build(_.last(process.argv));