-
Notifications
You must be signed in to change notification settings - Fork 240
/
auth.jake
199 lines (170 loc) · 6.84 KB
/
auth.jake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var path = require('path')
, cwd = process.cwd()
, geddyPassport = 'geddy-passport@0.1.x'
, helpers = require('./helpers')
, mixinJSONData = helpers.mixinJSONData
, getRouterPath = helpers.getRouterPath
, getRoutes = helpers.getRoutes
, addRoute = helpers.addRoute;
namespace('auth', function () {
task('update', {async: true}, function () {
var updatePath = path.join('geddy-passport', 'app', 'helpers', 'passport')
, from
, to;
console.log('Updating passport helpers from', geddyPassport);
jake.exec('npm uninstall ' + geddyPassport +
' && npm install ' + geddyPassport, function () {
from = path.join(cwd, 'node_modules', updatePath);
to = path.join(cwd, 'app', 'helpers');
jake.rmRf(path.join(cwd, 'passport'), {silent: true});
jake.cpR(from, to, {silent: true});
console.log("\nCleaning up...");
jake.exec('npm uninstall ' + geddyPassport, function () {
complete();
});
}, {printStdout: true});
});
task('init', {async: true}, function (engine) {
var env = process.env
, readline = require('readline')
, fromBase = env.srcDir ||
path.join(cwd, 'node_modules', 'geddy-passport')
, install = false
, rl, installPackages, passportCopy;
if (!engine || engine == 'default') {
engine = 'ejs';
}
// Create and start the prompt
rl = readline.createInterface({
input: process.stdin
, output: process.stdout
});
rl.setPrompt("WARNING: This command will create/overwrite files in your app.\n" +
"Do you wish to continue?(yes|no)\n\n");
rl.prompt();
rl.on('line', function (line) {
if (line === 'yes' || line === 'y') {
install = true;
}
rl.close();
});
rl.on('close', function () {
if (!install) {
return;
}
if (env.srcDir) {
console.log('Installing from ' + fromBase);
installPackages();
}
else {
console.log('Installing', geddyPassport);
jake.exec('npm uninstall ' + geddyPassport +
' && npm install ' + geddyPassport, installPackages, {printStdout: true});
}
});
// Gets the package versions from geddy-passport's package.json
// and installs them, then calls passportCopy
installPackages = function () {
var deps = require(path.join(fromBase, 'package')).dependencies
, packages = ''
, k;
mixinJSONData(path.join(cwd, 'package.json'), {dependencies: deps});
for (k in deps) {
packages += k + '@' + deps[k] + ' ';
}
console.log("\nInstalling", packages);
jake.exec('npm uninstall ' + packages +
' && npm install ' + packages, passportCopy, {printStdout: true});
};
// Copy the contents of geddy-passport into the application
passportCopy = function () {
var list = require(path.join(fromBase, 'file_list'))
, routerPath = getRouterPath()
, newRoute
, from
, to
, p
, i
, engineExt = {
ejs: '.ejs'
, jade: '.jade'
, handlebars: '.hbs'
, mustache: '.ms'
, swig: '.swig'
};
// Copy files to the application
for (i = 0; i < list.length; i++) {
item = list[i];
from = path.join(fromBase, item);
to = path.dirname(path.join(cwd, item));
if (item.match('app/views')) {
from = from.replace('app' + path.sep + 'views', 'app' + path.sep + 'views_' + engine);
from = from.replace(/\[\.ext\]$/, engineExt[engine]);
to = to.replace(/\[\.ext\]$/, engineExt[engine]);
}
jake.mkdirP(to);
console.log('Creating file:', path.join(to, path.basename(from)));
// Delete any existing interferring templates
if (item.match('app/views')) {
['.jade', '.ejs', '.ms', '.mustache', '.hbs', '.handlebars', '.swig'].forEach(function (ext) {
p = path.basename(item, path.extname(item)) + ext;
jake.rmRf(path.join(to, p), {silent: true});
});
}
jake.cpR(from, to, {silent: true});
}
// Add new routes to router
// FIXME: This should be done programmatically
// with some sort of enum/loop
if (routerPath) {
// CoffeeScript routes
if (routerPath.match('.coffee')) {
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" +
"router.get('/auth/facebook/callback').to 'Auth.facebookCallback'\n" +
"router.get('/auth/yammer').to 'Auth.yammer'\n" +
"router.get('/auth/yammer/callback').to 'Auth.yammerCallback'\n" +
"router.get('/auth/google').to 'Auth.google'\n" +
"router.get('/auth/google/callback').to 'Auth.googleCallback'\n" +
"router.resource 'users'";
} 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" +
"router.get('/auth/facebook/callback').to('Auth.facebookCallback');\n" +
"router.get('/auth/yammer').to('Auth.yammer');\n" +
"router.get('/auth/yammer/callback').to('Auth.yammerCallback');\n" +
"router.get('/auth/google').to('Auth.google');\n" +
"router.get('/auth/google/callback').to('Auth.googleCallback');\n" +
"router.resource('users');";
}
if (addRoute(routerPath, "\n" + newRoute)) {
console.log('\nAdded authentication routes:\n' + newRoute);
} else {
console.log('\nAuthentication routes already defined in', routerPath);
}
}
else {
console.log('\nThere is no router file to add routes too.');
}
// Create secrets and copy the secrets template
console.log("\nCreating secrets.json file with stubbed-out Passport config.");
jake.cpR(path.join(fromBase, 'config', 'secrets.json.template'),
path.join(cwd, 'config', 'secrets.json'), {silent: true});
jake.Task['gen:secret'].invoke();
// Remove geddy-passport as it isn't needed anymore
console.log('\nCleaning up...');
jake.exec('npm uninstall geddy-passport', function () {
console.log('Please set up your Passport config in config/secrets.json');
complete();
});
};
});
});