Permalink
Browse files

Added generator for auth

  • Loading branch information...
1 parent ac25a59 commit 7ae4e055b0c24a2e3cb2e05e607f75c675f12dee mde committed Nov 1, 2012
Showing with 129 additions and 40 deletions.
  1. +129 −40 templates/Jakefile
View
@@ -4,8 +4,55 @@ require('../lib/geddy')
// Dependencies
var fs = require('fs')
, path = require('path')
+ , fs = require('fs')
+ , path = require('path')
, utils = require('../lib/utils')
- , Adapter = require('../lib/template/adapters');
+ , Adapter = require('../lib/template/adapters')
+ , getRouterPath
+ , addRoute;
+
+getRouterPath = function () {
+ var beginPath
+ , jsRouter = path.normalize('config/router.js')
+ , coffeeRouter = path.normalize('config/router.coffee')
+ , routerPath;
+ // Check if the router file exists
+ beginPath = path.join(process.cwd(), 'config');
+ utils.file.searchParentPath(jsRouter, function (err) {
+ if (err) {
+ var jsErr = err;
+ // If jsEnvironment wasn't found, try finding coffee variant
+ utils.file.searchParentPath(coffeeRouter, beginPath, function (err) {
+ if (err) {
+ throw jsErr;
+ } else {
+ routerPath = coffeeRouter;
+ }
+ });
+ } else {
+ routerPath = jsRouter;
+ }
+ });
+ return routerPath;
+};
+
+addRoute = function (routerPath, newRoute) {
+ var text = fs.readFileSync(routerPath, 'utf8')
+ , routerArr;
+ // Don't add the same route over and over
+ if (text.indexOf(newRoute) == -1) {
+ // Add the new resource route just above the export
+ routerArr = text.split('exports.router');
+ routerArr[0] += newRoute + '\n';
+
+ text = routerArr.join('exports.router');
+ fs.writeFileSync(routerPath, text, 'utf8');
+ return true;
+ }
+ else {
+ return false;
+ }
+};
namespace('env', function () {
task('init', function (environment) {
@@ -462,37 +509,11 @@ namespace('gen', function () {
options = options || {};
var names = utils.string.getInflections(name)
+ , routerPath = getRouterPath()
, routeType = options.bare ? 'Bare' : 'Resource'
- , jsRouter = path.normalize('config/router.js')
- , coffeeRouter = path.normalize('config/router.coffee')
- , routerPath
- , routerArr
- , text
- , splitText
- , newRoute
- , beginPath;
-
- // Check if the router file exists
- beginPath = path.join(process.cwd(), 'config');
- utils.file.searchParentPath(jsRouter, function (err) {
- if (err) {
- var jsErr = err;
- // If jsEnvironment wasn't found, try finding coffee variant
- utils.file.searchParentPath(coffeeRouter, beginPath, function (err) {
- if (err) {
- throw jsErr;
- } else {
- routerPath = coffeeRouter;
- }
- });
- } else {
- routerPath = jsRouter;
- }
- });
+ , newRoute;
if (routerPath) {
- text = fs.readFileSync(routerPath, 'utf8');
-
if (routerPath.match('.coffee')) {
if (options.bare) {
newRoute = 'router.match(\'/' + names.filename.plural +
@@ -511,26 +532,19 @@ namespace('gen', function () {
}
}
- // Don't add the same route over and over
- if (text.indexOf(newRoute) == -1) {
- // Add the new resource route just above the export
- routerArr = text.split('exports.router');
- routerArr[0] += newRoute + '\n';
-
- text = routerArr.join('exports.router');
- fs.writeFileSync(routerPath, text, 'utf8');
-
+ if (addRoute(routerPath, newRoute)) {
console.log('[Added] ' + routeType + ' ' + names.filename.plural +
' route added to ' + routerPath);
}
else {
console.log(routeType + ' ' + names.filename.plural + ' route already defined in ' +
routerPath);
}
-
- } else {
+ }
+ else {
console.log('There is no router file to add routes too');
}
+
});
task('views', function (name, options) {
@@ -727,3 +741,78 @@ namespace('gen', function () {
});
});
+
+namespace('auth', function () {
+
+ task('init', {async: true}, function () {
+ var go = false
+ , readline = require('readline')
+ , rl = readline.createInterface({
+ input: process.stdin
+ , output: process.stdout
+ })
+ , toBase = process.cwd()
+ , fromBase = path.join(toBase, 'node_modules', 'geddy-passport');
+
+ rl.setPrompt('This command should probably only be run in a fresh Geddy app.\n' +
+ 'Do you wish to continue? (yes|no)\n');
+ rl.prompt();
+
+ rl.addListener('line', function (line) {
+ if (line == 'yes') {
+ go = true;
+ }
+ rl.close();
+ });
+
+ rl.addListener('close', function () {
+ if (go) {
+ console.log('Installing geddy-passport ...');
+ jake.exec('npm install geddy-passport', function () {
+ var list = fs.readFileSync(path.join(fromBase, 'file_list.json'))
+ , routerPath = getRouterPath()
+ , newRoute;
+
+ list = JSON.parse(list.toString());
+ list.forEach(function (item) {
+ var from = path.join(fromBase, item)
+ , to = path.dirname(path.join(toBase, item));
+ jake.mkdirP(to);
+ jake.cpR(from, to, {silent: true});
+ });
+
+ jake.rmRf(path.join('node_modules', 'geddy-passport'));
+
+ if (routerPath) {
+ if (routerPath.match('.coffee')) {
+ throw new Error(
+ 'Geddy passport integration does not support CoffeeScript.');
+ }
+ else {
+ newRoute = "router.get('/login').to('Main.login');\n" +
+ "router.get('/logout').to('Main.logout');\n" +
+ "router.post('/auth/local').to('Auth.local');\n" +
+ "router.get('/auth/twitter').to('Auth.twitter');\n" +
+ "router.get('/auth/twitter/callback').to('Auth.twitterCallback');\n" +
+ "router.get('/auth/facebook').to('Auth.facebook');\n";
+
+ if (addRoute(routerPath, newRoute)) {
+ console.log('Added autnehtication routes.');
+ }
+ else {
+ console.log('Authentication routes already defined in ' +
+ routerPath);
+ }
+ }
+ }
+ else {
+ console.log('There is no router file to add routes too');
+ }
+
+ complete();
+ }, {printStdout: true});
+ }
+ });
+ });
+
+});

0 comments on commit 7ae4e05

Please sign in to comment.