From b040337a941014f094484356f52ba05dcd312db7 Mon Sep 17 00:00:00 2001 From: RainbowDashDC Date: Sun, 15 Feb 2015 11:05:24 -0800 Subject: [PATCH] Fixes #99, resolves #97 by warning on missing file. New examples. New log functions --- examples/conditional-reqs/build.js | 17 ++++++++++++++ examples/conditional-reqs/found-file.json | 1 + examples/conditional-reqs/index.js | 7 ++++++ examples/conditional-reqs/makefile | 2 ++ examples/conditional-reqs/package.json | 6 +++++ examples/embedded-files/browser.js | 1 + examples/embedded-files/build.js | 17 ++++++++++++++ .../config.json | 0 examples/embedded-files/index.js | 23 +++++++++++++++++++ examples/embedded-files/makefile | 2 ++ examples/embedded-files/message.txt | 1 + examples/embedded-files/package.json | 4 ++++ examples/hello-world/build.js | 4 +--- examples/hello-world/index.js | 4 ++-- lib/bundle.js | 20 +++++++++++----- lib/exe.js | 4 ++-- lib/log.js | 13 +++++++++-- 17 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 examples/conditional-reqs/build.js create mode 100644 examples/conditional-reqs/found-file.json create mode 100644 examples/conditional-reqs/index.js create mode 100644 examples/conditional-reqs/makefile create mode 100644 examples/conditional-reqs/package.json create mode 100644 examples/embedded-files/browser.js create mode 100644 examples/embedded-files/build.js rename examples/{hello-world => embedded-files}/config.json (100%) create mode 100644 examples/embedded-files/index.js create mode 100644 examples/embedded-files/makefile create mode 100644 examples/embedded-files/message.txt create mode 100644 examples/embedded-files/package.json diff --git a/examples/conditional-reqs/build.js b/examples/conditional-reqs/build.js new file mode 100644 index 0000000..20ca7ca --- /dev/null +++ b/examples/conditional-reqs/build.js @@ -0,0 +1,17 @@ +var nexe = require('nexe'); + +nexe.compile( + { + input: "./index.js", + output: "./out.exe", + nodeVersion: "latest", + nodeTempDir: "", + python: "C:\\Python27\\python.exe", + flags: true + }, + function (err) { + if (err) { + console.log(err); + } + } +); diff --git a/examples/conditional-reqs/found-file.json b/examples/conditional-reqs/found-file.json new file mode 100644 index 0000000..aae7919 --- /dev/null +++ b/examples/conditional-reqs/found-file.json @@ -0,0 +1 @@ +{ "head": "Success!" } diff --git a/examples/conditional-reqs/index.js b/examples/conditional-reqs/index.js new file mode 100644 index 0000000..70eb22a --- /dev/null +++ b/examples/conditional-reqs/index.js @@ -0,0 +1,7 @@ +if(false) { + require('./not-found-file.json'); +} + +var out = require('./found-file.json'); + +console.log(out.head); diff --git a/examples/conditional-reqs/makefile b/examples/conditional-reqs/makefile new file mode 100644 index 0000000..73d3185 --- /dev/null +++ b/examples/conditional-reqs/makefile @@ -0,0 +1,2 @@ +all: + nexe -i ./ -o ./hello-world.nex diff --git a/examples/conditional-reqs/package.json b/examples/conditional-reqs/package.json new file mode 100644 index 0000000..3e8edff --- /dev/null +++ b/examples/conditional-reqs/package.json @@ -0,0 +1,6 @@ +{ + "main": "./index.js", + "dependencies": { + "nexe": "*" + } +} diff --git a/examples/embedded-files/browser.js b/examples/embedded-files/browser.js new file mode 100644 index 0000000..9872315 --- /dev/null +++ b/examples/embedded-files/browser.js @@ -0,0 +1 @@ +console.log("hello browser!"); \ No newline at end of file diff --git a/examples/embedded-files/build.js b/examples/embedded-files/build.js new file mode 100644 index 0000000..43c279f --- /dev/null +++ b/examples/embedded-files/build.js @@ -0,0 +1,17 @@ +var nexe = require('nexe'); + +nexe.compile( + { + input: "./index.js", + output: "./hellowWorld.exe", + nodeVersion: "0.10.33", + nodeTempDir: "", + flags: true, + resourceFiles: ["./message.txt"] + }, + function (err) { + if (err) { + console.log(err); + } + } +); diff --git a/examples/hello-world/config.json b/examples/embedded-files/config.json similarity index 100% rename from examples/hello-world/config.json rename to examples/embedded-files/config.json diff --git a/examples/embedded-files/index.js b/examples/embedded-files/index.js new file mode 100644 index 0000000..3263c2c --- /dev/null +++ b/examples/embedded-files/index.js @@ -0,0 +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) { + //return the embeded file in response to any request + +res.write(nexeres.get("message.txt")); + + res.end(); +}).listen(port); diff --git a/examples/embedded-files/makefile b/examples/embedded-files/makefile new file mode 100644 index 0000000..fe19143 --- /dev/null +++ b/examples/embedded-files/makefile @@ -0,0 +1,2 @@ +all: + nexe -i ./ -o ./hello-world.nex -r 0.8.18 \ No newline at end of file diff --git a/examples/embedded-files/message.txt b/examples/embedded-files/message.txt new file mode 100644 index 0000000..bc7774a --- /dev/null +++ b/examples/embedded-files/message.txt @@ -0,0 +1 @@ +hello world! \ No newline at end of file diff --git a/examples/embedded-files/package.json b/examples/embedded-files/package.json new file mode 100644 index 0000000..9eeffe6 --- /dev/null +++ b/examples/embedded-files/package.json @@ -0,0 +1,4 @@ +{ + "main": "./index.js", + "browser": "./index.js" +} \ No newline at end of file diff --git a/examples/hello-world/build.js b/examples/hello-world/build.js index be97c6b..858a6b6 100644 --- a/examples/hello-world/build.js +++ b/examples/hello-world/build.js @@ -6,9 +6,7 @@ nexe.compile( output: "", nodeVersion: "0.10.33", nodeTempDir: "", - python: "C:\\Python27\\python.exe", - flags: true, - resourceFiles: ["./message.txt"] + flags: true }, function (err) { if (err) { diff --git a/examples/hello-world/index.js b/examples/hello-world/index.js index 3263c2c..1c68a6a 100755 --- a/examples/hello-world/index.js +++ b/examples/hello-world/index.js @@ -16,8 +16,8 @@ console.log("started HTTP server on port %d", port = Number(process.argv.concat( http.createServer(function(req, res) { //return the embeded file in response to any request - -res.write(nexeres.get("message.txt")); + +res.write("Hello, world!"); res.end(); }).listen(port); diff --git a/lib/bundle.js b/lib/bundle.js index 37c05be..277347e 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -1,15 +1,16 @@ var mdeps = require("module-deps"), through = require("through"), async = require("async"), -//uglify = require("uglify-js"), builtins = require("builtins"); +var _log = require("./log"); function bundle (input, complete) { async.waterfall([ /** + * first we resolve all of the deps */ function resolveDeps (next) { @@ -39,9 +40,9 @@ function bundle (input, complete) { function end() { var content = buffer.join(""); // console.log(content); - - - /*var source = uglify.parse(content, { + /* + // TODO add uglify flag + var source = uglify.parse(content, { strict: false }); source.figure_out_scope(); @@ -63,8 +64,15 @@ function bundle (input, complete) { if (id == 'nexeres') { return false; } //treat our new moudle as a builtin module return !~builtins.indexOf(id); }, - extensions: ['.js', '.json' ] + extensions: ['.js', '.json' ], + ignoreMissing: true }); + + /** event for missing packages, fixes conditional requires **/ + md.on('missing', function(id, parent) { + _log("warn", "couldn't find require '"+id+"', errors may occur."); + }); + md.pipe(through(function (chunk) { deps.push(chunk); }, function () { @@ -82,7 +90,7 @@ function bundle (input, complete) { for (var i = deps.length; i--;) { var dep = deps[i]; - var req = "function (require, module, exports, __dirname) { \n" + var req = "function (require, module, exports, __dirname) { \n" if (dep.id.toLowerCase().indexOf(".json", dep.id.length - 5) !== -1) { //this is the result of require("someFile.json"), so we need to export it from the function req += "module.exports = " diff --git a/lib/exe.js b/lib/exe.js index 95f8087..7a14478 100644 --- a/lib/exe.js +++ b/lib/exe.js @@ -37,7 +37,7 @@ exports.compile = function (options, complete) { /* * Have we been given a custom flag for python executable? */ - if(options.python!=='python' && options.python!=="") { + if(options.python!=='python' && options.python!=="" && options.python!==undefined) { if(isWin) { isPy=options.python.replace(/\//gm, "\\"); // use windows file paths, batch is sensitive. } else { @@ -150,7 +150,7 @@ exports.compile = function (options, complete) { _log("cp %s %s", nodeCompiler.releasePath, options.output); ncp(nodeCompiler.releasePath, options.output, function (err) { if (err) { - console.log("* NOTICE * Failed to copy binary. Did the build fail?"); + _log("error", "Couldn't copy binary."); throw err; // dump raw error object } _log('copied'); diff --git a/lib/log.js b/lib/log.js index a162c30..b318031 100644 --- a/lib/log.js +++ b/lib/log.js @@ -1,10 +1,13 @@ module.exports = _log; /** + * logging aka stdout wrapper */ function _log () { + var colors = require('colors'); + var args = Array.prototype.slice.call(arguments, 0), level = args.shift(); @@ -13,7 +16,13 @@ function _log () { level = "log"; } - args[0] = "----> " + args[0]; + if(level == "log") { + args[0] = "----> " + args[0]; + } else if(level == "error") { + args[0] = "....> " + colors.red("ERROR: ") + args[0] + } else if(level == "warn") { + args[0] = "....> " + colors.yellow("WARNING: ") + args[0] + } console[level].apply(console, args); -} \ No newline at end of file +}