diff --git a/lib/compiler.js b/lib/compiler.js index d5d870a..37df616 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -247,13 +247,18 @@ function findModelDefinitions(rootDir, sources) { sources.forEach(function(src) { var srcDir = path.resolve(rootDir, src); var files = tryReadDir(srcDir); + + var otherFiles = files.filter(function(f){ + return path.extname(f) !== '.json'; + }); + files .filter(function(f) { return f[0] !== '_' && path.extname(f) === '.json'; }) .forEach(function(f) { var fullPath = path.resolve(srcDir, f); - var entry = loadModelDefinition(rootDir, fullPath); + var entry = loadModelDefinition(rootDir, fullPath, otherFiles); var modelName = entry.definition.name; if (!modelName) { debug('Skipping model definition without Model name: %s', @@ -267,24 +272,26 @@ function findModelDefinitions(rootDir, sources) { return registry; } -function loadModelDefinition(rootDir, jsonFile) { +function loadModelDefinition(rootDir, jsonFile, otherFiles) { var definition = require(jsonFile); - var sourceFile = path.join( - path.dirname(jsonFile), - path.basename(jsonFile, path.extname(jsonFile))); + var basename = path.basename(jsonFile, path.extname(jsonFile)) + + // find a matching file with `.js` or any other supported extension like `.coffee` + var sourceFile = otherFiles + .filter(function(f) { + return path.basename(f, path.extname(f)) === basename; + })[0]; try { - // resolve the file to `.js` or any other supported extension like `.coffee` + sourceFile = path.join(path.dirname(jsonFile), sourceFile); sourceFile = require.resolve(sourceFile); + } catch (err) { debug('Model source code not found: %s - %s', sourceFile, err.code || err); sourceFile = undefined; } - if (sourceFile === jsonFile) - sourceFile = undefined; - debug('Found model "%s" - %s %s', definition.name, path.relative(rootDir, jsonFile), sourceFile ? path.relative(rootDir, sourceFile) : '(no source file)'); diff --git a/test/compiler.test.js b/test/compiler.test.js index 45e17ec..af49fc0 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -450,6 +450,28 @@ describe('compiler', function() { sourceFile: path.resolve(appdir.PATH, 'models', 'car.js') }); }); + + it('loads coffeescript models from `./models`', function() { + appdir.createConfigFilesSync({}, {}, { + Car: { dataSource: 'db' } + }); + appdir.writeConfigFileSync('models/car.json', { name: 'Car' }); + appdir.writeFileSync('models/car.coffee', ''); + + var instructions = boot.compile(appdir.PATH); + + expect(instructions.models).to.have.length(1); + expect(instructions.models[0]).to.eql({ + name: 'Car', + config: { + dataSource: 'db' + }, + definition: { + name: 'Car' + }, + sourceFile: path.resolve(appdir.PATH, 'models', 'car.coffee') + }); + }); it('supports `modelSources` option', function() { appdir.createConfigFilesSync({}, {}, {