1. Brian McKenna
  2. roy

Commits

joneshf  committed eb3c3dc

Factored out flag processing.

  • Participants
  • Parent commits 8c2986c
  • Branches master

Comments (0)

Files changed (1)

File src/compile.js

View file
  • Ignore whitespace
     }
 };
 
-var main = function() {
-    var argv = process.argv.slice(2);
-
-    // Meta-commands configuration
-    var opts = {
-        colorConsole: false
-    };
-
-    // Roy package information
-    var fs = require('fs'),
-        path = require('path');
-    var info = JSON.parse(fs.readFileSync(path.dirname(__dirname) + '/package.json', 'utf8'));
-
-    if(process.argv.length < 3) {
-        console.log("Roy: " + info.description);
-        console.log(info.author);
+var processFlags = function(argv, opts) {
+    if(argv.length === 0) {
+        console.log("Roy: " + opts.info.description);
+        console.log(opts.info.author);
         console.log(":? for help");
         nodeRepl(opts);
         return;
     }
 
-    var source;
-    var vm;
-    var browserModules = false;
-    var run = false;
-    var includePrelude = true;
     switch(argv[0]) {
     case "-v":
     case "--version":
-        console.log("Roy: " + info.description);
-        console.log(info.version);
+        console.log("Roy: " + opts.info.description);
+        console.log(opts.info.version);
         process.exit();
         break;
     case "--help":
     case "-h":
-        console.log("Roy: " + info.description + "\n");
+        console.log("Roy: " + opts.info.description + "\n");
         console.log("-v        : show current version");
         console.log("-r [file] : run Roy-code without JavaScript output");
         console.log("-p        : run without prelude (standard library)");
         console.log("-h        : show this help");
         return;
     case "--stdio":
-        source = '';
+        var source = '';
         process.stdin.resume();
         process.stdin.setEncoding('utf8');
         process.stdin.on('data', function(data) {
         });
         return;
     case "-p":
-        includePrelude = false;
+        opts.includePrelude = false;
         /* falls through */
     case "-r":
-        vm = require('vm');
-        run = true;
+        opts.run = true;
         argv.shift();
         break;
     case "-b":
     case "--browser":
-        browserModules = true;
+        opts.browserModules = true;
         argv.shift();
         break;
     case "-c":
         opts.colorConsole = true;
         nodeRepl(opts);
         return;
+    default:
+        runRoy(argv, opts);
+        return;
     }
 
+    processFlags(argv, opts);
+};
+
+var runRoy = function(argv, opts) {
+    var fs = require('fs');
+    var path = require('path');
+    var vm = require('vm');
+
     var extensions = /\.l?roy$/;
     var literateExtension = /\.lroy$/;
 
     var aliases = {};
     var sandbox = getSandbox();
 
-    if(run) {
+    if(opts.run) {
         // Include the standard library
-        if(includePrelude) {
+        if(opts.includePrelude) {
             argv.unshift(path.dirname(__dirname) + '/lib/prelude.roy');
         }
     } else {
         var sourceMap = new SourceMapGenerator({file: path.basename(outputPath)});
 
         var compiled = compile(source, env, aliases, {
-            nodejs: !browserModules,
+            nodejs: !opts.browserModules,
             filename: filename,
-            run: run,
+            run: opts.run,
             exported: exported,
             sourceMap: sourceMap
         });
-        if(run) {
+        if(opts.run) {
             // Execute the JavaScript output.
             output = vm.runInNewContext(compiled.output, sandbox, 'eval');
         } else {
         }
     });
 };
+
+var main = function() {
+    var argv = process.argv.slice(2);
+
+    // Roy package information
+    var fs = require('fs');
+    var path = require('path');
+
+    // Meta-commands configuration
+    var opts = {
+        colorConsole: false,
+        info: JSON.parse(fs.readFileSync(path.dirname(__dirname) + '/package.json', 'utf8')),
+        browserModules: false,
+        run: false,
+        includePrelude: true
+    };
+
+    processFlags(argv, opts);
+};
 exports.main = main;
 
 if(exports && !module.parent) {