From 1b2fe69a1830b45ed43b4661f6a2e624e5accdc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20H=C3=A4fner?= Date: Wed, 20 Mar 2013 17:11:42 +0100 Subject: [PATCH] Fix Windows path separator issue in `baseFileName`. Windows accepts forward and backward slashes as path separator. --- lib/coffee-script/command.js | 14 ++++++++------ lib/coffee-script/helpers.js | 9 +++++---- src/command.coffee | 11 ++++++----- src/helpers.coffee | 3 ++- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/coffee-script/command.js b/lib/coffee-script/command.js index efe370b6..f2d74177 100644 --- a/lib/coffee-script/command.js +++ b/lib/coffee-script/command.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.6.2 (function() { - var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, exists, forkNode, fs, helpers, hidden, joinTimeout, lint, notSources, optionParser, optparse, opts, outputPath, parseOptions, path, printLine, printTokens, printWarn, removeSource, sourceCode, sources, spawn, timeLog, unwatchDir, usage, version, wait, watch, watchDir, watchers, writeJs, _ref; + var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, exists, forkNode, fs, helpers, hidden, joinTimeout, lint, notSources, optionParser, optparse, opts, outputPath, parseOptions, path, printLine, printTokens, printWarn, removeSource, sourceCode, sources, spawn, timeLog, unwatchDir, usage, useWinPathSep, version, wait, watch, watchDir, watchers, writeJs, _ref; fs = require('fs'); @@ -18,6 +18,8 @@ exists = fs.exists || path.exists; + useWinPathSep = path.sep === '\\'; + helpers.extend(CoffeeScript, new EventEmitter); printLine = function(line) { @@ -387,7 +389,7 @@ if (extension == null) { extension = ".js"; } - basename = helpers.baseFileName(source, true, path.sep); + basename = helpers.baseFileName(source, true, useWinPathSep); srcDir = path.dirname(source); baseDir = base === '.' ? srcDir : srcDir.substring(base.length); dir = opts.output ? path.join(opts.output, baseDir) : srcDir; @@ -407,7 +409,7 @@ js = ' '; } if (generatedSourceMap) { - js = "" + js + "\n/*\n//@ sourceMappingURL=" + (helpers.baseFileName(sourceMapPath, false, path.sep)) + "\n*/\n"; + js = "" + js + "\n/*\n//@ sourceMappingURL=" + (helpers.baseFileName(sourceMapPath, false, useWinPathSep)) + "\n*/\n"; } fs.writeFile(jsPath, js, function(err) { if (err) { @@ -503,13 +505,13 @@ jsPath: jsPath, sourceRoot: path.relative(jsDir, cwd), sourceFiles: [path.relative(cwd, filename)], - generatedFile: helpers.baseFileName(jsPath, false, path.sep) + generatedFile: helpers.baseFileName(jsPath, false, useWinPathSep) }); } else { answer = helpers.merge(answer, { sourceRoot: "", - sourceFiles: [helpers.baseFileName(filename, false, path.sep)], - generatedFile: helpers.baseFileName(filename, true, path.sep) + ".js" + sourceFiles: [helpers.baseFileName(filename, false, useWinPathSep)], + generatedFile: helpers.baseFileName(filename, true, useWinPathSep) + ".js" }); } } diff --git a/lib/coffee-script/helpers.js b/lib/coffee-script/helpers.js index bbdca39f..b4882410 100644 --- a/lib/coffee-script/helpers.js +++ b/lib/coffee-script/helpers.js @@ -156,14 +156,15 @@ } }; - exports.baseFileName = function(file, stripExt, pathSep) { - var parts; + exports.baseFileName = function(file, stripExt, useWinPathSep) { + var parts, pathSep; if (stripExt == null) { stripExt = false; } - if (pathSep == null) { - pathSep = '/'; + if (useWinPathSep == null) { + useWinPathSep = false; } + pathSep = useWinPathSep ? /\\|\// : /\//; parts = file.split(pathSep); file = parts[parts.length - 1]; if (!stripExt) { diff --git a/src/command.coffee b/src/command.coffee index 3af19312..d0a0f5bc 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -14,6 +14,7 @@ CoffeeScript = require './coffee-script' {EventEmitter} = require 'events' exists = fs.exists or path.exists +useWinPathSep = path.sep is '\\' # Allow CoffeeScript to emit Node.js events. helpers.extend CoffeeScript, new EventEmitter @@ -256,7 +257,7 @@ removeSource = (source, base, removeJs) -> # Get the corresponding output JavaScript path for a source file. outputPath = (source, base, extension=".js") -> - basename = helpers.baseFileName source, yes, path.sep + basename = helpers.baseFileName source, yes, useWinPathSep 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 @@ -274,7 +275,7 @@ writeJs = (base, sourcePath, js, jsPath, generatedSourceMap = null) -> compile = -> if opts.compile js = ' ' if js.length <= 0 - if generatedSourceMap then js = "#{js}\n/*\n//@ sourceMappingURL=#{helpers.baseFileName sourceMapPath, no, path.sep}\n*/\n" + if generatedSourceMap then js = "#{js}\n/*\n//@ sourceMappingURL=#{helpers.baseFileName sourceMapPath, no, useWinPathSep}\n*/\n" fs.writeFile jsPath, js, (err) -> if err printLine err.message @@ -343,13 +344,13 @@ compileOptions = (filename, base) -> jsPath sourceRoot: path.relative jsDir, cwd sourceFiles: [path.relative cwd, filename] - generatedFile: helpers.baseFileName(jsPath, no, path.sep) + generatedFile: helpers.baseFileName(jsPath, no, useWinPathSep) } else answer = helpers.merge answer, sourceRoot: "" - sourceFiles: [helpers.baseFileName filename, no, path.sep] - generatedFile: helpers.baseFileName(filename, yes, path.sep) + ".js" + sourceFiles: [helpers.baseFileName filename, no, useWinPathSep] + generatedFile: helpers.baseFileName(filename, yes, useWinPathSep) + ".js" answer # Start up a new Node.js instance with the arguments in `--nodejs` passed to diff --git a/src/helpers.coffee b/src/helpers.coffee index bc8aed25..a534b924 100644 --- a/src/helpers.coffee +++ b/src/helpers.coffee @@ -118,7 +118,8 @@ exports.locationDataToString = (obj) -> "No location data" # A `.coffee.md` compatible version of `basename`, that returns the file sans-extension. -exports.baseFileName = (file, stripExt = no, pathSep = '/') -> +exports.baseFileName = (file, stripExt = no, useWinPathSep = no) -> + pathSep = if useWinPathSep then /\\|\// else /\// parts = file.split(pathSep) file = parts[parts.length - 1] return file unless stripExt