44var fs = require ( 'fs' ) ;
55const Configstore = require ( 'configstore' ) ;
66const pkg = require ( './package.json' ) ;
7- const conf = new Configstore ( pkg . name )
7+ const conf = new Configstore ( pkg . name ) ;
8+ const dialog = require ( "remote" ) . dialog ;
89var os ;
910
1011var unix_paths = [
@@ -13,43 +14,49 @@ var unix_paths = [
1314 "/etc/php" ,
1415 "/usr/lib/php" ,
1516 "/usr/share/php"
16- ]
17+ ] ;
1718
1819var win_paths = [
1920 "C:\\php\\php.exe" ,
2021 "C:\\xampp\\php\\php.exe"
21- ]
22+ ] ;
2223
2324var locale = window . navigator . userLanguage || window . navigator . language ;
2425
2526$ ( function ( ) {
27+ // Buttons actions
28+ $ ( "#browse" ) . click ( browse ) ; // Invoke browse();
29+ $ ( "#type" ) . click ( type ) ; // Invoke type();
30+ $ ( "#type-done" ) . click ( typeDone ) ; // Invoke typeDone();
31+ $ ( "#quit" ) . click ( function ( ) { require ( "remote" ) . app . quit ( ) ; } ) ;
2632
33+ // Startup routines
2734 // Save locale
2835 if ( locale )
2936 conf . set ( "general.locale" , locale ) ;
3037 else
3138 conf . set ( "general.locale" , "en" ) ;
3239
3340 // Check OS
34- checkWrite ( "Detecting system... " , 0 ) ;
41+ console . log ( "Detecting system... " ) ;
3542
3643 if ( process . platform == "win32" ) {
3744 os = "win" ;
38- checkWrite ( "Windows" , 1 ) ;
45+ console . log ( "Windows" ) ;
3946 }
4047 else if ( process . platform == 'darwin' ) {
4148 os = "osx" ;
42- checkWrite ( "Mac OSX" , 1 ) ;
49+ console . log ( "Mac OSX" ) ;
4350 }
4451 else {
4552 os = "linux" ;
46- checkWrite ( "Linux" , 1 ) ;
53+ console . log ( "Linux" ) ;
4754 }
4855
4956 conf . set ( "system.os" , os ) ;
5057
5158 // Searching for PHP binary
52- checkWrite ( "Trying to find PHP binary... " , 0 ) ;
59+ console . log ( "Trying to find PHP binary... " ) ;
5360 switch ( os ) {
5461 case "osx" :
5562 case "linux" :
@@ -77,23 +84,58 @@ function checkPhpPath(list, index) {
7784}
7885
7986function phpFound ( path ) {
80- checkWrite ( "Found! (" + path + ")" , 1 ) ;
81- checkWrite ( "Storing data..." , 0 ) ;
87+ $ ( "#output" ) . toggleClass ( "alert-danger alert-success" ) ;
88+ console . log ( "Found! (" + path + ")" ) ;
89+ console . log ( "Storing data..." ) ;
8290 conf . set ( "php.path" , path ) ;
83- checkWrite ( "Done!" , 1 ) ;
84- checkWrite ( "Starting app..." ) ;
85- // Wait for 1.5 second, just in the first run
86- setTimeout ( 'window.location = "index.html"' , 1500 ) ;
91+ console . log ( "Done!" ) ;
92+ console . log ( "Starting app..." ) ;
93+
94+ checkWrite ( "PHP binary found! (" + path + ")<br>Starting app..." ) ;
95+ // Wait for 2 seconds, just in the first run
96+ setTimeout ( 'window.location = "index.html"' , 2000 ) ;
8797}
8898
8999function phpNotFound ( ) {
90- // @TODO Implement instructions and possibility to search for PHP binary
91- checkWrite ( "Not found. Install PHP and try again." ) ;
100+ console . log ( "Not found. Install PHP and try again." ) ;
101+
102+ checkWrite ( "Could not find PHP binary!" ) ;
103+ phpSearchOptions ( true ) ;
104+ }
105+
106+ function checkWrite ( text ) {
107+ $ ( "#output" ) . html ( text ) ;
108+ }
109+
110+ function browse ( ) {
111+ var file = dialog . showOpenDialog ( {
112+ "title" : "Find PHP binary"
113+ } ) ;
114+
115+ if ( file ) {
116+ phpSearchOptions ( false ) ;
117+ phpFound ( file ) ;
118+ }
119+ }
120+
121+ function type ( ) {
122+ $ ( "#type" ) . css ( "display" , "none" ) ;
123+ $ ( "#type-input" ) . css ( "display" , "block" ) ;
124+ $ ( "form" ) . on ( "submit" , function ( ) { return false ; } ) ; // Prevents form default
125+ $ ( "#path" ) . focus ( ) ;
126+ }
127+
128+ function typeDone ( ) {
129+ var tPath = $ ( "#path" ) . val ( ) ;
130+ if ( fs . lstatSync ( tPath ) . isFile ( ) )
131+ phpFound ( tPath ) ;
132+ else
133+ $ ( "#output" ) . html ( "Oops! Invalid binary or the file doesn't exists." ) ;
92134}
93135
94- function checkWrite ( text , br ) {
95- if ( br )
96- $ ( "body " ) . append ( text + "<br> ") ;
136+ function phpSearchOptions ( show ) {
137+ if ( show )
138+ $ ( "#find-or-quit " ) . css ( "visibility" , "visible ") ;
97139 else
98- $ ( "body " ) . append ( text ) ;
140+ $ ( "#find-or-quit " ) . css ( "visibility" , "hidden" ) ;
99141}
0 commit comments