diff --git a/index.js b/index.js index b6fdae6..3c9b239 100644 --- a/index.js +++ b/index.js @@ -40,7 +40,7 @@ function idFromFilename(filename, ext) { } // Compile a Couchapp module. -function compileModule(filename, options, callback) { +function compileModule(extension, filename, options, callback) { var doc; var err; @@ -55,7 +55,7 @@ function compileModule(filename, options, callback) { } if (!doc._id) { - doc._id = idFromFilename(filename, '.js'); + doc._id = idFromFilename(filename, extension); } callback(null, doc); @@ -185,14 +185,30 @@ function compileDirectory(dir, options, callback) { function useIndex(source, options, callback) { if (!options.index) { - return callback(null, false); + return callback(null, null); } var filename = path.join(source, 'index.js'); fs.stat(filename, function(err, stats) { if (err && err.code === 'ENOENT') { - return callback(null, false); + filename = path.join(source, 'index.coffee'); + + fs.stat(filename, function(err, stats) { + if (err && err.code === 'ENOENT') { + return callback(null, null); + } + + if (err) { + return callback(err); + } + + if (stats.isFile()) { + return callback(null, '.coffee'); + } + }) + + return; } if (err) { @@ -200,7 +216,7 @@ function useIndex(source, options, callback) { } if (stats.isFile()) { - return callback(null, true); + return callback(null, '.js'); } }) } @@ -226,13 +242,13 @@ module.exports = function compile(source, options, callback) { } if (stats.isDirectory()) { - return useIndex(source, options, function(err, answer) { + return useIndex(source, options, function(err, ext) { if (err) { return callback(err); } - if (answer) { - return compileModule(source, options, callback); + if (null !== ext) { + return compileModule(ext, source, options, callback); } compileDirectory(source, options, callback); @@ -245,9 +261,10 @@ module.exports = function compile(source, options, callback) { var ext = path.extname(source); - switch (path.extname(source)) { + switch (ext) { case '.js': - compileModule(source, options, callback); + case '.coffee': + compileModule(ext, source, options, callback); break; case '.json': diff --git a/package.json b/package.json index 00492ca..90eaab9 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "couchdb-compile", + "version": "0.0.0-semantically-released", "description": "Build CouchDB documents from fs.", "main": "index.js", "bin": "cli.js", @@ -28,8 +29,9 @@ "minimist": "^1.2.0" }, "devDependencies": { + "coffee-script": "^1.10.0", + "semantic-release": "^6.0.3", "tap-spec": "^4.1.0", - "tape": "^4.2.1", - "semantic-release": "^6.0.3" + "tape": "^4.2.1" } } diff --git a/test/compile-test.js b/test/compile-test.js index 67cba5c..44c0445 100644 --- a/test/compile-test.js +++ b/test/compile-test.js @@ -1,3 +1,5 @@ +require('coffee-script/register'); + var test = require('tape'); var compile = require('..'); var fs = require('fs'); diff --git a/test/expected/coffee-module.json b/test/expected/coffee-module.json new file mode 100644 index 0000000..9324ce5 --- /dev/null +++ b/test/expected/coffee-module.json @@ -0,0 +1,4 @@ +{ + "_id": "coffee-module", + "foo": "bar" +} diff --git a/test/expected/coffee.json b/test/expected/coffee.json new file mode 100644 index 0000000..1bbe18d --- /dev/null +++ b/test/expected/coffee.json @@ -0,0 +1,10 @@ +{ + "_id": "_design/full", + "language": "coffeescript", + "views": { + "numbers": { + "map": "(doc) -> emit doc.number, null if doc.number", + "reduce": "_count" + } + } +} diff --git a/test/fixtures/coffee-module/index.coffee b/test/fixtures/coffee-module/index.coffee new file mode 100644 index 0000000..8c260b7 --- /dev/null +++ b/test/fixtures/coffee-module/index.coffee @@ -0,0 +1 @@ +module.exports = foo: 'bar' diff --git a/test/fixtures/coffee/_id b/test/fixtures/coffee/_id new file mode 100644 index 0000000..9249e89 --- /dev/null +++ b/test/fixtures/coffee/_id @@ -0,0 +1 @@ +_design/full diff --git a/test/fixtures/coffee/language b/test/fixtures/coffee/language new file mode 100644 index 0000000..df56ceb --- /dev/null +++ b/test/fixtures/coffee/language @@ -0,0 +1 @@ +coffeescript diff --git a/test/fixtures/coffee/views/numbers/map.coffee b/test/fixtures/coffee/views/numbers/map.coffee new file mode 100644 index 0000000..87e56cd --- /dev/null +++ b/test/fixtures/coffee/views/numbers/map.coffee @@ -0,0 +1 @@ +(doc) -> emit doc.number, null if doc.number diff --git a/test/fixtures/coffee/views/numbers/reduce b/test/fixtures/coffee/views/numbers/reduce new file mode 100644 index 0000000..c866cd7 --- /dev/null +++ b/test/fixtures/coffee/views/numbers/reduce @@ -0,0 +1 @@ +_count