Permalink
Browse files

fixed compile so exceptions are handled

  • Loading branch information...
1 parent 590f2cd commit becb92ac3211d81f6294fe570e08a801c006877c @khoomeister committed Jun 5, 2011
Showing with 52 additions and 51 deletions.
  1. +24 −23 lib/coffee-css.js
  2. +1 −1 package.json
  3. +18 −16 src/coffee-css.coffee
  4. +9 −11 test/test.coffee
View
@@ -19,29 +19,30 @@
if (newFileName === void 0) {
newFileName = fileName.replace(/\.css\.coffee$/i, '.css');
}
- try {
- return fs.readFile(fileName, function(err, data) {
- var css, js, sandbox;
- js = coffee.compile(data.toString().trim());
- sandbox = {
- exports: {
- css: {},
- mixins: getDefaultMixins()
- },
- require: require
- };
- vm.runInNewContext(js, sandbox);
- css = compileObject(sandbox.exports.css, sandbox.exports.mixins);
- return fs.writeFile(newFileName, css, function(err) {
- if (err) {
- throw err;
- }
- return callback(null, fileName, newFileName);
- });
- });
- } catch (err) {
- return callback(err, fileName, newFileName);
- }
+ return fs.readFile(fileName, function(err, data) {
+ var css, js, sandbox;
+ if (err) {
+ return callback(err, fileName, newFileName);
+ } else {
+ try {
+ js = coffee.compile(data.toString().trim());
+ sandbox = {
+ exports: {
+ css: {},
+ mixins: getDefaultMixins()
+ },
+ require: require
+ };
+ vm.runInNewContext(js, sandbox);
+ css = compileObject(sandbox.exports.css, sandbox.exports.mixins);
+ return fs.writeFile(newFileName, css, function(err) {
+ return callback(err, fileName, newFileName);
+ });
+ } catch (err) {
+ return callback(err, fileName, newFileName);
+ }
+ }
+ });
};
exports.getDefaultMixins = getDefaultMixins;
compileObject = function(cssObject, mixins) {
View
@@ -2,7 +2,7 @@
"name": "coffee-css",
"description": "More CSS for CoffeeScript",
"author": "khoomeister",
- "version": "0.0.4",
+ "version": "0.0.5",
"licenses": [{
"type": "MIT",
"url": "https://github.com/aeosynth/ccss/raw/master/LICENSE"
View
@@ -17,22 +17,24 @@ exports.compile = (fileName, newFileName, callback = (err, fileName, newFileName
if newFileName is undefined
newFileName = fileName.replace /\.css\.coffee$/i, '.css'
- try
- fs.readFile fileName, (err, data) ->
- js = coffee.compile(data.toString().trim())
- sandbox =
- exports:
- css: {}
- mixins: getDefaultMixins()
- require: require
- vm.runInNewContext js, sandbox
-
- css = compileObject sandbox.exports.css, sandbox.exports.mixins
- fs.writeFile newFileName, css, (err) ->
- if err then throw err
- callback null, fileName, newFileName
- catch err
- callback err, fileName, newFileName
+ fs.readFile fileName, (err, data) ->
+ if err
+ callback err, fileName, newFileName
+ else
+ try
+ js = coffee.compile(data.toString().trim())
+ sandbox =
+ exports:
+ css: {}
+ mixins: getDefaultMixins()
+ require: require
+ vm.runInNewContext js, sandbox
+
+ css = compileObject sandbox.exports.css, sandbox.exports.mixins
+ fs.writeFile newFileName, css, (err) ->
+ callback err, fileName, newFileName
+ catch err
+ callback err, fileName, newFileName
exports.getDefaultMixins = getDefaultMixins
View
@@ -1,18 +1,10 @@
# requires
+assert = require 'assert'
coffeeCss = require '../lib/coffee-css'
fs = require 'fs'
path = require 'path'
should = require 'should'
-# constants
-COFFEE_CSS_SAMPLE_FILE_DATA = """
- exports.css =
- 'a':
- textDecoration: 'none'
- 'a:hover':
- textDecoration: 'underline'
- """
-
# tests
exports.testBoxShadowAll = ->
css = coffeeCss.compile
@@ -80,8 +72,14 @@ exports.testOpacityAll = ->
exports.testCompileFile = ->
coffeeCssFile = __dirname + '/testFile.css.coffee'
- data = COFFEE_CSS_SAMPLE_FILE_DATA
-
+ data = """
+ exports.css =
+ 'a':
+ textDecoration: 'none'
+ 'a:hover':
+ textDecoration: 'underline'
+ """
+
fs.writeFile coffeeCssFile, data, (err) ->
coffeeCss.compile coffeeCssFile, (err, fileName, newFileName) ->
should.not.exist err

0 comments on commit becb92a

Please sign in to comment.