Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -185,22 +185,38 @@ 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) {
return callback(err);
}

if (stats.isFile()) {
return callback(null, true);
return callback(null, '.js');
}
})
}
Expand All @@ -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);
Expand All @@ -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':
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
}
2 changes: 2 additions & 0 deletions test/compile-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('coffee-script/register');

var test = require('tape');
var compile = require('..');
var fs = require('fs');
Expand Down
4 changes: 4 additions & 0 deletions test/expected/coffee-module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_id": "coffee-module",
"foo": "bar"
}
10 changes: 10 additions & 0 deletions test/expected/coffee.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"_id": "_design/full",
"language": "coffeescript",
"views": {
"numbers": {
"map": "(doc) -> emit doc.number, null if doc.number",
"reduce": "_count"
}
}
}
1 change: 1 addition & 0 deletions test/fixtures/coffee-module/index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = foo: 'bar'
1 change: 1 addition & 0 deletions test/fixtures/coffee/_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_design/full
1 change: 1 addition & 0 deletions test/fixtures/coffee/language
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coffeescript
1 change: 1 addition & 0 deletions test/fixtures/coffee/views/numbers/map.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(doc) -> emit doc.number, null if doc.number
1 change: 1 addition & 0 deletions test/fixtures/coffee/views/numbers/reduce
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_count