mirror of
https://github.com/nasa/openmct.git
synced 2025-01-20 19:49:17 +00:00
[API] Include dependencies during rebundling
This commit is contained in:
parent
792fda48e3
commit
8d3aec02d6
50
rebundle.js
50
rebundle.js
@ -3,29 +3,53 @@
|
||||
|
||||
var glob = require('glob'),
|
||||
fs = require('fs'),
|
||||
header = fs.readFileSync('header.txt', 'utf8');
|
||||
_ = require('lodash'),
|
||||
template = _.template(fs.readFileSync('template.txt', 'utf8'));
|
||||
|
||||
function indent(str) {
|
||||
return str.split('\n').map(function (line, index) {
|
||||
return index === 0 ? line : (' ' + line);
|
||||
function indent(str, depth) {
|
||||
return _.trimLeft(str.split('\n').map(function (line) {
|
||||
return _.repeat(' ', depth || 1) + line;
|
||||
}).filter(function (line) {
|
||||
return line.trim().length > 0;
|
||||
}).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;
|
||||
}).join('\n');
|
||||
}
|
||||
|
||||
function rebundle(file) {
|
||||
var plainJson = fs.readFileSync(file, 'utf8'),
|
||||
bundleContents = JSON.parse(plainJson),
|
||||
impls = findImpls(bundleContents),
|
||||
bundleName = file.replace("/bundle.json", ""),
|
||||
outputFile = file.replace(".json", ".js"),
|
||||
contents = [
|
||||
header,
|
||||
" legacyRegistry.register(\"",
|
||||
bundleName,
|
||||
"\", ",
|
||||
indent(plainJson),
|
||||
");\n",
|
||||
"});\n"
|
||||
].join('');
|
||||
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)))
|
||||
});
|
||||
fs.writeFileSync(outputFile, contents, 'utf8');
|
||||
}
|
||||
|
||||
|
@ -21,5 +21,14 @@
|
||||
*****************************************************************************/
|
||||
/*global define*/
|
||||
|
||||
define(['legacyRegistry'], function (legacyRegistry) {
|
||||
define([
|
||||
<%= implPaths %>
|
||||
'legacyRegistry'
|
||||
], function (
|
||||
<%= implNames %>
|
||||
legacyRegistry
|
||||
) {
|
||||
"use strict";
|
||||
|
||||
legacyRegistry.register("<%= bundleName %>", <%= bundleContents %>);
|
||||
});
|
Loading…
Reference in New Issue
Block a user