Permalink
Browse files

lib: Detect and compile coffee files

Allow building and testing coffeescript files with
javascript files. Assume that coffeescript files
should be compiled if provided.
  • Loading branch information...
1 parent 2435b22 commit 05f269f274928b7f9d8dcd8ad468cea0cda6f09a Greg Guthe committed Apr 5, 2012
Showing with 31 additions and 31 deletions.
  1. +0 −2 README.md
  2. +0 −1 lib/cli.coffee
  3. +31 −28 lib/package.coffee
View
2 README.md
@@ -93,8 +93,6 @@ that you can specify when configuring your project:
Optional configs. These all have sane default values, but can be tweaked if desired:
- * `coffee` - (False) Is this project written in CoffeeScript? Any truthy value will
- flip jspackle to run in CoffeeScript mode
* `test_build_folder` - ("build") Temporary folder to put compiled coffeescript source files to run tests.
* `depends_folder` - ("requires") Folder that contains dependencies in order for the
tests to run
View
1 lib/cli.coffee
@@ -23,7 +23,6 @@ module.exports = ->
.option('-q, --quiet', 'Only print critical errors to the screen')
.option('-n, --no-color', 'Disable colors in the output')
.option('-m, --minify', 'Minify the build (uses uglify-js)')
- .option('-c, --coffee', 'Look for and compile coffee-script files')
.option('-r, --root <root>', 'The of the project', process.cwd()+'/')
.option('-p, --path <path>', 'Path of the config file, relative to root', 'jspackle.json')
.option('-s, --test_server <test_server>', 'Test server', 'http://localhost:9876')
View
59 lib/package.coffee
@@ -5,8 +5,10 @@ pathLib = require 'path'
String::isHTTP = ->
@substring(0, 7) == 'http://' or @substring(0, 8) == 'https://'
-String::isScript = ->
- this.substring(this.length-2) is 'js' or this.substring(this.length-6) is 'coffee'
+String::isCoffee = -> pathLib.extname(pathLib.basename(@)) == '.coffee'
+String::isJs = -> pathLib.extname(pathLib.basename(@)) == '.js'
+
+String::isScript = -> @isCoffee or @isJs
###
Load any methods or libraries that interact with the outside
@@ -314,27 +316,22 @@ Output:
logging.debug "Dumping configs to: #{path}"
yaml.dump configs, path, callback
- _coffeeCompile: (sources, buildFolder)->
- paths = []
- for src in sources
- if src.isHTTP()
- paths.push src
- else
- try
- compiled = coffee.compile fs.readFileSync(src).toString()
- catch e
- logging.critical "Cannot parse #{src} as valid CoffeeScript!"
- logging.critical e
- throw e
-
- fileName = pathLib.join buildFolder, src.replace('.coffee', '.js')
- filePath = pathLib.join buildFolder, pathLib.dirname src
-
- fs.mkdirSync filePath, 0777, true
- logging.info "Compiling #{src} to '#{fileName}'"
- fs.writeFileSync fileName, compiled
- paths.push fileName
- return paths
+ _coffeeCompile: (src, buildFolder)->
+ # Compiles a coffeescript file and writes it to buildFolder
+ try
+ compiled = coffee.compile fs.readFileSync(src).toString()
+ catch e
+ logging.critical "Cannot parse #{src} as valid CoffeeScript!"
+ logging.critical e
+ throw e
+
+ fileName = pathLib.join buildFolder, src.replace('.coffee', '.js')
+ filePath = pathLib.join buildFolder, pathLib.dirname src
+
+ fs.mkdirSync filePath, 0777, true
+ logging.info "Compiling #{src} to '#{fileName}'"
+ fs.writeFileSync fileName, compiled
+ return fileName
_findTests: ->
found = readDir @opts.root+@opts.spec_folder
@@ -355,7 +352,7 @@ Output:
filePath = filePath.replace re, @opts[variable]
return filePath
- _process: (option, folder, buildFolder='')->
+ _process: (option, folder, buildFolder=@opts.test_build_folder)->
root = folder+'/'
sources = []
if typeof option == 'string'
@@ -365,11 +362,17 @@ Output:
for source in srcPaths
if source.isHTTP()
- sources.push source
+ if source.isJs()
+ sources.push source
+ else
+ logging.warn "Cannot include coffee script HTTP resources!"
+ else if source.isJs()
+ sources.push root+source
+ else if source.isCoffee()
+ sources.push @_coffeeCompile root+source, buildFolder
else
- sources.push root+source
- return sources if not (buildFolder and @opts.coffee)
- @_coffeeCompile sources, buildFolder
+ logging.warn "Ignoring unkonown file type #{source}"
+ return sources
###
Read only properties

0 comments on commit 05f269f

Please sign in to comment.