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

Commit 3999e81

Browse files
committed
Implemented text handler.
1 parent 53d0f82 commit 3999e81

File tree

12 files changed

+173
-95
lines changed

12 files changed

+173
-95
lines changed

src/handler/CommonCoffeeHandler.coffee

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ module.exports = class CommonCoffeeHandler
2828

2929
return
3030

31+
try
32+
js = coffee.compile data.toString()
33+
catch error
34+
reject error
35+
36+
return
37+
3138
resolve [
3239
path.basename filePath, '.coffee'
33-
util.format @template, coffee.compile data.toString()
40+
util.format @template, js
3441
]
3542

3643
return

src/handler/TextHandler.coffee

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fs = require 'fs'
2+
path = require 'path'
3+
Promise = require 'bluebird'
4+
5+
module.exports = class TextHandler
6+
7+
constructor: (@extensions = ['', '.txt']) ->
8+
9+
handle: (filePath) -> new Promise (resolve, reject) =>
10+
extension = path.extname filePath
11+
12+
unless extension in @extensions
13+
resolve null
14+
15+
return
16+
17+
fs.readFile filePath, (error, data) ->
18+
if error
19+
reject error
20+
21+
return
22+
23+
resolve [
24+
path.basename filePath, extension
25+
data.toString().replace /(?:\r\n|\r|\n)$/, ''
26+
]
27+
28+
return

test/fixture/invalid/coffee.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.

test/fixture/valid/file

Whitespace-only changes.

test/fixture/valid/no-extension

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no extension

test/fixture/valid/other.other

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
other

test/fixture/valid/txt.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
text

test/suite/CouchBuilder.spec.coffee

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ describe 'CouchBuilder', ->
3333
'file-b-a': "b-a\n"
3434
'file-b-b': "b-b\n"
3535
'coffee.coffee': "test = 'It works.'\nmodule.exports = -> [test, arguments]\n"
36-
'file': ''
37-
'json.json': '{"a":1,"b":2}\n'
3836
'js.js': "var test = 'It works.';\nmodule.exports = function () { return [test, arguments] };\n"
37+
'json.json': '{"a":1,"b":2}\n'
38+
'no-extension': 'no extension\n'
39+
'other.other': 'other\n'
40+
'txt.txt': 'text\n'
3941

4042
return @subject.build filePath
4143
.then (actual) ->

test/suite/handler/CommonCoffeeHandler.spec.coffee

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,51 @@ describe 'CommonCoffeeHandler', ->
55
beforeEach ->
66
@subject = new CommonCoffeeHandler()
77

8-
describe 'handle', ->
8+
it 'resolves to a compiled module wrapper for CoffeeScript files', ->
9+
path = "#{__dirname}/../../fixture/valid/coffee.coffee"
10+
expected = [
11+
'coffee'
12+
'''
13+
function () {
14+
var module = {};
915
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 = {};
16+
(function() {
17+
var test;
1718
18-
(function() {
19-
var test;
19+
test = 'It works.';
2020
21-
test = 'It works.';
21+
module.exports = function() {
22+
return [test, arguments];
23+
};
2224
23-
module.exports = function() {
24-
return [test, arguments];
25-
};
25+
}).call(this);
2626
27-
}).call(this);
27+
return module.exports.apply(this, arguments);
28+
}
29+
'''
30+
]
2831

29-
return module.exports.apply(this, arguments);
30-
}
31-
'''
32-
]
32+
return @subject.handle path
33+
.then (actual) ->
34+
assert.deepEqual actual, expected
3335

34-
return @subject.handle path
35-
.then (actual) ->
36-
assert.deepEqual actual, expected
36+
it 'resolves to null for non-CoffeeScript files', ->
37+
path = "#{__dirname}/../../fixture/valid/other.other"
3738

38-
it 'resolves to null for non-CoffeeScript files', ->
39-
path = "#{__dirname}/../../fixture/valid/file"
39+
return @subject.handle path
40+
.then (actual) ->
41+
assert.isNull actual
4042

41-
return @subject.handle path
42-
.then (actual) ->
43-
assert.isNull actual
43+
it 'handles invalid CoffeeScript data', ->
44+
path = "#{__dirname}/../../fixture/invalid/coffee.coffee"
4445

45-
it 'handles file system errors', ->
46-
path = "#{__dirname}/../../fixture/invalid/nonexistent.js"
46+
return @subject.handle path
47+
.catch (actual) ->
48+
assert.instanceOf actual, SyntaxError
4749

48-
return @subject.handle path
49-
.catch (actual) ->
50-
assert.instanceOf actual, Error
50+
it 'handles file system errors', ->
51+
path = "#{__dirname}/../../fixture/invalid/nonexistent.js"
52+
53+
return @subject.handle path
54+
.catch (actual) ->
55+
assert.instanceOf actual, Error

test/suite/handler/CommonJsHandler.spec.coffee

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,38 @@ describe 'CommonJsHandler', ->
55
beforeEach ->
66
@subject = new CommonJsHandler()
77

8-
describe 'handle', ->
9-
10-
it 'resolves to a module wrapper for JavaScript files', ->
11-
path = "#{__dirname}/../../fixture/valid/js.js"
12-
expected = [
13-
'js'
14-
'''
15-
function () {
16-
var module = {};
17-
(function () {
18-
19-
var test = 'It works.';
20-
module.exports = function () { return [test, arguments] };
21-
22-
}).call(this);
23-
return module.exports.apply(this, arguments);
24-
}
25-
'''
26-
]
27-
28-
return @subject.handle path
29-
.then (actual) ->
30-
assert.deepEqual actual, expected
31-
32-
it 'resolves to null for non-JavaScript files', ->
33-
path = "#{__dirname}/../../fixture/valid/file"
34-
35-
return @subject.handle path
36-
.then (actual) ->
37-
assert.isNull actual
38-
39-
it 'handles file system errors', ->
40-
path = "#{__dirname}/../../fixture/invalid/nonexistent.js"
41-
42-
return @subject.handle path
43-
.catch (actual) ->
44-
assert.instanceOf actual, Error
8+
it 'resolves to a module wrapper for JavaScript files', ->
9+
path = "#{__dirname}/../../fixture/valid/js.js"
10+
expected = [
11+
'js'
12+
'''
13+
function () {
14+
var module = {};
15+
(function () {
16+
17+
var test = 'It works.';
18+
module.exports = function () { return [test, arguments] };
19+
20+
}).call(this);
21+
return module.exports.apply(this, arguments);
22+
}
23+
'''
24+
]
25+
26+
return @subject.handle path
27+
.then (actual) ->
28+
assert.deepEqual actual, expected
29+
30+
it 'resolves to null for non-JavaScript files', ->
31+
path = "#{__dirname}/../../fixture/valid/other.other"
32+
33+
return @subject.handle path
34+
.then (actual) ->
35+
assert.isNull actual
36+
37+
it 'handles file system errors', ->
38+
path = "#{__dirname}/../../fixture/invalid/nonexistent.js"
39+
40+
return @subject.handle path
41+
.catch (actual) ->
42+
assert.instanceOf actual, Error

0 commit comments

Comments
 (0)