Skip to content

Commit

Permalink
Merge branch 'master' into extending_template_engine_support
Browse files Browse the repository at this point in the history
  • Loading branch information
larzconwell committed Jun 27, 2012
2 parents 60ad15e + 56706a8 commit ffe0c97
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 34 deletions.
5 changes: 4 additions & 1 deletion bin/cli.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var geddy = require('../lib/geddy'); var geddy = require('../lib/geddy');


var exec = require('child_process').exec var exec = require('child_process').exec
, fs = require('fs')
, path = require('path') , path = require('path')
, parseopts = require('../lib/parseopts') , parseopts = require('../lib/parseopts')
, utils = require('../lib/utils/index') , utils = require('../lib/utils/index')
Expand Down Expand Up @@ -135,7 +136,9 @@ else {
// Just `geddy` -- start the server // Just `geddy` -- start the server
else { else {
var configPath = path.join(cwd, 'config') var configPath = path.join(cwd, 'config')
, geddyApp = path.existsSync(configPath); , existsSync = typeof fs.existsSync == 'function' ?
fs.existsSync : path.existsSync
, geddyApp = existsSync(configPath);


if(geddyApp) { if(geddyApp) {
// Start the server // Start the server
Expand Down
22 changes: 12 additions & 10 deletions lib/app.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
*/ */


var fs = require('fs') var fs = require('fs')
, path = require('path')
, existsSync = typeof fs.existsSync == 'function' ?
fs.existsSync : path.existsSync
, url = require('url') , url = require('url')
, querystring = require('../deps/qs') , querystring = require('../deps/qs')
, path = require('path')
, cwd = process.cwd() , cwd = process.cwd()
, errors = require('./response/errors') , errors = require('./response/errors')
, response = require('./response') , response = require('./response')
Expand Down Expand Up @@ -100,7 +102,7 @@ var App = function () {
, adapterPath , adapterPath
, appAdaptersPath = path.join(cwd, modelDir, '/adapters/'); , appAdaptersPath = path.join(cwd, modelDir, '/adapters/');
// May be running entirely model-less // May be running entirely model-less
if (!path.existsSync(path.join(cwd, modelDir))) { if (!existsSync(path.join(cwd, modelDir))) {
next(); next();
} }
else { else {
Expand All @@ -117,10 +119,10 @@ var App = function () {
// - But I couldn't think of a reliable way that would work // - But I couldn't think of a reliable way that would work


// If the app has an adapter that matches the model adapter, use it. // If the app has an adapter that matches the model adapter, use it.
if (path.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.js')) || if (existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.js')) ||
path.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.coffee'))) { existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.coffee'))) {


if(path.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.coffee'))) { if(existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.coffee'))) {
try { try {
usingCoffee = usingCoffee || require('coffee-script'); usingCoffee = usingCoffee || require('coffee-script');
} catch(err) { } catch(err) {
Expand All @@ -131,7 +133,7 @@ var App = function () {
geddy.model.adapter[item.ctorName] = require(adapterPath)[adapterName]; geddy.model.adapter[item.ctorName] = require(adapterPath)[adapterName];
} }
// if the model has been defined by geddy, use it. // if the model has been defined by geddy, use it.
else if (path.existsSync(path.join(__dirname, "./model/adapters/", adapterName.toLowerCase() + '.js'))) { else if (existsSync(path.join(__dirname, "./model/adapters/", adapterName.toLowerCase() + '.js'))) {
var config = {model: item.ctorName}; var config = {model: item.ctorName};
adapterPath = path.join(__dirname, "./model/adapters/", adapterName.toLowerCase()); adapterPath = path.join(__dirname, "./model/adapters/", adapterName.toLowerCase());
geddy.model.adapter[item.ctorName] = require(adapterPath)[adapterName](config); geddy.model.adapter[item.ctorName] = require(adapterPath)[adapterName](config);
Expand All @@ -142,7 +144,7 @@ var App = function () {
} }
// if the model doesn't define an adapter, use the default // if the model doesn't define an adapter, use the default
else if (geddy.config.adapters && geddy.config.adapters.default && else if (geddy.config.adapters && geddy.config.adapters.default &&
path.existsSync(path.join(__dirname, "./model/adapters/", geddy.config.adapters.default+".js"))) { existsSync(path.join(__dirname, "./model/adapters/", geddy.config.adapters.default+".js"))) {
geddy.model.adapter[item.ctorName] = require(path.join(__dirname, "./model/adapters/", geddy.config.adapters.default)); geddy.model.adapter[item.ctorName] = require(path.join(__dirname, "./model/adapters/", geddy.config.adapters.default));
} }
// the adapter will be undefined if the model doesn't define it, // the adapter will be undefined if the model doesn't define it,
Expand Down Expand Up @@ -179,7 +181,7 @@ var App = function () {
// ================== // ==================
, _loadRouter = function (next) { , _loadRouter = function (next) {
routerCsFile = path.join(cwd, '/config/router.coffee'); routerCsFile = path.join(cwd, '/config/router.coffee');
if(path.existsSync(routerCsFile)) { if(existsSync(routerCsFile)) {
try { try {
require('coffee-script'); require('coffee-script');
} catch(err) { } catch(err) {
Expand Down Expand Up @@ -224,7 +226,7 @@ var App = function () {
, _registerTemplatePaths = function (next) { , _registerTemplatePaths = function (next) {
var viewsPath = path.normalize('app/views'); var viewsPath = path.normalize('app/views');
// May be running entirely viewless // May be running entirely viewless
if (!path.existsSync(viewsPath)) { if (!existsSync(viewsPath)) {
this.templateRegistry = {}; this.templateRegistry = {};
next(); next();
} }
Expand Down Expand Up @@ -588,7 +590,7 @@ var App = function () {
// Decode path (e.g. %20) // Decode path (e.g. %20)
staticPath = decodeURIComponent(staticPath); staticPath = decodeURIComponent(staticPath);


if (path.existsSync(staticPath)) { if (existsSync(staticPath)) {
staticResp = new response.Response(resp); staticResp = new response.Response(resp);
staticResp.sendFile(staticPath); staticResp.sendFile(staticPath);
} }
Expand Down
25 changes: 17 additions & 8 deletions lib/cluster/master.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var Master
, cluster , cluster
, fs = require('fs') , fs = require('fs')
, path = require('path') , path = require('path')
, existsSync = typeof fs.existsSync == 'function' ?
fs.existsSync : path.existsSync
, errors = require('../response/errors') , errors = require('../response/errors')
, watchFiles = require('../watch_files') , watchFiles = require('../watch_files')
, Log = require('../../deps/log') , Log = require('../../deps/log')
Expand All @@ -20,13 +22,18 @@ var processModes = {
}; };


Master = function () { Master = function () {
var self = this; var self = this
, handleExit = function (worker) {
// Node 0.8 vs. 0.6
var proc = worker.process || worker
, id = proc.pid.toString();
self.handleWorkerExit(id);
};
this.init(); this.init();
// Clustering-only // Node 0.6
cluster.addListener('death', function (worker) { cluster.addListener('death', handleExit);
var id = worker.pid.toString(); // Node 0.8
self.handleWorkerExit(id); cluster.addListener('exit', handleExit);
});
}; };


Master.prototype = new (function () { Master.prototype = new (function () {
Expand Down Expand Up @@ -121,7 +128,7 @@ Master.prototype = new (function () {
types.forEach(function (type) { types.forEach(function (type) {
var currentLog = path.join(dir, type + '.log') var currentLog = path.join(dir, type + '.log')
, archivedLog = path.join(dir, type + '.' + now + '.log'); , archivedLog = path.join(dir, type + '.' + now + '.log');
if (path.existsSync(currentLog)) { if (existsSync(currentLog)) {
try { try {
fs.renameSync(currentLog, archivedLog); fs.renameSync(currentLog, archivedLog);
} }
Expand Down Expand Up @@ -320,7 +327,9 @@ Master.prototype = new (function () {
var self = this var self = this
, retireAt = dt || (new Date()).getTime() + this.config.rotationWindow , retireAt = dt || (new Date()).getTime() + this.config.rotationWindow
, w = cluster.fork() , w = cluster.fork()
, id = w.pid.toString() // Node 0.8 vs. 0.6
, proc = w.process || w
, id = proc.pid.toString()
, data = new WorkerData(id, w); , data = new WorkerData(id, w);
this.workers.addItem(id, data, retireAt); this.workers.addItem(id, data, retireAt);
this.addWorkerListeners(w); this.addWorkerListeners(w);
Expand Down
4 changes: 3 additions & 1 deletion lib/init/i18n.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var i18n = require('../i18n')
, exec = require('child_process').exec , exec = require('child_process').exec
, path = require('path') , path = require('path')
, fs = require('fs') , fs = require('fs')
, existsSync = typeof fs.existsSync == 'function' ?
fs.existsSync : path.existsSync
, fileUtils = require('../utils/file'); , fileUtils = require('../utils/file');


module.exports = new (function () { module.exports = new (function () {
Expand All @@ -17,7 +19,7 @@ module.exports = new (function () {
, loadLocaleData = function () { , loadLocaleData = function () {
localePaths.forEach(function (directory) { localePaths.forEach(function (directory) {
directory = path.normalize(directory); directory = path.normalize(directory);
if (path.existsSync(directory)) { if (existsSync(directory)) {
var files = fileUtils.readdirR(directory); var files = fileUtils.readdirR(directory);
for (var i = 0; i < files.length; i++) { for (var i = 0; i < files.length; i++) {
file = files[i]; file = files[i];
Expand Down
98 changes: 84 additions & 14 deletions lib/utils/file.js
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,13 @@
var fs = require('fs') var fs = require('fs')
, path = require('path'); , path = require('path')
, logger;

try {
logger = require('./logger');
}
catch (e) {
logger = console.log;
}


var fileUtils = new (function () { var fileUtils = new (function () {
var _copyFile = function(fromPath, toPath, opts) { var _copyFile = function(fromPath, toPath, opts) {
Expand All @@ -18,12 +26,10 @@ var fileUtils = new (function () {
fromStat = fs.statSync(from); fromStat = fs.statSync(from);


try { try {
//console.dir(to + ' destExists');
toStat = fs.statSync(to); toStat = fs.statSync(to);
destExists = true; destExists = true;
} }
catch(e) { catch(e) {
//console.dir(to + ' does not exist');
destDoesNotExistErr = e; destDoesNotExistErr = e;
destExists = false; destExists = false;
} }
Expand All @@ -49,7 +55,6 @@ var fileUtils = new (function () {
} }
} }
for (var i = 0, ii = dirContents.length; i < ii; i++) { for (var i = 0, ii = dirContents.length; i < ii; i++) {
//console.log(dirContents[i]);
_copyFile(path.join(from, dirContents[i]), targetDir); _copyFile(path.join(from, dirContents[i]), targetDir);
} }
} }
Expand All @@ -58,12 +63,10 @@ var fileUtils = new (function () {
content = fs.readFileSync(from); content = fs.readFileSync(from);
// Copy into dir // Copy into dir
if (toStat.isDirectory()) { if (toStat.isDirectory()) {
//console.log('copy into dir ' + to);
fs.writeFileSync(path.join(to, filename), content); fs.writeFileSync(path.join(to, filename), content);
} }
// Overwrite file // Overwrite file
else { else {
//console.log('overwriting ' + to);
fs.writeFileSync(to, content); fs.writeFileSync(to, content);
} }
} }
Expand All @@ -85,24 +88,48 @@ var fileUtils = new (function () {
paths = fs.readdirSync(dir); paths = fs.readdirSync(dir);
paths.forEach(function (p) { paths.forEach(function (p) {
var curr = path.join(dir, p); var curr = path.join(dir, p);
//console.log(curr);
var stat = fs.statSync(curr); var stat = fs.statSync(curr);
ret.push(curr);
if (stat.isDirectory()) { if (stat.isDirectory()) {
ret = ret.concat(_readDir(curr)); ret = ret.concat(_readDir(curr));
} }
else {
ret.push(curr);
}
}); });
return ret; return ret;
}

, _rmDir = function (dirPath) {
var dir = path.normalize(dirPath)
, paths = [];
paths = fs.readdirSync(dir);
paths.forEach(function (p) {
var curr = path.join(dir, p);
var stat = fs.statSync(curr);
if (stat.isDirectory()) {
_rmDir(curr);
}
else {
fs.unlinkSync(curr);
}
});
fs.rmdirSync(dir);
}; };


this.cpR = function (fromPath, toPath) { this.cpR = function (fromPath, toPath, options) {
var from = path.normalize(fromPath) var from = path.normalize(fromPath)
, to = path.normalize(toPath) , to = path.normalize(toPath)
, toStat , toStat
, doesNotExistErr , doesNotExistErr
, paths , paths
, filename , filename
, opts = {}; , opts = options || {};

if (!opts.silent) {
logger.log('cp -r ' + fromPath + ' ' + toPath);
}

opts = {}; // Reset


if (from == to) { if (from == to) {
throw new Error('Cannot copy ' + from + ' to itself.'); throw new Error('Cannot copy ' + from + ' to itself.');
Expand All @@ -116,7 +143,7 @@ var fileUtils = new (function () {
doesNotExistErr = e; doesNotExistErr = e;


// Get abs path so it's possible to check parent dir // Get abs path so it's possible to check parent dir
if (to.indexOf('/') != 0 || /^[A-Za-z]+:/.test(to)) { if (!this.isAbsolute(to)) {
to = path.join(process.cwd() , to); to = path.join(process.cwd() , to);
} }


Expand All @@ -133,7 +160,6 @@ var fileUtils = new (function () {
// Set the rename opt to pass to the copy func, will be used // Set the rename opt to pass to the copy func, will be used
// as the new file/dir name // as the new file/dir name
opts.rename = filename; opts.rename = filename;
//console.log('filename ' + filename);
} }
else { else {
throw doesNotExistErr; throw doesNotExistErr;
Expand All @@ -152,7 +178,6 @@ var fileUtils = new (function () {
if (paths[0] == '' || /^[A-Za-z]+:/.test(paths[0])) { if (paths[0] == '' || /^[A-Za-z]+:/.test(paths[0])) {
currPath = paths.shift() || '/'; currPath = paths.shift() || '/';
currPath = path.join(currPath, paths.shift()); currPath = path.join(currPath, paths.shift());
//console.log('basedir');
} }
while ((next = paths.shift())) { while ((next = paths.shift())) {
if (next == '..') { if (next == '..') {
Expand All @@ -161,7 +186,6 @@ var fileUtils = new (function () {
} }
currPath = path.join(currPath, next); currPath = path.join(currPath, next);
try { try {
//console.log('making ' + currPath);
fs.mkdirSync(currPath, mode || 0755); fs.mkdirSync(currPath, mode || 0755);
} }
catch(e) { catch(e) {
Expand All @@ -179,6 +203,52 @@ var fileUtils = new (function () {
ret = _readDir(dir); ret = _readDir(dir);
return format == 'string' ? ret.join('\n') : ret; return format == 'string' ? ret.join('\n') : ret;
}; };

this.rmRf = function (p, options) {
var stat
, opts = options || {};
if (!opts.silent) {
logger.log('rm -rf ' + p);
}
try {
stat = fs.statSync(p);
if (stat.isDirectory()) {
_rmDir(p);
}
else {
fs.unlinkSync(p);
}
}
catch (e) {}
};

this.isAbsolute = function (p) {
var match = /^[A-Za-z]+:\\|^\//.exec(p);
if (match && match.length) {
return match[0];
}
return false;
};

this.absolutize = function (p) {
if (this.isAbsolute(p)) {
return p;
}
else {
return path.join(process.cwd(), p);
}
};

this.basedir = function (p) {
var str = p || ''
, abs = this.isAbsolute(p);
if (abs) {
return abs;
}
str = str.replace(/\*/g, '').split('/')[0];
return str || '.';
};

})(); })();


module.exports = fileUtils; module.exports = fileUtils;
Expand Down

0 comments on commit ffe0c97

Please sign in to comment.