Add support for embedded files.

This commit is contained in:
Lorenz Gardner
2015-01-06 19:05:01 -06:00
parent b615e125c0
commit 97dbd37068
5 changed files with 69 additions and 14 deletions
+3 -4
View File
@@ -1,9 +1,7 @@
var mdeps = require("module-deps"),
through = require("through"),
async = require("async"),
path = require("path"),
fs = require("fs"),
uglify = require("uglify-js"),
//uglify = require("uglify-js"),
builtins = require("builtins");
@@ -62,6 +60,7 @@ function bundle (input, complete) {
},
filter: function (id) {
if (id == 'nexeres') { return false; } //treat our new moudle as a builtin module
return !~builtins.indexOf(id);
}
});
@@ -107,7 +106,7 @@ var loader = function (deps, key) {
var argv3 = process.argv[3];
if (argv3) {
argv3 = pathModule.relative(process.execPath, argv3);
argv3 = pathModule.relative(pathModule.dirname(process.execPath), argv3);
argv3 = pathModule.resolve(pathModule.dirname(key), argv3);
}
if (process.argv[2] == "--child_process" && deps[argv3]) {
+37
View File
@@ -0,0 +1,37 @@
var path = require("path"),
fs = require("fs");
function embed(resourceFiles, resourceRoot, complete) {
function encode(filePath) {
return fs.readFileSync(filePath).toString('base64');
}
resourceFiles = resourceFiles || [];
resourceRoot = resourceRoot || "";
if (!Array.isArray(resourceFiles)) {
throw new Error("Bad Argument: resourceFiles is not an array");
}
var buffer = "var embeddedFiles = {\n";
for (var i = 0; i < resourceFiles.length; ++i) {
buffer += JSON.stringify(path.relative(resourceRoot, resourceFiles[i])) + ': "';
buffer += encode(resourceFiles[i]) + '",\n';
}
buffer += "\n};\n\nmodule.exports.keys = function () { return Object.keys(embeddedFiles); }\n\nmodule.exports.get = ";
buffer += accessor.toString();
complete(null, buffer);
}
var accessor = function (key) {
if (embeddedFiles.hasOwnProperty(key)) {
return new Buffer(embeddedFiles[key], 'base64');
}
else {
//file was not embedded, throw err.
throw new Error('Embedded file not found');
}
}
module.exports = embed;
+15 -2
View File
@@ -9,6 +9,7 @@ ProgressBar = require("progress"),
child_process = require("child_process"),
glob = require("glob"),
bundle = require("./bundle"),
embed = require("./embed"),
os = require("os"),
targz = require('tar.gz'),
spawn = child_process.spawn;
@@ -60,6 +61,18 @@ exports.compile = function (options, complete) {
fs.writeFile(bundlePath, source, next);
},
function embedResources(next) {
_log("embedResources %s", options.resourceFiles);
embed(options.resourceFiles, options.resourceRoot, next);
},
function writeResources (resources, next) {
resourcePath = path.join(nodeCompiler.dir, "lib", "nexeres.js");
_log("resource -> %s", resourcePath);
fs.writeFile(resourcePath, resources, next);
},
/**
* monkeypatch some files so that the nexe.js file is loaded when the app runs
*/
@@ -260,7 +273,7 @@ function _monkeyPatchNodeConfig (compiler, complete) {
async.waterfall([
/**
* monkeypatch the gyp file to include the nexe.js file
* monkeypatch the gyp file to include the nexe.js and nexeres.js files
*/
function (next) {
@@ -291,7 +304,7 @@ function _monkeyPatchGyp (compiler, complete) {
return ~content.indexOf("nexe.js");
},
function (content, next) {
next(null, content.replace("'lib/fs.js',", "'lib/fs.js', 'lib/nexe.js', "))
next(null, content.replace("'lib/fs.js',", "'lib/fs.js', 'lib/nexe.js', 'lib/nexeres.js', "))
},
complete
)