Add lib/common/util.

This commit is contained in:
John-David Dalton
2016-02-14 12:57:34 -08:00
parent d358d00531
commit c006c28f55
2 changed files with 45 additions and 20 deletions

18
lib/common/util.js Normal file
View File

@@ -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
};

View File

@@ -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);
}