Permalink
Browse files

Command line traceur will now invoke the interpreter if no out param …

  • Loading branch information...
1 parent bc4dc90 commit fa7c011fd9f2bead80ff96a53f4311c65f564b9c @arv arv committed Mar 18, 2013
Showing with 127 additions and 103 deletions.
  1. +94 −0 src/node/command.js
  2. +30 −90 src/node/compiler.js
  3. +2 −12 src/node/interpreter.js
  4. +1 −1 traceur
View
@@ -0,0 +1,94 @@
+// Copyright 2013 Traceur Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+'use strict';
+
+var path = require('path');
+var flags;
+var cmdName = path.basename(process.argv[1]);
+try {
+ flags = new (require('commander').Command)(cmdName);
+} catch (ex) {
+ console.error('Commander.js is required for this to work. To install it ' +
+ 'run:\n\n npm install commander\n');
+ process.exit(1);
+}
+flags.setMaxListeners(100);
+
+require('./traceur.js');
+
+flags.option('--out <FILE>', 'Compile all input files into a single file');
+
+flags.option('--sourcemap', 'Generate source maps');
+flags.on('sourcemap', function() {
+ flags.sourceMaps = traceur.options.sourceMaps = true;
+});
+
+flags.option('--longhelp', 'Show all known options');
+flags.on('longhelp', function() {
+ flags.help();
+ process.exit();
+});
+
+flags.on('--help', function() {
+ console.log(' Examples:');
+ console.log('');
+ console.log(' $ %s a.js', cmdName);
+ console.log(' $ %s b.js c.js --out compiled.js', cmdName);
+ console.log('');
+});
+
+traceur.options.addOptions(flags);
+
+flags.usage('[options] [files]');
+
+// Override commander.js's optionHelp to filter out the Traceur feature flags
+// from showing up in the help message.
+var optionHelp = flags.optionHelp;
+flags.optionHelp = function() {
+ if (!flags.longhelp) {
+ this.options = this.options.filter(function(command) {
+ var dashedName = command.long.slice(2);
+ return traceur.options.filterOption(dashedName);
+ });
+ }
+ return optionHelp.call(this);
+}
+
+flags.parse(process.argv);
+
+var includes = flags.args;
+
+if (!includes.length) {
+ // TODO: Start trepl
+ console.error('\n Error: At least one input file is needed');
+ flags.help();
+ process.exit(1);
+}
+
+var interpret = require('./interpreter.js');
+var compiler = require('./compiler.js');
+var compileToSingleFile = compiler.compileToSingleFile;
+var compileToDirectory = compiler.compileToDirectory;
+
+var out = flags.out;
+if (out) {
+ var isSingleFileCompile = /\.js$/.test(out);
+ if (isSingleFileCompile)
+ compileToSingleFile(out, includes, flags.sourceMaps);
+ else
+ compileToDirectory(out, includes, flags.sourceMaps);
+} else {
+ interpret(includes[0]);
+}
View
@@ -1,4 +1,4 @@
-// Copyright 2012 Traceur Authors.
+// Copyright 2013 Traceur Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -16,77 +16,25 @@
var fs = require('fs');
var path = require('path');
-var flags;
-var cmdName = path.basename(process.argv[1]);
-try {
- flags = new (require('commander').Command)(cmdName);
-} catch (ex) {
- console.error('Commander.js is required for this to work. To install it ' +
- 'run:\n\n npm install commander\n');
- process.exit(1);
-}
-flags.setMaxListeners(100);
require('./traceur.js');
-flags.option('--out <FILE>', 'Compile all input files into a single file',
- 'compiled.js');
-
-flags.option('--sourcemap', 'Generate source maps');
-flags.on('sourcemap', function() {
- flags.sourceMaps = traceur.options.sourceMaps = true;
-});
-
-flags.option('--longhelp', 'Show all known options');
-flags.on('longhelp', function() {
- flags.help();
- process.exit();
-});
-
-flags.on('--help', function() {
- console.log(' Examples:');
- console.log('');
- console.log(' $ %s a.js', cmdName);
- console.log(' $ %s b.js c.js --out compiled.js', cmdName);
- console.log('');
-});
-
-traceur.options.addOptions(flags);
-
-flags.usage('[options] [files]');
-
-// Override commander.js's optionHelp to filter out the Traceur feature flags
-// from showing up in the help message.
-var optionHelp = flags.optionHelp;
-flags.optionHelp = function() {
- if (!flags.longhelp) {
- this.options = this.options.filter(function(command) {
- var dashedName = command.long.slice(2);
- return traceur.options.filterOption(dashedName);
- });
- }
- return optionHelp.call(this);
-}
-
-flags.parse(process.argv);
-
-var outputFile = flags.out;
-var includes = flags.args;
+var inlineAndCompile = require('./inline-module.js').inlineAndCompile;
+var util = require('./file-util.js');
+var writeFile = util.writeFile;
+var mkdirRecursive = util.mkdirRecursive;
-if (!includes.length) {
- // TODO: Start trepl
- console.error('\n Error: At least one input file is needed');
- flags.help();
- process.exit(1);
-}
+var ErrorReporter = traceur.util.ErrorReporter;
+var TreeWriter = traceur.outputgeneration.TreeWriter;
+var SourceMapGenerator = traceur.outputgeneration.SourceMapGenerator;
function getSourceMapFileName(name) {
return name.replace(/\.js$/, '.map');
}
-function writeTreeToFile(tree, filename, opt_sourceRoot) {
+function writeTreeToFile(tree, filename, useSourceMaps, opt_sourceRoot) {
var options = null;
- if (flags.sourceMaps) {
+ if (useSourceMaps) {
var sourceMapFilePath = getSourceMapFileName(filename);
var config = {
file: path.basename(filename),
@@ -97,34 +45,19 @@ function writeTreeToFile(tree, filename, opt_sourceRoot) {
}
var compiledCode = TreeWriter.write(tree, options);
- if (flags.sourceMaps) {
+ if (useSourceMaps) {
compiledCode += '\n//@ sourceMappingURL=' +
path.basename(sourceMapFilePath);
}
writeFile(filename, compiledCode);
- if (flags.sourceMaps)
+ if (useSourceMaps)
writeFile(sourceMapFilePath, options.sourceMap);
}
-var ErrorReporter = traceur.util.ErrorReporter;
-var TreeWriter = traceur.outputgeneration.TreeWriter;
-var SourceMapGenerator = traceur.outputgeneration.SourceMapGenerator;
-
-var reporter = new ErrorReporter();
-
-var util = require('./file-util.js');
-var inlineAndCompile = require('./inline-module.js').inlineAndCompile;
-
-var writeFile = util.writeFile;
-var mkdirRecursive = util.mkdirRecursive;
-
-var isSingleFileCompile = /\.js$/.test(outputFile);
-
-var resolvedOutputFile = path.resolve(outputFile);
-var outputDir = isSingleFileCompile ?
- path.dirname(resolvedOutputFile) : resolvedOutputFile;
-
-if (isSingleFileCompile) {
+function compileToSingleFile(outputFile, includes, useSourceMaps) {
+ var reporter = new ErrorReporter();
+ var resolvedOutputFile = path.resolve(outputFile);
+ var outputDir = path.dirname(resolvedOutputFile);
// Resolve includes before changing directory.
var resolvedIncludes = includes.map(function(include) {
@@ -139,32 +72,39 @@ if (isSingleFileCompile) {
return path.relative(outputDir, include);
});
- inlineAndCompile(resolvedIncludes, flags, reporter, function(tree) {
- writeTreeToFile(tree, resolvedOutputFile);
+ inlineAndCompile(resolvedIncludes, {}, reporter, function(tree) {
+ writeTreeToFile(tree, resolvedOutputFile, useSourceMaps);
process.exit(0);
}, function(err) {
process.exit(1);
});
+}
+
+function compileToDirectory(outputFile, includes, useSourceMaps) {
+ var reporter = new ErrorReporter();
+ var outputDir = path.resolve(outputFile);
-} else {
var current = 0;
- var next = function() {
+ function next() {
if (current === includes.length)
process.exit(0);
- inlineAndCompile(includes.slice(current, current + 1), flags, reporter,
+ inlineAndCompile(includes.slice(current, current + 1), {}, reporter,
function(tree) {
var outputFile = path.join(outputDir, includes[current]);
var sourceRoot = path.relative(path.dirname(outputFile));
- writeTreeToFile(tree, outputFile, sourceRoot);
+ writeTreeToFile(tree, outputFile, useSourceMaps, sourceRoot);
current++;
next();
},
function(err) {
process.exit(1);
});
- };
+ }
next();
}
+
+exports.compileToSingleFile = compileToSingleFile;
+exports.compileToDirectory = compileToDirectory;
@@ -1,5 +1,3 @@
-#!/usr/bin/env node
-
// Copyright 2013 Traceur Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,17 +21,15 @@ var inlineAndCompile = require('./inline-module.js').inlineAndCompile;
var ErrorReporter = traceur.util.ErrorReporter;
var TreeWriter = traceur.outputgeneration.TreeWriter;
-function interpret(filename, options) {
- options = options || {};
-
+function interpret(filename) {
var reporter = new ErrorReporter();
var argv = process.argv.slice(1);
argv[0] = 'traceur';
process.argv = argv;
module.filename = filename;
- inlineAndCompile([filename], options, reporter, function(tree) {
+ inlineAndCompile([filename], {}, reporter, function(tree) {
var compiledCode = TreeWriter.write(tree);
require.main._compile(compiledCode, filename);
process.exit(0);
@@ -43,9 +39,3 @@ function interpret(filename, options) {
}
module.exports = interpret;
-
-if (!module.parent) {
- var filename = process.argv[2];
- // Options should really be --traceur-options="...."
- interpret(filename);
-}
View
@@ -1,3 +1,3 @@
#!/usr/bin/env node
-require('./src/node/compiler.js');
+require('./src/node/command.js');

0 comments on commit fa7c011

Please sign in to comment.