Skip to content

Commit

Permalink
Fixed js-yaml library usage
Browse files Browse the repository at this point in the history
* ormDriver function in the generators fixed to use database.yml right
* locales and utils fixed to get first document from YAML file (there
  is no needs in the multiple documents per file support)
  • Loading branch information
Phillip Kovalev committed Jun 4, 2012
1 parent 799d45e commit 7d71b2a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
28 changes: 27 additions & 1 deletion lib/generators.js
Expand Up @@ -675,13 +675,39 @@ exports.list = function () {
return template.replace(/PREPEND_MIDDLEWARE/g, mw.join('\n '));
};

function loadConfig(filename) {
var filenameExt = /\.([^\.]+)$/.exec(filename)[1];
switch (filenameExt) {
case '~':
var config = null;
['.json','.yml'].forEach(function(testExt){
try {
var testFilename = filename.replace(/\.~$/, testExt)
var stats = fs.lstatSync(testFilename);
if (stats.isFile())
config = loadConfig(testFilename);
}
catch(e) {}
});
if (config != null) return config;
throw new Error( sys.format('Can not find configuration file by path template"%s"', filename) );
break;
case 'json':
return JSON.parse(require('fs').readFileSync(filename, 'utf8'));
case 'yml':
return require(filename).shift();
default:
throw new Error( sys.format('Unknown configuration file name extension "%s"', filenameExt) );
}
};

/**
* Return default ORM driver, based on config/database.json
* @deprecated
*/
function ormDriver() {
if (!ormDriver.config) {
ormDriver.config = JSON.parse(require('fs').readFileSync(process.cwd() + '/config/database.json', 'utf8')).development;
ormDriver.config = loadConfig(process.cwd() + '/config/database.~').development;
}
return ormDriver.config.driver;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/locales.js
Expand Up @@ -37,7 +37,7 @@ exports.load = function (dir) {
var obj;
try {
if (file.match(/\.ya?ml$/)) {
obj = require(filename);
obj = require(filename).shift();
} else if (file.match(/\.json/)) {
obj = JSON.parse(code);
} else if (file.match(/\.coffee/)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/railway_utils.js
Expand Up @@ -192,7 +192,7 @@ exports.addSpaces = addSpaces;

function readYaml(file) {
try {
return require(file);
return require(file).shift();
} catch (e) {
console.log('Error in reading', file);
console.log(e.message);
Expand Down

0 comments on commit 7d71b2a

Please sign in to comment.