Skip to content

Commit

Permalink
A bunch of refactoring to replace local utils with installed utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Aug 2, 2012
1 parent 4dee0ca commit 3a55f1c
Show file tree
Hide file tree
Showing 34 changed files with 121 additions and 99 deletions.
24 changes: 15 additions & 9 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
var geddy = require('../lib/geddy')
, exec = require('child_process').exec
, path = require('path')
, parseopts = require('../lib/parseopts')
, utils = require('../lib/utils/index');
, utils = require('utilities')
, parseopts = require('../lib/parseopts');

// Variables
var cwd = process.cwd()
Expand Down Expand Up @@ -173,19 +173,25 @@ if(cmds.length) {

cmd += ' --quiet';
exec(cmd, function(err, stdout, stderr) {
if(err) throw err;

if(stderr) console.log(utils.string.trim(stderr));
if(stdout) console.log(utils.string.trim(stdout));
if(err) {
throw err;
}
if (stderr) {
console.log(utils.string.trim(stderr));
}
if (stdout) {
console.log(utils.string.trim(stdout));
}
});
}
// Just `geddy` -- start the server
else {
// Search for 'config' directory in parent directories
utils.fileUtils.searchParentPath('config', function(err, filePath) {
if(err) {
utils.file.searchParentPath('config', function(err, filePath) {
if (err) {
die(usage);
} else {
}
else {
start();
}
});
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 17 additions & 17 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var fs = require('fs')
, errors = require('./response/errors')
, response = require('./response')
, model = require('./model')
, utils = require('./utils')
, utils = require('utilities')
, inflection = require('../deps/inflection')
, i18n = require('./i18n')
, init = require('./init')
Expand Down Expand Up @@ -65,7 +65,7 @@ var App = function () {
if (JSPAT.test(fileName)) {
if(fileName.match(/\.coffee$/)) {
// fileName is a CoffeeScript file so try to require it
usingCoffee = usingCoffee || utils.compat.dependency('coffee-script');
usingCoffee = usingCoffee || utils.file.dependency('coffee-script');
}
// Strip the extension from the file name
fileName = fileName.replace(JSPAT, '');
Expand Down Expand Up @@ -96,7 +96,7 @@ var App = function () {
, adapterPath
, appAdaptersPath = path.join(cwd, modelDir, '/adapters/');
// May be running entirely model-less
if (!utils.compat.existsSync(path.join(cwd, modelDir))) {
if (!utils.file.existsSync(path.join(cwd, modelDir))) {
next();
}
else {
Expand All @@ -113,17 +113,17 @@ 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 (utils.compat.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.js')) ||
utils.compat.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.coffee'))) {
if (utils.file.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.js')) ||
utils.file.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.coffee'))) {

if(utils.compat.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.coffee'))) {
usingCoffee = usingCoffee || utils.compat.dependency('coffee-script');
if(utils.file.existsSync(path.join(appAdaptersPath, adapterName.toLowerCase() + '.coffee'))) {
usingCoffee = usingCoffee || utils.file.dependency('coffee-script');
}
adapterPath = path.join(appAdaptersPath, adapterName.toLowerCase());
geddy.model.adapter[item.ctorName] = require(adapterPath)[adapterName];
}
// if the model has been defined by geddy, use it.
else if (utils.compat.existsSync(path.join(__dirname, "./model/adapters/", adapterName.toLowerCase() + '.js'))) {
else if (utils.file.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 @@ -134,7 +134,7 @@ var App = function () {
}
// if the model doesn't define an adapter, use the default
else if (geddy.config.adapters && geddy.config.adapters.default &&
utils.compat.existsSync(path.join(__dirname, "./model/adapters/", geddy.config.adapters.default+".js"))) {
utils.file.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 @@ -173,8 +173,8 @@ var App = function () {
// ==================
, _loadRouter = function (next) {
routerCsFile = path.join(cwd, '/config/router.coffee');
if(utils.compat.existsSync(routerCsFile)) {
usingCoffee = usingCoffee || utils.compat.dependency('coffee-script');
if(utils.file.existsSync(routerCsFile)) {
usingCoffee = usingCoffee || utils.file.dependency('coffee-script');
}
router = require(path.join(cwd, '/config/router'));
router = router.router || router;
Expand Down Expand Up @@ -223,12 +223,12 @@ var App = function () {
, geddyTemplatesPath = path.join(__dirname, 'template', 'templates');

// If viewsPath doesn't exist they're running viewless
if(!utils.compat.existsSync(viewsPath)) {
if(!utils.file.existsSync(viewsPath)) {
this.templateRegistry = {};
next();
} else {
var files = utils.fileUtils.readdirR(viewsPath)
, geddyTemplatesFiles = utils.fileUtils.readdirR(geddyTemplatesPath)
var files = utils.file.readdirR(viewsPath)
, geddyTemplatesFiles = utils.file.readdirR(geddyTemplatesPath)
, pat = /\.(ejs|jade|hbs|mustache|ms|mu)$/
, templates = {}
, file
Expand Down Expand Up @@ -322,7 +322,7 @@ var App = function () {
if(fileBaseName === 'init') {
if(fileExt === '.coffee') {
// fileName is a CoffeeScript file so try to require it
usingCoffee = usingCoffee || utils.compat.dependency('coffee-script');
usingCoffee = usingCoffee || utils.file.dependency('coffee-script');
}
require(path.join(cwd, 'config', fileName));
}
Expand All @@ -341,7 +341,7 @@ var App = function () {
// the server starts listening for requests. Socket.io
// won't work if we start it before.
if (geddy.config.socketIo) {
geddy.io = utils.compat.dependency('socket.io').listen(geddy.server);
geddy.io = utils.file.dependency('socket.io').listen(geddy.server);
}
try {
require(path.join(cwd, 'config', 'after_start'));
Expand Down Expand Up @@ -625,7 +625,7 @@ var App = function () {
// Decode path (e.g. %20)
staticPath = decodeURIComponent(staticPath);

if (utils.compat.existsSync(staticPath)) {
if (utils.file.existsSync(staticPath)) {
staticResp = new response.Response(resp);
staticResp.sendFile(staticPath);
}
Expand Down
34 changes: 15 additions & 19 deletions lib/cluster/master.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
var Master
, cluster
, cluster = require('cluster')
, fs = require('fs')
, path = require('path')
, Log = require('../../deps/log')
, dispatch = require('./master_dispatch')
, fileUtils = require('../utils/file')
, dateUtils = require('../utils/date').date
, compat = require('../utils/compat').compat;

if (!geddy.FD_HACK) {
cluster = require('cluster');
}
, utils = require('utilities')
, file = utils.file
, date = utils.date;

var processModes = {
KEEP_ALIVE: 'keepAlive'
Expand Down Expand Up @@ -67,7 +63,7 @@ Master.prototype = new (function () {
if(fileBaseName === this.config.environment || fileBaseName === 'environment') {
if(fileExt === '.coffee') {
// fileName is a CoffeeScript file so try to require it
usingCoffee = usingCoffee || compat.dependency('coffee-script');
usingCoffee = usingCoffee || file.dependency('coffee-script');
}
appBaseConfig = require(dir + '/config/environment');
appEnvConfig = require(dir + '/config/' + this.config.environment);
Expand All @@ -92,7 +88,7 @@ Master.prototype = new (function () {
, levelsByType
, stdoutLevel
, types = ['stdout', 'stderr', 'access']
, now = dateUtils.strftime(new Date(), '%FT%T')
, now = date.strftime(new Date(), '%FT%T')
, loggly
, dir = this.config.logDir
, rotateAndInitByType;
Expand All @@ -116,19 +112,19 @@ Master.prototype = new (function () {
};

// Create the log directory if it doesn't exist
fileUtils.mkdirP(dir);
file.mkdirP(dir);

types.forEach(function (type) {
var currentLog = path.join(dir, type + '.log')
, archivedLog = path.join(dir, type + '.' + now + '.log');
if (geddy.compat.existsSync(currentLog)) {
if (file.existsSync(currentLog)) {
try {
fs.renameSync(currentLog, archivedLog);
}
catch(e) {
// cpR doesn't work correctly on Windows either, just blow
// away the fucking log
//fileUtils.cpR(currentLog, archivedLog);
//file.cpR(currentLog, archivedLog);
fs.unlinkSync(currentLog);
}
}
Expand All @@ -149,7 +145,7 @@ Master.prototype = new (function () {
, port;
if (this.config.metrics) {
port = this.config.metrics.port;
metrics = compat.dependency('metrics');
metrics = file.dependency('metrics');
this.stdoutLog.info('Metrics server started on port ' + port);
this.metricsServer = new metrics.Server(port);
}
Expand Down Expand Up @@ -244,11 +240,11 @@ Master.prototype = new (function () {
};
// Watch individual files so we can compare mtimes and restart
// on code-changes
fileUtils.watch(dir + '/config', callback);
fileUtils.watch(dir + '/lib', callback);
fileUtils.watch(dir + '/app/controllers', callback);
fileUtils.watch(dir + '/app/models', callback);
fileUtils.watch(dir + '/app/views', callback);
file.watch(dir + '/config', callback);
file.watch(dir + '/lib', callback);
file.watch(dir + '/app/controllers', callback);
file.watch(dir + '/app/models', callback);
file.watch(dir + '/app/views', callback);
};

this.init = function () {
Expand Down
4 changes: 2 additions & 2 deletions lib/cluster/master_dispatch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
try {
var compat = require('../utils/compat').compat
, metrics = compat.dependency('metrics');
var file = require('utilities').file
, metrics = file.dependency('metrics');
} catch(err) {}

var dispatch = {
Expand Down
6 changes: 3 additions & 3 deletions lib/geddy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var geddy = global.geddy || {}
, cluster = require('cluster')
, master
, worker
, utils = require('./utils')
, utils = require('utilities')
, pkg = require('../package')
, App;

Expand Down Expand Up @@ -31,15 +31,15 @@ utils.mixin(geddy, new (function () {

// Master-process, start workers
if (geddy.isMaster) {
master = require('../lib/cluster/master')
master = require('../lib/cluster/master');
m = new master.Master();
m.start(_opts);
}
// Worker-process, start up an app
else {
App = require('./app.js').App;

worker = require('../lib/cluster/worker')
worker = require('../lib/cluster/worker');
w = new worker.Worker();
geddy.worker = w;

Expand Down
19 changes: 10 additions & 9 deletions lib/init/i18n.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var i18n = require('../i18n')
, path = require('path')
, fs = require('fs')
, fileUtils = require('../utils/file')
, compat = require('../utils/compat').compat;
, utils = require('utilities')
, file = utils.file;

module.exports = new (function () {
var LOCALE_PAT = /([^\/]*).json$/;
Expand All @@ -17,21 +17,22 @@ module.exports = new (function () {
, loadLocaleData = function () {
localePaths.forEach(function (directory) {
directory = path.normalize(directory);
if (compat.existsSync(directory)) {
var files = fileUtils.readdirR(directory);
if (file.existsSync(directory)) {
var f
, files = file.readdirR(directory);
for (var i = 0; i < files.length; i++) {
file = files[i];
if (file && /.json$/.test(file)) {
f = files[i];
if (f && /.json$/.test(f)) {
// Extract the locale-name from the filename (e.g.,
// foo/bar/baz/en-us.json => 'en-us'
if ((locale = LOCALE_PAT.exec(file)) && locale[1]) {
if ((locale = LOCALE_PAT.exec(f)) && locale[1]) {
try {
data = fs.readFileSync(file).toString();
data = fs.readFileSync(f).toString();
data = JSON.parse(data);
}
catch(e) {
throw new Error('Could not parse locale-data in file: ' +
file);
f);
}
i18n.loadLocale(locale[1], data);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/model/adapters/mongo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var compat = require('../../utils/compat').compat
, mongo = compat.dependency('mongodb-wrapper');
var file = require('utilities').file
, mongo = file.dependency('mongodb-wrapper');

var Mongo = function (config) {
if (!geddy.config.db.mongo) throw "No Mongo config defined.";
Expand Down
4 changes: 2 additions & 2 deletions lib/model/adapters/riak/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var compat = require('../../../utils/compat').compat
, riak = compat.dependency('riak-js');
var file = require('utilities').file
, riak = file.dependency('riak-js');

//require('riak-js').getClient()

Expand Down
2 changes: 1 addition & 1 deletion lib/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ User = geddy.model.register('User', User);
*/

var model = {}
, utils = require('../utils')
, utils = require('utilities')
, i18n = require('../i18n');

model.datatypes = require('./datatypes.js');
Expand Down
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var EventBuffer = require('./utils/event_buffer').EventBuffer
var EventBuffer = require('utilities').EventBuffer
, EventEmitter = require('events').EventEmitter;

var Request = function (httpReq) {
Expand Down
14 changes: 7 additions & 7 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var VERSION = '0.1.0'
, childProcess = require('child_process')
, fs = require('fs')
, Log = require('../deps/log')
, utils = require('./utils')
, utils = require('utilities')
, metrics
, Parser
, optsReg
Expand All @@ -35,7 +35,7 @@ var VERSION = '0.1.0'
, restartMode = false;

try {
metrics = utils.compat.dependency('metrics')
metrics = utils.file.dependency('metrics')
} catch(err) {}

/**
Expand Down Expand Up @@ -210,11 +210,11 @@ var server = module.exports = new function () {
};
// Watch individual files so we can compare mtimes and restart
// on code-changes
utils.fileUtils.watch(dir + '/config', callback);
utils.fileUtils.watch(dir + '/lib', callback);
utils.fileUtils.watch(dir + '/app/controllers', callback);
utils.fileUtils.watch(dir + '/app/models', callback);
utils.fileUtils.watch(dir + '/app/views', callback);
utils.file.watch(dir + '/config', callback);
utils.file.watch(dir + '/lib', callback);
utils.file.watch(dir + '/app/controllers', callback);
utils.file.watch(dir + '/app/models', callback);
utils.file.watch(dir + '/app/views', callback);
}

, _initLogging = function (callback) {
Expand Down
4 changes: 2 additions & 2 deletions lib/sessions/stores/memcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Config section should look like this:
'servers' is your array of Memcache servers.
*/

var compat = require('../../utils/compat').compat
, Memcached = compat.dependency('memcached');
var file = require('utilities').file
, Memcached = file.dependency('memcached');

var Memcache = function (callback) {
this.setup(callback);
Expand Down
Loading

0 comments on commit 3a55f1c

Please sign in to comment.