From f366dcbeae43c58cc1c68e9450363289c1c13282 Mon Sep 17 00:00:00 2001 From: DallonF Date: Thu, 14 Jun 2012 11:29:22 -0700 Subject: [PATCH] Updated folder structure --- bin/dpd | 73 ++++++++++++++++++++------------------ lib/config-loader.js | 4 +-- lib/resources.js | 2 +- lib/server.js | 4 +-- lib/type-loader.js | 30 ++++++++-------- lib/util/mongod.js | 67 +++++++++++++++++----------------- test/config-loader.unit.js | 4 +-- 7 files changed, 95 insertions(+), 89 deletions(-) diff --git a/bin/dpd b/bin/dpd index 2089665..ae5a774 100755 --- a/bin/dpd +++ b/bin/dpd @@ -5,57 +5,62 @@ */ var program = require('commander') - , deployd = require('../') - , repl = require('../lib/client/repl') - , shelljs = require('shelljs/global') - , mongod = require('../lib/util/mongod'); + , deployd = require('../') + , repl = require('../lib/client/repl') + , shelljs = require('shelljs/global') + , mongod = require('../lib/util/mongod'); /** * Get the version number from the package.json */ program - .version(require('../package').version) - .option('-m, --mongod [path]', 'path to mongod executable (defaults to `mongod`)') + .version(require('../package').version) + .option('-m, --mongod [path]', 'path to mongod executable (defaults to `mongod`)') /** * Commands */ program - .command('create [project-name]') - .description('\tcreate a project in a new directory\n\teg. `dpd create my-app`') - .action(function(name) { - var name = name || 'my-deployd-app'; - mkdir('-p', name); - mkdir('-p', name + '/resources'); - mkdir('-p', name + '/public'); - mkdir('-p', name + '/.dpd/data'); - mkdir('-p', name + '/.dpd/pids'); - ("{}").to(name + '/.dpd/resources.json'); - ("").to(name + '/.dpd/pids/mongod'); - console.info('To start your app:'); - console.info('\t$ cd', name); - console.info('\t$ dpd'); - }); + .command('create [project-name]') + .description('\tcreate a project in a new directory\n\teg. `dpd create my-app`') + .action(function(name) { + var name = name || 'my-deployd-app'; + mkdir('-p', name); + mkdir('-p', name + '/public'); + mkdir('-p', name + '/data'); + mkdir('-p', name + '/.dpd'); + mkdir('-p', name + '/.dpd/pids'); + ('').to(name + '/.dpd/pids/mongod'); + ("{}").to(name + '/app.dpd'); + console.info('To start your app:'); + console.info('\t$ cd', name); + console.info('\t$ dpd'); + }); function start(port) { - mongod.restart(program.mongod || 'mongod', function() { - var dpd = deployd({port: port, db: {host: '127.0.0.1', port: 27017, name: '-deployd'}}); - dpd.listen(); - dpd.on('listening', function () { - console.info('listening on port', port); - repl(dpd); - }); - }); + if (test('-f', 'app.dpd')) { + mongod.restart(program.mongod || 'mongod', function() { + var dpd = deployd({port: port, db: {host: '127.0.0.1', port: 27017, name: '-deployd'}}); + dpd.listen(); + dpd.on('listening', function () { + console.info('listening on port', port); + repl(dpd); + }); + }); + } else { + console.info("This directory does not contain a Deployd app!"); + console.info("Use \"dpd create \" to create a new app"); + } } program - .command('*') - .description('\t[default] start the server in the current project in development mode\n' + - '\twith an interactive shell/repl for interacting with the running server\n' + - '\teg. `dpd` (start at default port 2403) or `dpd 3000` (port 3000)') - .action(start); + .command('*') + .description('\t[default] start the server in the current project in development mode\n' + + '\twith an interactive shell/repl for interacting with the running server\n' + + '\teg. `dpd` (start at default port 2403) or `dpd 3000` (port 3000)') + .action(start); /** diff --git a/lib/config-loader.js b/lib/config-loader.js index 1d83b16..386a573 100644 --- a/lib/config-loader.js +++ b/lib/config-loader.js @@ -10,7 +10,7 @@ var fs = require('fs') * @param {Function} callback */ module.exports.loadConfig = function(basepath, fn) { - var resourcesPath = path.join(basepath, '/resources.json'); + var resourcesPath = path.join(basepath, '/app.dpd'); fs.readFile(resourcesPath, 'utf-8', function(err, data) { if (err) { return fn(err); } @@ -36,7 +36,7 @@ module.exports.loadConfig = function(basepath, fn) { * @param {Function} callback */ module.exports.saveConfig = function(resources, basepath, fn) { - var resourcesPath = path.join(basepath, '/resources.json') + var resourcesPath = path.join(basepath, '/app.dpd') , json = JSON.stringify(resources); fs.writeFile(resourcesPath, json, 'utf-8', function(err) { diff --git a/lib/resources.js b/lib/resources.js index cd93123..866529f 100644 --- a/lib/resources.js +++ b/lib/resources.js @@ -45,7 +45,7 @@ var build = exports.build = function (resourceConfig, server, fn) { } function InternalResources(settings, server) { - settings.configPath = settings.configPath || './.dpd/'; + settings.configPath = settings.configPath || './'; Resource.apply(this, arguments); this.store = server && server.createStore && server.createStore('resources'); this.server = server; diff --git a/lib/server.js b/lib/server.js index 185163b..343f51e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -95,7 +95,7 @@ function Server(options) { req.session = session; function route() { - config.loadConfig('./.dpd/', function(err, resourceConfig) { + config.loadConfig('./', function(err, resourceConfig) { resources.build(resourceConfig, server, function(err, resourcesInstances) { var router = new Router(resourcesInstances, server); server.router = router; @@ -135,7 +135,7 @@ util.inherits(Server, http.Server); Server.prototype.listen = function(port, host) { var server = this; - config.loadConfig('./.dpd/', function(err, resourceConfig) { + config.loadConfig('./', function(err, resourceConfig) { resources.build(resourceConfig, server, function(err, resourcesInstances) { server.resources = resourcesInstances; http.Server.prototype.listen.call(server, port || server.options.port, host || server.options.host); diff --git a/lib/type-loader.js b/lib/type-loader.js index 4cf1122..d82a8d4 100644 --- a/lib/type-loader.js +++ b/lib/type-loader.js @@ -12,24 +12,24 @@ module.exports = function loadTypes(basepath, fn) { var path = basepath || './resources'; // read local project resources - fs.readdir(path, function(err, dir) { - dir && dir.forEach(function(file) { + // fs.readdir(path, function(err, dir) { + // dir && dir.forEach(function(file) { + // if(file.indexOf('.js') == file.length - 3) { + // var c = require(require('path').resolve(path) + '/' + file); + // types[c.name] = c; + // } + // }); + + // read default lib resources + fs.readdir(__dirname + '/resources', function(err, dir) { + dir.forEach(function(file) { if(file.indexOf('.js') == file.length - 3) { - var c = require(require('path').resolve(path) + '/' + file); - types[c.name] = c; + var c = require(path + '/' + file); + defaults[c.name] = c; } }); - // read default lib resources - fs.readdir(__dirname + '/resources', function(err, dir) { - dir.forEach(function(file) { - if(file.indexOf('.js') == file.length - 3) { - var c = require(path + '/' + file); - defaults[c.name] = c; - } - }); - - fn(defaults, types); - }); + fn(defaults, types); }); + // }); } \ No newline at end of file diff --git a/lib/util/mongod.js b/lib/util/mongod.js index babdbdb..914893b 100644 --- a/lib/util/mongod.js +++ b/lib/util/mongod.js @@ -1,47 +1,48 @@ var fs = require('fs') - , spawn = require('child_process').spawn - , debug = require('debug')('mongod'); + , spawn = require('child_process').spawn + , debug = require('debug')('mongod'); /** * Utility for restarting the current apps mongod instance. */ exports.restart = function (mongod, fn) { - var pid; + var pid; - debug('starting %s', mongod); + debug('starting %s', mongod); - try { - pid = JSON.parse(fs.readFileSync('./.dpd/pids/mongod')); - } catch(e) {} + try { + pid = JSON.parse(fs.readFileSync('./.dpd/pids/mongod')); - if(pid) { - debug('pid %s', pid); - process.kill(pid); - } else { - debug('no pid found'); - } + if(pid) { + debug('pid %s', pid); + process.kill(pid); + } else { + debug('no pid found'); + } + } catch(e) {} + + var proc = spawn(mongod, ['--dbpath', './data', '--pidfilepath', './.dpd/pids/mongod'], {title: 'FOOBAR'}) + , buf = ''; + proc.stdout.on('data', function(data) { + buf += data; + if(~buf.indexOf('waiting for connections on port')) { + proc.emit('listening'); + } + debug(data); + }); - var proc = spawn(mongod, ['--dbpath', './.dpd/data', '--pidfilepath', './.dpd/pids/mongod'], {title: 'FOOBAR'}) - , buf = ''; - proc.stdout.on('data', function(data) { - buf += data; - if(~buf.indexOf('waiting for connections on port')) { - proc.emit('listening'); - } - debug(data); - }); + function kill(e) { + if(e) console.error(e); + debug('killing mongod'); + fs.writeFileSync('./.dpd/pids/mongod', ''); + proc.kill(); + process.exit(0); + } - function kill(e) { - if(e) console.error(e); - debug('killing mongod'); - proc.kill(); - process.exit(0); - } + // callback + proc.once('listening', fn); - // callback - proc.once('listening', fn); - - process.on('exit', kill); - process.on('uncaughtException', kill); + process.on('exit', kill); + process.on('uncaughtException', kill); } \ No newline at end of file diff --git a/test/config-loader.unit.js b/test/config-loader.unit.js index dea2e0a..cf77570 100644 --- a/test/config-loader.unit.js +++ b/test/config-loader.unit.js @@ -25,7 +25,7 @@ describe('config-loader', function() { , 'test': 'value' }; - fs.writeFileSync(path.join(basepath, '/resources.json'), JSON.stringify({'123': resource1, '456': resource2})); + fs.writeFileSync(path.join(basepath, '/app.dpd'), JSON.stringify({'123': resource1, '456': resource2})); configLoader.loadConfig(basepath, function(err, resources) { expect(Object.keys(resources)).to.have.length(2); @@ -55,7 +55,7 @@ describe('config-loader', function() { }; configLoader.saveConfig({'123': resource1, '456': resource2}, basepath, function(err) { - var resourcePath = path.join(basepath, '/resources.json'); + var resourcePath = path.join(basepath, '/app.dpd'); var resources = JSON.parse(fs.readFileSync(resourcePath));