Expanding __filename and __dirname when running with the coffee command -- Issue #771

This commit is contained in:
Jeremy Ashkenas
2010-10-24 14:19:47 -04:00
parent 6347849cd0
commit 08527075b7
2 changed files with 6 additions and 6 deletions

View File

@@ -1,10 +1,10 @@
(function() {
var Lexer, compile, fs, lexer, parser, path;
fs = require('fs');
path = require('path');
Lexer = require('./lexer').Lexer;
parser = require('./parser').parser;
if (require.extensions) {
fs = require('fs');
require.extensions['.coffee'] = function(module, filename) {
var content;
content = compile(fs.readFileSync(filename, 'utf8'));
@@ -40,7 +40,7 @@
while (root.parent) {
root = root.parent;
}
root.filename = options.fileName;
root.filename = fs.realpathSync(options.fileName);
if (root.moduleCache) {
root.moduleCache = {};
}
@@ -48,7 +48,7 @@
};
exports.eval = function(code, options) {
var __dirname, __filename;
__filename = options.fileName;
__filename = fs.realpathSync(options.fileName);
__dirname = path.dirname(__filename);
return eval(exports.compile(code, options));
};

View File

@@ -6,13 +6,13 @@
# If included on a webpage, it will automatically sniff out, compile, and
# execute all scripts present in `text/coffeescript` tags.
fs = require 'fs'
path = require 'path'
{Lexer} = require './lexer'
{parser} = require './parser'
# TODO: Remove registerExtension when fully deprecated
if require.extensions
fs = require 'fs'
require.extensions['.coffee'] = (module, filename) ->
content = compile fs.readFileSync filename, 'utf8'
module._compile content, filename
@@ -53,7 +53,7 @@ exports.run = (code, options) ->
while root.parent
root = root.parent
# Set the filename
root.filename = options.fileName
root.filename = fs.realpathSync options.fileName
# Clear the module cache
root.moduleCache = {} if root.moduleCache
# Compile
@@ -65,7 +65,7 @@ exports.run = (code, options) ->
# Compile and evaluate a string of CoffeeScript (in a Node.js-like environment).
# The CoffeeScript REPL uses this to run the input.
exports.eval = (code, options) ->
__filename = options.fileName
__filename = fs.realpathSync options.fileName
__dirname = path.dirname __filename
eval exports.compile(code, options)