This commit is contained in:
Lorenz Gardner
2015-02-09 16:06:29 -06:00
5 changed files with 87 additions and 8 deletions
+17
View File
@@ -0,0 +1,17 @@
var nexe = require('nexe');
nexe.compile(
{
input: "./index.js",
output: "./helloWorld.exe",
nodeVersion: "0.10.33",
nodeTempDir: "",
flags: true,
resourceFiles: ["./message.txt"]
},
function (err) {
if (err) {
console.log(err);
}
}
);
+15 -2
View File
@@ -1,10 +1,23 @@
var http = require("http"),
fs = require("fs"),
path = require("path"),
nexeres = require("nexeres"),
childProc = require("child_process"),
port = 1337;
//Fork a new process to execute the broser.js
childProc.fork(path.join(__dirname, "browser.js"));
if (false) {
require("./browser.js"); //force the bundler to include the .js file
}
//start a web server on specified port (or default if not given)
console.log("started HTTP server on port %d", port = Number(process.argv.concat().pop()) || port);
http.createServer(function(req, res) {
fs.createReadStream(path.join(process.cwd(), "message.txt")).pipe(res);
}).listen(port);
//return the embeded file in response to any request
res.write(nexeres.get("message.txt"));
res.end();
}).listen(port);
+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
)