2016-01-07 18:42:08 +00:00
|
|
|
// Temporary utility script to rewrite bundle.json
|
|
|
|
// files as bundle.js files.
|
|
|
|
|
|
|
|
var glob = require('glob'),
|
|
|
|
fs = require('fs'),
|
2016-01-07 23:00:51 +00:00
|
|
|
_ = require('lodash'),
|
|
|
|
template = _.template(fs.readFileSync('template.txt', 'utf8'));
|
2016-01-07 18:42:08 +00:00
|
|
|
|
2016-01-07 23:00:51 +00:00
|
|
|
function indent(str, depth) {
|
|
|
|
return _.trimLeft(str.split('\n').map(function (line) {
|
|
|
|
return _.repeat(' ', depth || 1) + line;
|
2016-01-07 18:45:00 +00:00
|
|
|
}).filter(function (line) {
|
|
|
|
return line.trim().length > 0;
|
2016-01-07 23:00:51 +00:00
|
|
|
}).join('\n'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function findImpls(bundleContents) {
|
|
|
|
return _(bundleContents.extensions || {})
|
|
|
|
.map().flatten().pluck('implementation').filter().value();
|
|
|
|
}
|
|
|
|
|
|
|
|
function toIdentifier(impl) {
|
|
|
|
var parts = impl.replace(".js", "").split('/');
|
|
|
|
return parts[parts.length - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function toPath(impl) {
|
|
|
|
return "\"./src/" + impl.replace(".js", "") + "\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceImpls(bundleText) {
|
|
|
|
var rx = /"implementation": "([^"]*)"/;
|
|
|
|
return bundleText.split('\n').map(function (line) {
|
|
|
|
var m = line.match(rx);
|
|
|
|
return m !== null ?
|
|
|
|
line.replace(rx, '"implementation": ' + toIdentifier(m[1])) :
|
|
|
|
line;
|
2016-01-07 18:42:08 +00:00
|
|
|
}).join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
function rebundle(file) {
|
|
|
|
var plainJson = fs.readFileSync(file, 'utf8'),
|
2016-01-07 23:00:51 +00:00
|
|
|
bundleContents = JSON.parse(plainJson),
|
|
|
|
impls = findImpls(bundleContents),
|
2016-01-07 18:42:08 +00:00
|
|
|
bundleName = file.replace("/bundle.json", ""),
|
|
|
|
outputFile = file.replace(".json", ".js"),
|
2016-01-07 23:00:51 +00:00
|
|
|
contents = template({
|
|
|
|
bundleName: bundleName,
|
|
|
|
implPaths: indent(impls.map(toPath).concat([""]).join(",\n")),
|
|
|
|
implNames: indent(impls.map(toIdentifier).concat([""]).join(",\n")),
|
|
|
|
bundleContents: indent(replaceImpls(JSON.stringify(bundleContents, null, 4)))
|
|
|
|
});
|
2016-01-07 18:42:08 +00:00
|
|
|
fs.writeFileSync(outputFile, contents, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
glob('**/bundle.json', {}, function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
files.forEach(rebundle);
|
|
|
|
});
|
|
|
|
|