2014-05-11 19:07:58 +00:00
var fs = require ( 'fs' ) ;
var blessed = require ( 'blessed' ) ;
var multiline = require ( 'multiline' ) ;
2014-05-11 12:10:00 +00:00
var screen = blessed . screen ( {
2014-05-11 15:11:17 +00:00
autoPadding : true
2014-05-11 12:10:00 +00:00
} ) ;
2014-05-11 15:11:17 +00:00
var home = blessed . list ( {
2014-05-11 12:10:00 +00:00
parent : screen ,
padding : { top : 2 } ,
mouse : true ,
keys : true ,
fg : 'white' ,
bg : 'blue' ,
selectedFg : 'blue' ,
selectedBg : 'white' ,
items : [
2014-05-11 19:07:58 +00:00
'» REMOVE AUTHENTICATION' ,
'» CHANGE EMAIL SERVICE' ,
'» ENABLE SOCKET.IO' ,
'» ADD NODE.JS CLUSTER SUPPORT' ,
'» EXIT'
2014-05-11 12:10:00 +00:00
]
} ) ;
2014-05-11 19:07:58 +00:00
var inner = blessed . form ( {
2014-05-11 16:24:16 +00:00
top : 'center' ,
left : 'center' ,
2014-05-11 19:07:58 +00:00
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'
} ) ;
2014-05-11 19:07:58 +00:00
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 socketText = blessed . text ( {
top : 'top' ,
bg : 'red' ,
fg : 'white' ,
tags : true ,
content : 'Add real-time support to your application with Socket.IO.'
} ) ;
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 : {
2014-05-11 19:07:58 +00:00
fg : 'red' ,
bg : 'white'
2014-05-11 17:15:15 +00:00
}
}
} ) ;
2014-05-11 19:07:58 +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 : {
2014-05-11 19:07:58 +00:00
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 : {
2014-05-11 19:07:58 +00:00
fg : 'red' ,
bg : 'white'
2014-05-11 17:15:15 +00:00
}
}
} ) ;
2014-05-11 16:24:16 +00:00
2014-05-11 19:07:58 +00:00
cancel . on ( 'press' , function ( ) {
home . focus ( ) ;
home . remove ( inner ) ;
screen . render ( ) ;
} ) ;
var auth = blessed . form ( {
2014-05-11 15:11:17 +00:00
mouse : true ,
keys : true ,
fg : 'white' ,
bg : 'blue' ,
padding : { left : 1 , right : 1 }
} ) ;
var authText = blessed . text ( {
2014-05-11 19:07:58 +00:00
parent : auth ,
2014-05-11 15:11:17 +00:00
content : 'Selecting a checkbox adds an authentication provider. Unselecting a checkbox removes it. If authentication provider is already present, no action will be taken.' ,
padding : 1 ,
bg : 'magenta' ,
fg : 'white'
} ) ;
var facebookCheckbox = blessed . checkbox ( {
2014-05-11 19:07:58 +00:00
parent : auth ,
2014-05-11 15:11:17 +00:00
top : 6 ,
checked : true ,
mouse : true ,
fg : 'white' ,
bg : 'blue' ,
content : 'Facebook'
} ) ;
var githubCheckbox = blessed . checkbox ( {
2014-05-11 19:07:58 +00:00
parent : auth ,
2014-05-11 15:11:17 +00:00
top : 7 ,
checked : true ,
mouse : true ,
fg : 'white' ,
bg : 'blue' ,
content : 'GitHub'
} ) ;
var googleCheckbox = blessed . checkbox ( {
2014-05-11 19:07:58 +00:00
parent : auth ,
2014-05-11 15:11:17 +00:00
top : 8 ,
checked : true ,
mouse : true ,
fg : 'white' ,
bg : 'blue' ,
content : 'Google'
} ) ;
var twitterCheckbox = blessed . checkbox ( {
2014-05-11 19:07:58 +00:00
parent : auth ,
2014-05-11 15:11:17 +00:00
top : 9 ,
checked : true ,
mouse : true ,
fg : 'white' ,
bg : 'blue' ,
content : 'Twitter'
} ) ;
var linkedinCheckbox = blessed . checkbox ( {
2014-05-11 19:07:58 +00:00
parent : auth ,
2014-05-11 15:11:17 +00:00
top : 10 ,
checked : true ,
mouse : true ,
fg : 'white' ,
bg : 'blue' ,
content : 'LinkedIn'
} ) ;
var instagramCheckbox = blessed . checkbox ( {
2014-05-11 19:07:58 +00:00
parent : auth ,
2014-05-11 15:11:17 +00:00
top : 11 ,
checked : true ,
mouse : true ,
fg : 'white' ,
bg : 'blue' ,
content : 'Instagram'
} ) ;
2014-05-11 19:07:58 +00:00
var authSubmit = blessed . button ( {
parent : auth ,
2014-05-11 15:11:17 +00:00
top : 13 ,
mouse : true ,
shrink : true ,
2014-05-11 19:07:58 +00:00
name : 'submit' ,
2014-05-11 15:11:17 +00:00
content : ' SUBMIT ' ,
style : {
fg : 'blue' ,
bg : 'white' ,
focus : {
fg : 'white' ,
bg : 'red'
}
}
} ) ;
var authCancel = blessed . button ( {
2014-05-11 19:07:58 +00:00
parent : auth ,
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'
}
}
} ) ;
2014-05-11 19:07:58 +00:00
authCancel . on ( 'press' , function ( ) {
home . focus ( ) ;
home . remove ( auth ) ;
screen . render ( ) ;
} ) ;
2014-05-11 16:24:16 +00:00
var email = blessed . form ( {
mouse : true ,
keys : true ,
2014-05-11 19:07:58 +00:00
fg : 'white' ,
2014-05-11 16:24:16 +00:00
bg : 'blue' ,
padding : { left : 1 , right : 1 }
} ) ;
2014-05-11 19:07:58 +00:00
2014-05-11 16:24:16 +00:00
var emailText = blessed . text ( {
parent : email ,
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 ( {
parent : email ,
top : 5 ,
mouse : true ,
fg : 'white' ,
bg : 'blue' ,
content : 'SendGrid'
} ) ;
var mailgunRadio = blessed . radiobutton ( {
parent : email ,
top : 6 ,
mouse : true ,
fg : 'white' ,
bg : 'blue' ,
content : 'Mailgun'
} ) ;
var mandrillRadio = blessed . radiobutton ( {
parent : email ,
top : 7 ,
mouse : true ,
fg : 'white' ,
bg : 'blue' ,
content : 'Mandrill'
} ) ;
var emailOk = blessed . button ( {
parent : email ,
top : 9 ,
mouse : true ,
shrink : true ,
name : 'ok' ,
content : ' SUBMIT ' ,
style : {
fg : 'blue' ,
bg : 'white' ,
focus : {
fg : 'white' ,
bg : 'red'
}
}
} ) ;
var emailCancel = blessed . button ( {
parent : email ,
top : 9 ,
left : 9 ,
mouse : true ,
shrink : true ,
name : 'cancel' ,
content : ' CANCEL ' ,
style : {
fg : 'blue' ,
bg : 'white' ,
focus : {
fg : 'white' ,
bg : 'red'
}
}
} ) ;
2014-05-11 19:07:58 +00:00
emailCancel . on ( 'press' , function ( ) {
home . focus ( ) ;
home . remove ( email ) ;
screen . render ( ) ;
} ) ;
2014-05-11 15:11:17 +00:00
var title = blessed . text ( {
parent : screen ,
2014-05-11 12:10:00 +00:00
align : 'center' ,
fg : 'blue' ,
bg : 'white' ,
content : 'Hackathon Starter (c) 2014'
2014-05-11 15:11:17 +00:00
} ) ;
2014-05-11 12:10:00 +00:00
2014-05-11 15:11:17 +00:00
var footer = blessed . text ( {
parent : screen ,
2014-05-11 12:10:00 +00:00
bottom : 0 ,
fg : 'white' ,
bg : 'blue' ,
2014-05-11 19:07:58 +00:00
tags : true ,
content : ' {cyan-fg}<Up/Down>{/cyan-fg} moves | {cyan-fg}<Enter>{/cyan-fg} selects | {cyan-fg}<q>{/cyan-fg} exits'
2014-05-11 15:11:17 +00:00
} ) ;
home . on ( 'select' , function ( child , index ) {
switch ( index ) {
case 0 :
2014-05-11 19:07:58 +00:00
home . append ( auth ) ;
auth . focus ( ) ;
screen . render ( ) ;
2014-05-11 15:11:17 +00:00
break ;
case 1 :
2014-05-11 16:24:16 +00:00
home . append ( email ) ;
email . focus ( ) ;
2014-05-11 15:11:17 +00:00
break ;
case 2 :
2014-05-11 17:15:15 +00:00
home . append ( inner ) ;
inner . append ( socketText ) ;
inner . focus ( ) ;
screen . render ( ) ;
2014-05-11 15:11:17 +00:00
break ;
case 3 :
2014-05-11 19:07:58 +00:00
// Cluster
addClusterSupport ( ) ;
home . append ( success ) ;
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 node {underline}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 19:07:58 +00:00
2014-05-11 15:11:17 +00:00
break ;
default :
process . exit ( 0 ) ;
}
} ) ;
2014-05-11 12:10:00 +00:00
screen . key ( 'q' , function ( ) {
process . exit ( 0 ) ;
} ) ;
screen . render ( ) ;
2014-05-11 19:07:58 +00:00
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 ) ;
}