UNPKG

1.1 kBJavaScriptView Raw
1'use strict';
2
3var handlebars = require('handlebars');
4var path = require('path');
5var fs = require('fs');
6var xtend = require('xtend');
7
8var templatePath = path.join(__dirname, 'index.hbs');
9var template = fs.readFileSync(templatePath, 'utf8');
10var index = handlebars.compile(template);
11var genPath = path.join(__dirname, '..', 'loaders');
12
13/**
14 * Generates an index for all the loader files to allow requiring them like this on the client side:
15 * require('bromote').foo(function (foo) { ... }
16 * @name genIndex
17 * @function
18 * @param fullPaths {Array} of full paths to the loader files
19 * @param cb {Function} calls back with error or none
20 */
21var genIndex = module.exports = function (fullPaths, cb) {
22 if (!fullPaths.length) cb(null);
23
24 var keys = fullPaths.map(function (p) {
25 var extlen = path.extname(p).length;
26 var filename = path.basename(p);
27 return extlen ? filename.slice(0, -extlen) : filename;
28 });
29
30 var s = index({ keys: keys });
31
32 var filePath = path.join(genPath, 'index.js');
33 fs.writeFile(filePath, s, 'utf8', cb);
34};