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
25 changes: 16 additions & 9 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)');
Expand Down
22 changes: 22 additions & 0 deletions test/compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({}, {}, {
Expand Down