From 21f6f25f1fce05717b20ded616bd7aaffd4d3363 Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Fri, 8 Jan 2016 09:13:16 +1000 Subject: [PATCH 1/4] Rudimentary CoffeeScript support. --- index.js | 35 ++++++++++++++----- package.json | 5 +-- test/compile-test.js | 2 ++ test/expected/coffee-module.json | 4 +++ test/expected/coffee.json | 10 ++++++ test/fixtures/coffee-module/index.coffee | 1 + test/fixtures/coffee/_id | 1 + test/fixtures/coffee/language | 1 + test/fixtures/coffee/views/numbers/map.coffee | 1 + test/fixtures/coffee/views/numbers/reduce | 1 + 10 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 test/expected/coffee-module.json create mode 100644 test/expected/coffee.json create mode 100644 test/fixtures/coffee-module/index.coffee create mode 100644 test/fixtures/coffee/_id create mode 100644 test/fixtures/coffee/language create mode 100644 test/fixtures/coffee/views/numbers/map.coffee create mode 100644 test/fixtures/coffee/views/numbers/reduce diff --git a/index.js b/index.js index b6fdae6..4608d29 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); @@ -192,7 +192,23 @@ function useIndex(source, options, callback) { 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..a3f40e2 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,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 From 1054a09af1e66f64fcb37e8d5d93b833e501b61c Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Fri, 8 Jan 2016 09:19:42 +1000 Subject: [PATCH 2/4] Add dummy version to package.json so that dev versions can be installed. --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index a3f40e2..6d88ed0 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "couchdb-compile", + "version": "0.0.0", "description": "Build CouchDB documents from fs.", "main": "index.js", "bin": "cli.js", From 482cfc4f92913af75c63fe77b5de3971ae28af4e Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Sun, 10 Jan 2016 11:02:16 +1000 Subject: [PATCH 3/4] Fixed bug when a directory has no index. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 4608d29..3c9b239 100644 --- a/index.js +++ b/index.js @@ -185,7 +185,7 @@ 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'); From 0a0a35093a438e7d2ce55ff9bbb29cd2ad334fb8 Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Sun, 10 Jan 2016 11:02:37 +1000 Subject: [PATCH 4/4] Updated placeholder version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6d88ed0..90eaab9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "couchdb-compile", - "version": "0.0.0", + "version": "0.0.0-semantically-released", "description": "Build CouchDB documents from fs.", "main": "index.js", "bin": "cli.js",