Skip to content
This repository was archived by the owner on Jul 8, 2023. It is now read-only.

Commit 53d0f82

Browse files
committed
Working common CS handler.
1 parent cb162a9 commit 53d0f82

File tree

6 files changed

+97
-12
lines changed

6 files changed

+97
-12
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
coffee = require 'coffee-script'
2+
fs = require 'fs'
3+
path = require 'path'
4+
Promise = require 'bluebird'
5+
util = require 'util'
6+
7+
module.exports = class CommonCoffeeHandler
8+
9+
constructor: (@template) ->
10+
@template ?= '''
11+
function () {
12+
var module = {};
13+
14+
%s
15+
return module.exports.apply(this, arguments);
16+
}
17+
'''
18+
19+
handle: (filePath) -> new Promise (resolve, reject) =>
20+
if path.extname(filePath) isnt '.coffee'
21+
resolve null
22+
23+
return
24+
25+
fs.readFile filePath, (error, data) =>
26+
if error
27+
reject error
28+
29+
return
30+
31+
resolve [
32+
path.basename filePath, '.coffee'
33+
util.format @template, coffee.compile data.toString()
34+
]
35+
36+
return

src/handler/CommonJsHandler.coffee

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ module.exports = class CommonJsHandler
88
constructor: (@template) ->
99
@template ?= '''
1010
function () {
11-
var module = {};
12-
13-
(function () {
11+
var module = {};
12+
(function () {
1413
1514
%s
16-
})();
17-
18-
return module.exports.apply(this, arguments);
15+
}).call(this);
16+
return module.exports.apply(this, arguments);
1917
}
2018
'''
2119

test/fixture/valid/coffee.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test = 'It works.'
2+
module.exports = -> [test, arguments]

test/suite/CouchBuilder.spec.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe 'CouchBuilder', ->
3232
'directory-b':
3333
'file-b-a': "b-a\n"
3434
'file-b-b': "b-b\n"
35+
'coffee.coffee': "test = 'It works.'\nmodule.exports = -> [test, arguments]\n"
3536
'file': ''
3637
'json.json': '{"a":1,"b":2}\n'
3738
'js.js': "var test = 'It works.';\nmodule.exports = function () { return [test, arguments] };\n"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CommonCoffeeHandler = require '../../../src/handler/CommonCoffeeHandler'
2+
3+
describe 'CommonCoffeeHandler', ->
4+
5+
beforeEach ->
6+
@subject = new CommonCoffeeHandler()
7+
8+
describe 'handle', ->
9+
10+
it 'resolves to a compiled module wrapper for CoffeeScript files', ->
11+
path = "#{__dirname}/../../fixture/valid/coffee.coffee"
12+
expected = [
13+
'coffee'
14+
'''
15+
function () {
16+
var module = {};
17+
18+
(function() {
19+
var test;
20+
21+
test = 'It works.';
22+
23+
module.exports = function() {
24+
return [test, arguments];
25+
};
26+
27+
}).call(this);
28+
29+
return module.exports.apply(this, arguments);
30+
}
31+
'''
32+
]
33+
34+
return @subject.handle path
35+
.then (actual) ->
36+
assert.deepEqual actual, expected
37+
38+
it 'resolves to null for non-CoffeeScript files', ->
39+
path = "#{__dirname}/../../fixture/valid/file"
40+
41+
return @subject.handle path
42+
.then (actual) ->
43+
assert.isNull actual
44+
45+
it 'handles file system errors', ->
46+
path = "#{__dirname}/../../fixture/invalid/nonexistent.js"
47+
48+
return @subject.handle path
49+
.catch (actual) ->
50+
assert.instanceOf actual, Error

test/suite/handler/CommonJsHandler.spec.coffee

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ describe 'CommonJsHandler', ->
1313
'js'
1414
'''
1515
function () {
16-
var module = {};
17-
18-
(function () {
16+
var module = {};
17+
(function () {
1918
2019
var test = 'It works.';
2120
module.exports = function () { return [test, arguments] };
2221
23-
})();
24-
25-
return module.exports.apply(this, arguments);
22+
}).call(this);
23+
return module.exports.apply(this, arguments);
2624
}
2725
'''
2826
]

0 commit comments

Comments
 (0)