@@ -10,14 +10,19 @@ var fs = require('fs');
1010var formidable = require ( 'formidable' ) ;
1111var pjson = require ( '../package.json' ) ;
1212var pio = require ( './pio' ) ;
13- var serial = require ( './console' ) ;
1413var http = require ( 'http' ) ;
1514var ua = require ( 'universal-analytics' ) ;
1615var promisify = require ( './helpers' ) . promisify ;
1716
1817var port = 3000 ;
1918var server = http . Server ( app ) ;
2019var visitor = ua ( 'UA-99239389-1' ) ;
20+ var isElectron = module . parent && module . parent . filename . indexOf ( 'index.js' ) >= 0 ;
21+
22+ var serial ;
23+ var serial_enabled = ! isElectron || process . platform == 'linux' ;
24+ if ( serial_enabled )
25+ serial = require ( './console' ) ;
2126
2227app . use ( '/' , express . static ( path . join ( __dirname , '..' , 'static' ) ) ) ;
2328app . use ( '/libs' , express . static ( path . join ( __dirname , '..' , 'node_modules' ) ) ) ;
@@ -52,7 +57,9 @@ function serial_init(){
5257 } )
5358 } ) . catch ( a => console . error ( a ) ) ;
5459}
55- app . get ( '/ports' , function ( req , res , next ) {
60+ app . get ( '/ports' , function ( req , res ) {
61+ if ( ! serial_enabled )
62+ return res . status ( 403 ) . end ( )
5663 req . socket . setTimeout ( Number . MAX_SAFE_INTEGER ) ;
5764 console . log ( 'SSE conected' ) ;
5865 res . writeHead ( 200 , {
@@ -72,13 +79,17 @@ app.get('/ports', function (req, res,next) {
7279} ) ;
7380
7481app . get ( '/port/:port/:speed' , function ( req , res ) {
82+ if ( ! serial_enabled )
83+ return res . status ( 404 )
7584 serial . init ( server , req . params . port , req . params . speed )
7685 . then ( data => {
7786 res . send ( data ) ;
7887 } )
7988 . catch ( a => res . status ( 403 ) . send ( a ) )
8089} ) ;
8190app . get ( '/port-close/:port' , function ( req , res ) {
91+ if ( ! serial_enabled )
92+ return res . status ( 404 )
8293 serial . close ( req . params . port )
8394 . then ( data => {
8495 res . send ( data ) ;
@@ -163,7 +174,7 @@ app.get('/pio-flash/:port', function (req, res) {
163174 params . push ( port )
164175 close = true ;
165176 }
166- ( close ?serial . close ( port ) :Promise . resolve ( true ) )
177+ ( close && serial_enabled ?serial . close ( port ) :Promise . resolve ( true ) )
167178 . then ( a => git . root ( ) )
168179 . then ( root => {
169180 console . log ( root ) ;
@@ -268,7 +279,7 @@ function main(noOpn){
268279 console . error ( e ) ;
269280 throw e ;
270281 } )
271- . then ( serial_init )
282+ . then ( serial_enabled ? serial_init : a => a )
272283// .catch(a=>console.error('serial failed'))
273284 . then ( ( ) => getPort ( 3000 ) )
274285 . then ( port => new Promise ( ( done , fail ) => {
0 commit comments