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
Expand Up @@ -4,6 +4,7 @@
var geddy = require('../lib/geddy');

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

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

var fs = require('fs')
, path = require('path')
, existsSync = typeof fs.existsSync == 'function' ?
fs.existsSync : path.existsSync
, url = require('url')
, querystring = require('../deps/qs')
, path = require('path')
, cwd = process.cwd()
, errors = require('./response/errors')
, response = require('./response')
Expand Down Expand Up @@ -100,7 +102,7 @@ var App = function () {
, adapterPath
, appAdaptersPath = path.join(cwd, modelDir, '/adapters/');
// May be running entirely model-less
if (!path.existsSync(path.join(cwd, modelDir))) {
if (!existsSync(path.join(cwd, modelDir))) {
next();
}
else {
Expand All @@ -117,10 +119,10 @@ var App = function () {
// - 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 (path.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.js')) ||
path.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.coffee'))) {
if (existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.js')) ||
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 {
usingCoffee = usingCoffee || require('coffee-script');
} catch(err) {
Expand All @@ -131,7 +133,7 @@ var App = function () {
geddy.model.adapter[item.ctorName] = require(adapterPath)[adapterName];
}
// 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};
adapterPath = path.join(__dirname, "./model/adapters/", adapterName.toLowerCase());
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
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));
}
// 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) {
routerCsFile = path.join(cwd, '/config/router.coffee');
if(path.existsSync(routerCsFile)) {
if(existsSync(routerCsFile)) {
try {
require('coffee-script');
} catch(err) {
Expand Down Expand Up @@ -224,7 +226,7 @@ var App = function () {
, _registerTemplatePaths = function (next) {
var viewsPath = path.normalize('app/views');
// May be running entirely viewless
if (!path.existsSync(viewsPath)) {
if (!existsSync(viewsPath)) {
this.templateRegistry = {};
next();
}
Expand Down Expand Up @@ -588,7 +590,7 @@ var App = function () {
// Decode path (e.g. %20)
staticPath = decodeURIComponent(staticPath);

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

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();
// Clustering-only
cluster.addListener('death', function (worker) {
var id = worker.pid.toString();
self.handleWorkerExit(id);
});
// Node 0.6
cluster.addListener('death', handleExit);
// Node 0.8
cluster.addListener('exit', handleExit);
};

Master.prototype = new (function () {
Expand Down Expand Up @@ -121,7 +128,7 @@ Master.prototype = new (function () {
types.forEach(function (type) {
var currentLog = path.join(dir, type + '.log')
, archivedLog = path.join(dir, type + '.' + now + '.log');
if (path.existsSync(currentLog)) {
if (existsSync(currentLog)) {
try {
fs.renameSync(currentLog, archivedLog);
}
Expand Down Expand Up @@ -320,7 +327,9 @@ Master.prototype = new (function () {
var self = this
, retireAt = dt || (new Date()).getTime() + this.config.rotationWindow
, 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);
this.workers.addItem(id, data, retireAt);
this.addWorkerListeners(w);
Expand Down
4 changes: 3 additions & 1 deletion lib/init/i18n.js
Expand Up @@ -2,6 +2,8 @@ var i18n = require('../i18n')
, exec = require('child_process').exec
, path = require('path')
, fs = require('fs')
, existsSync = typeof fs.existsSync == 'function' ?
fs.existsSync : path.existsSync
, fileUtils = require('../utils/file');

module.exports = new (function () {
Expand All @@ -17,7 +19,7 @@ module.exports = new (function () {
, loadLocaleData = function () {
localePaths.forEach(function (directory) {
directory = path.normalize(directory);
if (path.existsSync(directory)) {
if (existsSync(directory)) {
var files = fileUtils.readdirR(directory);
for (var i = 0; i < files.length; i++) {
file = files[i];
Expand Down
98 changes: 84 additions & 14 deletions lib/utils/file.js
@@ -1,5 +1,13 @@
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 _copyFile = function(fromPath, toPath, opts) {
Expand All @@ -18,12 +26,10 @@ var fileUtils = new (function () {
fromStat = fs.statSync(from);

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

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

opts = {}; // Reset

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

// 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);
}

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

0 comments on commit ffe0c97

Please sign in to comment.