Merging in .coffee.md support alongside .litcoffee ... I think we'll keep .litcoffee as the canonical, however.

This commit is contained in:
Jeremy Ashkenas
2013-03-01 12:46:40 +13:00
7 changed files with 62 additions and 44 deletions

View File

@@ -10,19 +10,17 @@ fs = require 'fs'
path = require 'path'
{Lexer} = require './lexer'
{parser} = require './parser'
helpers = require './helpers'
vm = require 'vm'
# The file extensions that are considered to be CoffeeScript.
extensions = ['.coffee', '.litcoffee']
# Load and run a CoffeeScript file for Node, stripping any `BOM`s.
loadFile = (module, filename) ->
raw = fs.readFileSync filename, 'utf8'
stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
module._compile compile(stripped, {filename}), filename
module._compile compile(stripped, {filename, literate: helpers.isLiterate filename}), filename
if require.extensions
for ext in extensions
for ext in ['.coffee', '.litcoffee', '.md', '.coffee.md']
require.extensions[ext] = loadFile
# The current CoffeeScript version number.
@@ -73,7 +71,7 @@ exports.run = (code, options = {}) ->
mainModule.paths = require('module')._nodeModulePaths path.dirname fs.realpathSync options.filename
# Compile.
if (path.extname(mainModule.filename) not in extensions) or require.extensions
if not helpers.isCoffee(mainModule.filename) or require.extensions
mainModule._compile compile(code, options), mainModule.filename
else
mainModule._compile code, mainModule.filename

View File

@@ -56,7 +56,6 @@ sourceCode = []
notSources = {}
watchers = {}
optionParser = null
coffee_exts = ['.coffee', '.litcoffee']
# Run `coffee` by parsing passed options and determining what action to take.
# Many flags cause us to divert before compiling anything. Flags passed after
@@ -79,8 +78,8 @@ exports.run = ->
compilePath source, yes, path.normalize source
# Compile a path, which could be a script or a directory. If a directory
# is passed, recursively compile all '.coffee' and '.litcoffee' extension source
# files in it and all subdirectories.
# is passed, recursively compile all '.coffee', '.litcoffee', and '.coffee.md'
# extension source files in it and all subdirectories.
compilePath = (source, topLevel, base) ->
fs.stat source, (err, stats) ->
throw err if err and err.code isnt 'ENOENT'
@@ -98,7 +97,7 @@ compilePath = (source, topLevel, base) ->
sourceCode[index..index] = files.map -> null
files.forEach (file) ->
compilePath (path.join source, file), no, base
else if topLevel or path.extname(source) in coffee_exts
else if topLevel or helpers.isCoffee source
watch source, base if opts.watch
fs.readFile source, (err, code) ->
throw err if err and err.code isnt 'ENOENT'
@@ -243,7 +242,7 @@ removeSource = (source, base, removeJs) ->
# Get the corresponding output JavaScript path for a source file.
outputPath = (source, base) ->
filename = path.basename(source, path.extname(source)) + '.js'
filename = helpers.getBasename(source) + '.js'
srcDir = path.dirname source
baseDir = if base is '.' then srcDir else srcDir.substring base.length
dir = if opts.output then path.join opts.output, baseDir else srcDir
@@ -306,8 +305,7 @@ parseOptions = ->
# The compile-time options to pass to the CoffeeScript compiler.
compileOptions = (filename) ->
literate = path.extname(filename) is '.litcoffee'
{filename, literate, bare: opts.bare, header: opts.compile}
{filename, literate: helpers.isLiterate(filename), bare: opts.bare, header: opts.compile}
# Start up a new Node.js instance with the arguments in `--nodejs` passed to
# the `node` binary, preserving the other options.

View File

@@ -2,6 +2,8 @@
# the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten
# arrays, count characters, that sort of thing.
path = require 'path'
# Peek at the beginning of a given string to see if it matches a sequence.
exports.starts = (string, literal, start) ->
literal is string.substr start, literal.length
@@ -92,4 +94,14 @@ exports.locationDataToString = (obj) ->
else
"No location data"
# Determine if a filename represents a CoffeeScript file.
exports.isCoffee = (file) -> /\.((lit)?coffee|coffee\.md)$/.test file
# Determine if a filename represents a Literate CoffeeScript file.
exports.isLiterate = (file) -> /\.(litcoffee|coffee\.md)$/.test file
# Extract the basename from a source file name, accounting for all possible
# CoffeeScript extensions.
exports.getBasename = (file) -> path.basename file, file?.match?(/\.((lit)?coffee|coffee\.md)$/)?[0] or path.extname(file)