freeCodeCamp/setup.js

631 lines
16 KiB
JavaScript
Raw Normal View History

var fs = require('fs');
2014-05-13 06:59:46 +00:00
var os = require('os');
var blessed = require('blessed');
var multiline = require('multiline');
if (os.platform() === 'win32') {
2014-10-18 17:38:55 +00:00
console.log('**************************************************************');
console.log('Hackathon Starter Generator has been disabled on Windows until');
console.log('https://github.com/chjj/blessed is fixed or until I find a');
console.log('better CLI module.');
2014-10-18 17:38:55 +00:00
console.log('**************************************************************');
process.exit();
}
var screen = blessed.screen({
2014-05-11 15:11:17 +00:00
autoPadding: true
});
2014-05-13 05:13:27 +00:00
screen.key('q', function() {
process.exit(0);
});
2014-05-11 15:11:17 +00:00
var home = blessed.list({
parent: screen,
padding: { top: 2 },
mouse: true,
keys: true,
fg: 'white',
bg: 'blue',
selectedFg: 'blue',
selectedBg: 'white',
items: [
2014-05-13 07:57:23 +00:00
'» REMOVE AUTHENTICATION PROVIDER',
'» CHANGE EMAIL SERVICE',
'» ADD NODE.JS CLUSTER SUPPORT',
'» EXIT'
]
});
2014-05-13 22:39:25 +00:00
var homeTitle = blessed.text({
parent: screen,
align: 'center',
fg: 'blue',
bg: 'white',
content: 'Hackathon Starter (c) 2014'
});
var footer = blessed.text({
parent: screen,
bottom: 0,
fg: 'white',
bg: 'blue',
tags: true,
content: ' {cyan-fg}<Up/Down>{/cyan-fg} moves | {cyan-fg}<Enter>{/cyan-fg} selects | {cyan-fg}<q>{/cyan-fg} exits'
});
var inner = blessed.form({
2014-05-11 16:24:16 +00:00
top: 'center',
left: 'center',
mouse: true,
keys: true,
2014-05-11 17:15:15 +00:00
width: 33,
height: 10,
2014-05-11 16:24:16 +00:00
border: {
type: 'line',
fg: 'white',
bg: 'red'
},
fg: 'white',
bg: 'red'
});
var success = blessed.box({
top: 'center',
left: 'center',
mouse: true,
keys: true,
tags: true,
width: '50%',
height: '40%',
border: {
type: 'line',
fg: 'white',
bg: 'green'
},
fg: 'white',
bg: 'green',
padding: 1
});
success.on('keypress', function() {
home.focus();
home.remove(success);
});
2014-05-11 17:15:15 +00:00
var clusterText = blessed.text({
top: 'top',
bg: 'red',
fg: 'white',
tags: true,
content: 'Take advantage of multi-core systems using built-in {underline}cluster{/underline} module.'
});
var enable = blessed.button({
parent: inner,
bottom: 0,
mouse: true,
shrink: true,
name: 'enable',
content: ' ENABLE ',
border: {
type: 'line',
fg: 'white',
bg: 'red'
},
style: {
fg: 'white',
bg: 'red',
focus: {
fg: 'red',
bg: 'white'
2014-05-11 17:15:15 +00:00
}
}
});
2014-05-11 17:15:15 +00:00
var disable = blessed.button({
parent: inner,
bottom: 0,
left: 10,
mouse: true,
shrink: true,
name: 'disable',
content: ' DISABLE ',
border: {
type: 'line',
fg: 'white',
bg: 'red'
},
style: {
fg: 'white',
bg: 'red',
focus: {
fg: 'red',
bg: 'white'
2014-05-11 17:15:15 +00:00
}
}
});
var cancel = blessed.button({
parent: inner,
bottom: 0,
left: 21,
mouse: true,
shrink: true,
name: 'cancel',
content: ' CANCEL ',
border: {
type: 'line',
fg: 'white',
bg: 'red'
},
style: {
fg: 'white',
bg: 'red',
focus: {
fg: 'red',
bg: 'white'
2014-05-11 17:15:15 +00:00
}
}
});
2014-05-11 16:24:16 +00:00
cancel.on('press', function() {
home.focus();
home.remove(inner);
screen.render();
});
2014-05-13 07:57:23 +00:00
var authForm = blessed.form({
2014-05-11 15:11:17 +00:00
mouse: true,
keys: true,
fg: 'white',
bg: 'blue',
padding: { left: 1, right: 1 }
});
2014-05-13 08:40:28 +00:00
authForm.on('submit', function() {
2014-05-13 07:57:23 +00:00
var passportConfig = fs.readFileSync('config/passport.js').toString().split(os.EOL);
var loginTemplate = fs.readFileSync('views/account/login.jade').toString().split(os.EOL);
var profileTemplate = fs.readFileSync('views/account/profile.jade').toString().split(os.EOL);
var userModel = fs.readFileSync('models/User.js').toString().split(os.EOL);
var app = fs.readFileSync('app.js').toString().split(os.EOL);
var secrets = fs.readFileSync('config/secrets.js').toString().split(os.EOL);
2014-05-13 08:40:28 +00:00
var index = passportConfig.indexOf("var FacebookStrategy = require('passport-facebook').Strategy;");
if (facebookCheckbox.checked && index !== -1) {
2014-05-13 07:57:23 +00:00
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with Facebook.');
passportConfig.splice(index, 47);
fs.writeFileSync('config/passport.js', passportConfig.join(os.EOL));
index = loginTemplate.indexOf(" a.btn.btn-block.btn-facebook.btn-social(href='/auth/facebook')");
loginTemplate.splice(index, 3);
fs.writeFileSync('views/account/login.jade', loginTemplate.join(os.EOL));
index = profileTemplate.indexOf(" if user.facebook");
profileTemplate.splice(index - 1, 5);
fs.writeFileSync('views/account/profile.jade', profileTemplate.join(os.EOL));
index = userModel.indexOf(' facebook: String,');
userModel.splice(index, 1);
fs.writeFileSync('models/User.js', userModel.join(os.EOL));
index = app.indexOf("app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));");
app.splice(index, 4);
fs.writeFileSync('app.js', app.join(os.EOL));
}
index = passportConfig.indexOf("var GitHubStrategy = require('passport-github').Strategy;");
2014-05-13 08:40:28 +00:00
if (githubCheckbox.checked && index !== -1) {
console.log(index);
2014-05-13 08:40:28 +00:00
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with GitHub.');
passportConfig.splice(index, 48);
fs.writeFileSync('config/passport.js', passportConfig.join(os.EOL));
index = loginTemplate.indexOf(" a.btn.btn-block.btn-github.btn-social(href='/auth/github')");
loginTemplate.splice(index, 3);
fs.writeFileSync('views/account/login.jade', loginTemplate.join(os.EOL));
index = profileTemplate.indexOf(' if user.github');
profileTemplate.splice(index - 1, 5);
fs.writeFileSync('views/account/profile.jade', profileTemplate.join(os.EOL));
index = userModel.indexOf(' github: String,');
userModel.splice(index, 1);
fs.writeFileSync('models/User.js', userModel.join(os.EOL));
index = app.indexOf("app.get('/auth/github', passport.authenticate('github'));");
app.splice(index, 4);
fs.writeFileSync('app.js', app.join(os.EOL));
}
index = passportConfig.indexOf("var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;");
2014-05-13 08:40:28 +00:00
if (googleCheckbox.checked && index !== -1) {
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with Google.');
passportConfig.splice(index, 46);
fs.writeFileSync('config/passport.js', passportConfig.join(os.EOL));
index = loginTemplate.indexOf(" a.btn.btn-block.btn-google-plus.btn-social(href='/auth/google')");
loginTemplate.splice(index, 3);
fs.writeFileSync('views/account/login.jade', loginTemplate.join(os.EOL));
index = profileTemplate.indexOf(' if user.google');
profileTemplate.splice(index - 1, 5);
fs.writeFileSync('views/account/profile.jade', profileTemplate.join(os.EOL));
index = userModel.indexOf(' google: String,');
userModel.splice(index, 1);
fs.writeFileSync('models/User.js', userModel.join(os.EOL));
index = app.indexOf("app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));");
app.splice(index, 4);
fs.writeFileSync('app.js', app.join(os.EOL));
}
index = passportConfig.indexOf("var TwitterStrategy = require('passport-twitter').Strategy;");
2014-05-13 08:40:28 +00:00
if (twitterCheckbox.checked && index !== -1) {
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with Twitter.');
passportConfig.splice(index, 43);
fs.writeFileSync('config/passport.js', passportConfig.join(os.EOL));
index = loginTemplate.indexOf(" a.btn.btn-block.btn-twitter.btn-social(href='/auth/twitter')");
loginTemplate.splice(index, 3);
fs.writeFileSync('views/account/login.jade', loginTemplate.join(os.EOL));
index = profileTemplate.indexOf(' if user.twitter');
profileTemplate.splice(index - 1, 5);
fs.writeFileSync('views/account/profile.jade', profileTemplate.join(os.EOL));
index = userModel.indexOf(' twitter: String,');
userModel.splice(index, 1);
fs.writeFileSync('models/User.js', userModel.join(os.EOL));
index = app.indexOf("app.get('/auth/twitter', passport.authenticate('twitter'));");
app.splice(index, 4);
fs.writeFileSync('app.js', app.join(os.EOL));
}
index = passportConfig.indexOf("var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;");
2014-05-13 08:40:28 +00:00
if (linkedinCheckbox.checked && index !== -1) {
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with LinkedIn.');
passportConfig.splice(index, 47);
2014-05-13 08:40:28 +00:00
fs.writeFileSync('config/passport.js', passportConfig.join(os.EOL));
index = loginTemplate.indexOf(" a.btn.btn-block.btn-linkedin.btn-social(href='/auth/linkedin')");
loginTemplate.splice(index, 3);
fs.writeFileSync('views/account/login.jade', loginTemplate.join(os.EOL));
index = profileTemplate.indexOf(' if user.linkedin');
profileTemplate.splice(index - 1, 5);
fs.writeFileSync('views/account/profile.jade', profileTemplate.join(os.EOL));
index = userModel.indexOf(' linkedin: String,');
userModel.splice(index, 1);
fs.writeFileSync('models/User.js', userModel.join(os.EOL));
index = app.indexOf("app.get('/auth/linkedin', passport.authenticate('linkedin', { state: 'SOME STATE' }));");
app.splice(index, 4);
fs.writeFileSync('app.js', app.join(os.EOL));
}
index = passportConfig.indexOf("var InstagramStrategy = require('passport-instagram').Strategy;");
2014-05-13 08:40:28 +00:00
if (instagramCheckbox.checked && index !== -1) {
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with Instagram.');
passportConfig.splice(index, 43);
2014-05-13 08:40:28 +00:00
fs.writeFileSync('config/passport.js', passportConfig.join(os.EOL));
index = loginTemplate.indexOf(" a.btn.btn-block.btn-instagram.btn-social(href='/auth/instagram')");
loginTemplate.splice(index, 3);
fs.writeFileSync('views/account/login.jade', loginTemplate.join(os.EOL));
index = profileTemplate.indexOf(' if user.instagram');
profileTemplate.splice(index - 1, 5);
fs.writeFileSync('views/account/profile.jade', profileTemplate.join(os.EOL));
index = app.indexOf("app.get('/auth/instagram', passport.authenticate('instagram'));");
app.splice(index, 4);
fs.writeFileSync('app.js', app.join(os.EOL));
userModel.splice(index, 1);
fs.writeFileSync('models/User.js', userModel.join(os.EOL));
}
2014-05-13 07:57:23 +00:00
home.remove(authForm);
home.append(success);
2014-05-13 08:40:28 +00:00
success.setContent('Selected authentication providers have been removed from passportConfig.js, User.js, app.js, login.jade and profile.jade!');
2014-05-13 07:57:23 +00:00
success.focus();
screen.render();
});
2014-05-11 15:11:17 +00:00
var authText = blessed.text({
2014-05-13 07:57:23 +00:00
parent: authForm,
content: 'Selecting a checkbox removes an authentication provider. If authentication provider is already removed, no action will be taken.',
2014-05-11 15:11:17 +00:00
padding: 1,
bg: 'magenta',
fg: 'white'
});
var facebookCheckbox = blessed.checkbox({
2014-05-13 07:57:23 +00:00
parent: authForm,
2014-05-11 15:11:17 +00:00
top: 6,
mouse: true,
fg: 'white',
bg: 'blue',
content: 'Facebook'
});
var githubCheckbox = blessed.checkbox({
2014-05-13 07:57:23 +00:00
parent: authForm,
2014-05-11 15:11:17 +00:00
top: 7,
mouse: true,
fg: 'white',
bg: 'blue',
content: 'GitHub'
});
var googleCheckbox = blessed.checkbox({
2014-05-13 07:57:23 +00:00
parent: authForm,
2014-05-11 15:11:17 +00:00
top: 8,
mouse: true,
fg: 'white',
bg: 'blue',
content: 'Google'
});
var twitterCheckbox = blessed.checkbox({
2014-05-13 07:57:23 +00:00
parent: authForm,
2014-05-11 15:11:17 +00:00
top: 9,
mouse: true,
fg: 'white',
bg: 'blue',
content: 'Twitter'
});
var linkedinCheckbox = blessed.checkbox({
2014-05-13 07:57:23 +00:00
parent: authForm,
2014-05-11 15:11:17 +00:00
top: 10,
mouse: true,
fg: 'white',
bg: 'blue',
content: 'LinkedIn'
});
var instagramCheckbox = blessed.checkbox({
2014-05-13 07:57:23 +00:00
parent: authForm,
2014-05-11 15:11:17 +00:00
top: 11,
mouse: true,
fg: 'white',
bg: 'blue',
content: 'Instagram'
});
var authSubmit = blessed.button({
2014-05-13 07:57:23 +00:00
parent: authForm,
2014-05-11 15:11:17 +00:00
top: 13,
mouse: true,
shrink: true,
name: 'submit',
2014-05-11 15:11:17 +00:00
content: ' SUBMIT ',
style: {
fg: 'blue',
bg: 'white',
focus: {
fg: 'white',
bg: 'red'
}
}
});
2014-05-13 07:57:23 +00:00
authSubmit.on('press', function() {
authForm.submit();
});
2014-05-11 15:11:17 +00:00
var authCancel = blessed.button({
2014-05-13 07:57:23 +00:00
parent: authForm,
2014-05-11 15:11:17 +00:00
top: 13,
left: 9,
mouse: true,
shrink: true,
name: 'cancel',
content: ' CANCEL ',
style: {
fg: 'blue',
bg: 'white',
focus: {
fg: 'white',
bg: 'red'
}
}
});
authCancel.on('press', function() {
home.focus();
2014-05-13 07:57:23 +00:00
home.remove(authForm);
screen.render();
});
2014-05-13 06:59:46 +00:00
var emailForm = blessed.form({
2014-05-11 16:24:16 +00:00
mouse: true,
keys: true,
fg: 'white',
2014-05-11 16:24:16 +00:00
bg: 'blue',
padding: { left: 1, right: 1 }
});
2014-05-13 08:40:28 +00:00
emailForm.on('submit', function() {
2014-05-13 06:59:46 +00:00
var contactCtrl = fs.readFileSync('controllers/contact.js').toString().split(os.EOL);
var userCtrl = fs.readFileSync('controllers/user.js').toString().split(os.EOL);
var choice = null;
if (sendgridRadio.checked) {
choice = 'SendGrid';
} else if (mailgunRadio.checked) {
choice = 'Mailgun';
} else if (mandrillRadio.checked) {
choice = 'Mandrill';
}
2014-07-15 21:25:44 +00:00
var index = contactCtrl.indexOf('var transporter = nodemailer.createTransport({');
2014-05-13 06:59:46 +00:00
contactCtrl.splice(index + 1, 1, " service: '" + choice + "',");
2014-05-29 22:40:23 +00:00
contactCtrl.splice(index + 3, 1, ' user: secrets.' + choice.toLowerCase() +'.user,');
contactCtrl.splice(index + 4, 1, ' pass: secrets.' + choice.toLowerCase() + '.password');
2014-05-13 06:59:46 +00:00
fs.writeFileSync('controllers/contact.js', contactCtrl.join(os.EOL));
2014-07-15 21:25:44 +00:00
index = userCtrl.indexOf(' var transporter = nodemailer.createTransport({');
2014-05-13 06:59:46 +00:00
userCtrl.splice(index + 1, 1, " service: '" + choice + "',");
userCtrl.splice(index + 3, 1, ' user: secrets.' + choice.toLowerCase() + '.user,');
userCtrl.splice(index + 4, 1, ' pass: secrets.' + choice.toLowerCase() + '.password');
2014-07-15 21:25:44 +00:00
index = userCtrl.indexOf(' var transporter = nodemailer.createTransport({', (index + 1));
2014-05-13 06:59:46 +00:00
userCtrl.splice(index + 1, 1, " service: '" + choice + "',");
userCtrl.splice(index + 3, 1, ' user: secrets.' + choice.toLowerCase() + '.user,');
userCtrl.splice(index + 4, 1, ' pass: secrets.' + choice.toLowerCase() + '.password');
fs.writeFileSync('controllers/user.js', userCtrl.join(os.EOL));
home.remove(emailForm);
home.append(success);
success.setContent('Email Service has been switched to ' + choice);
success.focus();
screen.render();
});
2014-05-11 16:24:16 +00:00
var emailText = blessed.text({
2014-05-13 06:59:46 +00:00
parent: emailForm,
2014-05-11 16:24:16 +00:00
content: 'Select one of the following email service providers for {underline}contact form{/underline} and {underline}password reset{/underline}.',
padding: 1,
bg: 'red',
fg: 'white',
tags: true
});
var sendgridRadio = blessed.radiobutton({
2014-05-13 06:59:46 +00:00
parent: emailForm,
2014-05-11 16:24:16 +00:00
top: 5,
2014-05-13 06:59:46 +00:00
checked: true,
2014-05-11 16:24:16 +00:00
mouse: true,
fg: 'white',
bg: 'blue',
content: 'SendGrid'
});
var mailgunRadio = blessed.radiobutton({
2014-05-13 06:59:46 +00:00
parent: emailForm,
2014-05-11 16:24:16 +00:00
top: 6,
mouse: true,
fg: 'white',
bg: 'blue',
content: 'Mailgun'
});
var mandrillRadio = blessed.radiobutton({
2014-05-13 06:59:46 +00:00
parent: emailForm,
2014-05-11 16:24:16 +00:00
top: 7,
mouse: true,
fg: 'white',
bg: 'blue',
content: 'Mandrill'
});
2014-05-13 06:59:46 +00:00
var emailSubmit = blessed.button({
parent: emailForm,
2014-05-11 16:24:16 +00:00
top: 9,
mouse: true,
shrink: true,
2014-05-13 06:59:46 +00:00
name: 'submit',
2014-05-11 16:24:16 +00:00
content: ' SUBMIT ',
style: {
fg: 'blue',
bg: 'white',
focus: {
fg: 'white',
bg: 'red'
}
}
});
2014-05-13 06:59:46 +00:00
emailSubmit.on('press', function() {
emailForm.submit();
});
2014-05-11 16:24:16 +00:00
var emailCancel = blessed.button({
2014-05-13 06:59:46 +00:00
parent: emailForm,
2014-05-11 16:24:16 +00:00
top: 9,
left: 9,
mouse: true,
shrink: true,
name: 'cancel',
content: ' CANCEL ',
style: {
fg: 'blue',
bg: 'white',
focus: {
fg: 'white',
bg: 'red'
}
}
});
emailCancel.on('press', function() {
home.focus();
2014-05-13 06:59:46 +00:00
home.remove(emailForm);
screen.render();
});
2014-05-11 15:11:17 +00:00
home.on('select', function(child, index) {
switch (index) {
case 0:
2014-05-13 07:57:23 +00:00
home.append(authForm);
authForm.focus();
screen.render();
2014-05-11 15:11:17 +00:00
break;
case 1:
2014-05-13 06:59:46 +00:00
home.append(emailForm);
emailForm.focus();
2014-05-11 15:11:17 +00:00
break;
case 2:
addClusterSupport();
home.append(success);
2014-05-13 05:13:27 +00:00
success.setContent('New file {underline}cluster_app.js{/underline} has been created. Your app is now able to use more than 1 CPU by running {underline}node cluster_app.js{/underline}, which in turn spawns multiple instances of {underline}app.js{/underline}');
success.focus();
2014-05-11 17:15:15 +00:00
screen.render();
2014-05-11 15:11:17 +00:00
break;
default:
process.exit(0);
}
});
screen.render();
function addClusterSupport() {
var fileContents = multiline(function() {
/*
var os = require('os');
var cluster = require('cluster');
cluster.setupMaster({
exec: 'app.js'
});
cluster.on('exit', function(worker) {
console.log('worker ' + worker.id + ' died');
cluster.fork();
});
for (var i = 0; i < os.cpus().length; i++) {
cluster.fork();
}
*/
});
fs.writeFileSync('cluster_app.js', fileContents);
2014-08-18 23:24:27 +00:00
}