Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify extension handling to allow for .coffee.md #2738

Merged
merged 1 commit into from Feb 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cakefile
@@ -1,8 +1,8 @@
fs = require 'fs'
path = require 'path'
{extend} = require './lib/coffee-script/helpers'
CoffeeScript = require './lib/coffee-script'
{spawn, exec} = require 'child_process'
helpers = require './lib/coffee-script/helpers'

# ANSI Terminal Colors.
bold = red = green = reset = ''
Expand Down Expand Up @@ -77,7 +77,7 @@ task 'build:full', 'rebuild the source twice, and run the tests', ->


task 'build:parser', 'rebuild the Jison parser (run build first)', ->
extend global, require('util')
helpers.extend global, require('util')
require 'jison'
parser = require('./lib/coffee-script/grammar').parser
fs.writeFile 'lib/coffee-script/parser.js', parser.generate()
Expand Down Expand Up @@ -223,7 +223,7 @@ runTests = (CoffeeScript) ->
# Run every test in the `test` folder, recording failures.
files = fs.readdirSync 'test'
for file in files when file.match /\.(lit)?coffee$/i
literate = path.extname(file) is '.litcoffee'
literate = helpers.isLiterate file
currentFile = filename = path.join 'test', file
code = fs.readFileSync filename
try
Expand Down
35 changes: 18 additions & 17 deletions lib/coffee-script/coffee-script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions lib/coffee-script/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion lib/coffee-script/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions src/coffee-script.coffee
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions src/command.coffee
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -103,7 +102,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'
Expand Down Expand Up @@ -248,7 +247,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
Expand Down Expand Up @@ -311,8 +310,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.
Expand Down
12 changes: 12 additions & 0 deletions src/helpers.coffee
Expand Up @@ -2,6 +2,8 @@
# the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten
# arrays, count characters, that sort of thing.

path = require 'path'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this break browser support?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point. I'll move the offenders over into command.


# 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
Expand Down Expand Up @@ -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)