Add support for embedded files.
This commit is contained in:
@@ -3,10 +3,11 @@ var nexe = require('nexe');
|
|||||||
nexe.compile(
|
nexe.compile(
|
||||||
{
|
{
|
||||||
input: "./index.js",
|
input: "./index.js",
|
||||||
output: "./hellowWord.exe",
|
output: "./helloWorld.exe",
|
||||||
nodeVersion: "0.10.33",
|
nodeVersion: "0.10.33",
|
||||||
nodeTempDir: "",
|
nodeTempDir: "",
|
||||||
flags: true
|
flags: true,
|
||||||
|
resourceFiles: ["./message.txt"]
|
||||||
},
|
},
|
||||||
function (err) {
|
function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|||||||
@@ -1,18 +1,23 @@
|
|||||||
var http = require("http"),
|
var http = require("http"),
|
||||||
fs = require("fs"),
|
fs = require("fs"),
|
||||||
path = require("path"),
|
path = require("path"),
|
||||||
|
nexeres = require("nexeres"),
|
||||||
|
childProc = require("child_process"),
|
||||||
port = 1337;
|
port = 1337;
|
||||||
|
|
||||||
//Fork a new process to execute the broser.js
|
//Fork a new process to execute the broser.js
|
||||||
var childProc = require("child_process");
|
childProc.fork(path.join(__dirname, "browser.js"));
|
||||||
childProc.fork("./browser.js");
|
|
||||||
if (false) {
|
if (false) {
|
||||||
require("./browser.js"); //force the bundler to include the .js file
|
require("./browser.js"); //force the bundler to include the .js file
|
||||||
}
|
}
|
||||||
|
|
||||||
//start a web server on specified port (or default if non given)
|
//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);
|
console.log("started HTTP server on port %d", port = Number(process.argv.concat().pop()) || port);
|
||||||
|
|
||||||
http.createServer(function(req, res) {
|
http.createServer(function(req, res) {
|
||||||
fs.createReadStream(path.join(process.cwd(), "message.txt")).pipe(res);
|
//return the embeded file in response to any request
|
||||||
|
|
||||||
|
res.write(nexeres.get("message.txt"));
|
||||||
|
|
||||||
|
res.end();
|
||||||
}).listen(port);
|
}).listen(port);
|
||||||
+3
-4
@@ -1,9 +1,7 @@
|
|||||||
var mdeps = require("module-deps"),
|
var mdeps = require("module-deps"),
|
||||||
through = require("through"),
|
through = require("through"),
|
||||||
async = require("async"),
|
async = require("async"),
|
||||||
path = require("path"),
|
//uglify = require("uglify-js"),
|
||||||
fs = require("fs"),
|
|
||||||
uglify = require("uglify-js"),
|
|
||||||
builtins = require("builtins");
|
builtins = require("builtins");
|
||||||
|
|
||||||
|
|
||||||
@@ -62,6 +60,7 @@ function bundle (input, complete) {
|
|||||||
|
|
||||||
},
|
},
|
||||||
filter: function (id) {
|
filter: function (id) {
|
||||||
|
if (id == 'nexeres') { return false; } //treat our new moudle as a builtin module
|
||||||
return !~builtins.indexOf(id);
|
return !~builtins.indexOf(id);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -107,7 +106,7 @@ var loader = function (deps, key) {
|
|||||||
|
|
||||||
var argv3 = process.argv[3];
|
var argv3 = process.argv[3];
|
||||||
if (argv3) {
|
if (argv3) {
|
||||||
argv3 = pathModule.relative(process.execPath, argv3);
|
argv3 = pathModule.relative(pathModule.dirname(process.execPath), argv3);
|
||||||
argv3 = pathModule.resolve(pathModule.dirname(key), argv3);
|
argv3 = pathModule.resolve(pathModule.dirname(key), argv3);
|
||||||
}
|
}
|
||||||
if (process.argv[2] == "--child_process" && deps[argv3]) {
|
if (process.argv[2] == "--child_process" && deps[argv3]) {
|
||||||
|
|||||||
@@ -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
@@ -9,6 +9,7 @@ ProgressBar = require("progress"),
|
|||||||
child_process = require("child_process"),
|
child_process = require("child_process"),
|
||||||
glob = require("glob"),
|
glob = require("glob"),
|
||||||
bundle = require("./bundle"),
|
bundle = require("./bundle"),
|
||||||
|
embed = require("./embed"),
|
||||||
os = require("os"),
|
os = require("os"),
|
||||||
targz = require('tar.gz'),
|
targz = require('tar.gz'),
|
||||||
spawn = child_process.spawn;
|
spawn = child_process.spawn;
|
||||||
@@ -60,6 +61,18 @@ exports.compile = function (options, complete) {
|
|||||||
fs.writeFile(bundlePath, source, next);
|
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
|
* 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([
|
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) {
|
function (next) {
|
||||||
@@ -291,7 +304,7 @@ function _monkeyPatchGyp (compiler, complete) {
|
|||||||
return ~content.indexOf("nexe.js");
|
return ~content.indexOf("nexe.js");
|
||||||
},
|
},
|
||||||
function (content, next) {
|
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
|
complete
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user