Skip to content

Commit

Permalink
Added generator for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Nov 1, 2012
1 parent ac25a59 commit 7ae4e05
Showing 1 changed file with 129 additions and 40 deletions.
169 changes: 129 additions & 40 deletions templates/Jakefile
Expand Up @@ -3,9 +3,56 @@ require('../lib/geddy')


// Dependencies // Dependencies
var fs = require('fs') var fs = require('fs')
, path = require('path')
, fs = require('fs')
, path = require('path') , path = require('path')
, utils = require('../lib/utils') , 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 () { namespace('env', function () {
task('init', function (environment) { task('init', function (environment) {
Expand Down Expand Up @@ -462,37 +509,11 @@ namespace('gen', function () {
options = options || {}; options = options || {};


var names = utils.string.getInflections(name) var names = utils.string.getInflections(name)
, routerPath = getRouterPath()
, routeType = options.bare ? 'Bare' : 'Resource' , routeType = options.bare ? 'Bare' : 'Resource'
, jsRouter = path.normalize('config/router.js') , newRoute;
, 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;
}
});


if (routerPath) { if (routerPath) {
text = fs.readFileSync(routerPath, 'utf8');

if (routerPath.match('.coffee')) { if (routerPath.match('.coffee')) {
if (options.bare) { if (options.bare) {
newRoute = 'router.match(\'/' + names.filename.plural + newRoute = 'router.match(\'/' + names.filename.plural +
Expand All @@ -511,26 +532,19 @@ namespace('gen', function () {
} }
} }


// Don't add the same route over and over if (addRoute(routerPath, newRoute)) {
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');

console.log('[Added] ' + routeType + ' ' + names.filename.plural + console.log('[Added] ' + routeType + ' ' + names.filename.plural +
' route added to ' + routerPath); ' route added to ' + routerPath);
} }
else { else {
console.log(routeType + ' ' + names.filename.plural + ' route already defined in ' + console.log(routeType + ' ' + names.filename.plural + ' route already defined in ' +
routerPath); routerPath);
} }

}
} else { else {
console.log('There is no router file to add routes too'); console.log('There is no router file to add routes too');
} }

}); });


task('views', function (name, options) { task('views', function (name, options) {
Expand Down Expand Up @@ -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.