@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
77 step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
88 } ) ;
99} ;
10- import { mkdirSync , statSync , writeFileSync , existsSync } from 'fs' ;
10+ import { mkdirSync , statSync , writeFileSync , existsSync , readFileSync } from 'fs' ;
1111import fs_extra from 'fs-extra' ;
1212const { copySync, mkdirpSync } = fs_extra ;
1313import Store from 'electron-store' ;
@@ -40,6 +40,53 @@ function shouldMigrateDatabase(store) {
4040function shouldOptimize ( store ) {
4141 return process . env . NODE_ENV !== 'development' ;
4242}
43+ function hasNightwatchInstalled ( appPath ) {
44+ const candidateRoots = [
45+ appPath ,
46+ join ( appPath , "build" , "__nativephp_app_bundle" )
47+ ] ;
48+ for ( const root of candidateRoots ) {
49+ if ( existsSync ( join ( root , "vendor" , "laravel" , "nightwatch" ) ) ) {
50+ return true ;
51+ }
52+ const composerLock = join ( root , "composer.lock" ) ;
53+ if ( ! existsSync ( composerLock ) ) {
54+ continue ;
55+ }
56+ try {
57+ if ( readFileSync ( composerLock , "utf8" ) . includes ( "\"name\": \"laravel/nightwatch\"" ) ) {
58+ return true ;
59+ }
60+ }
61+ catch ( _a ) {
62+ }
63+ }
64+ return false ;
65+ }
66+ function getNightwatchToken ( appPath ) {
67+ if ( process . env . NIGHTWATCH_TOKEN ) {
68+ return process . env . NIGHTWATCH_TOKEN ;
69+ }
70+ const candidateRoots = [
71+ appPath ,
72+ join ( appPath , "build" , "__nativephp_app_bundle" )
73+ ] ;
74+ for ( const root of candidateRoots ) {
75+ const envPath = join ( root , ".env" ) ;
76+ if ( ! existsSync ( envPath ) ) {
77+ continue ;
78+ }
79+ try {
80+ const content = readFileSync ( envPath , "utf8" ) ;
81+ const match = content . match ( / ^ N I G H T W A T C H _ T O K E N = ( .+ ) $ / m) ;
82+ if ( match && match [ 1 ] ) {
83+ return match [ 1 ] . replace ( / ^ [ ' " ] | [ ' " ] $ / g, "" ) ;
84+ }
85+ }
86+ catch ( _a ) {
87+ }
88+ }
89+ }
4390function getPhpPort ( ) {
4491 return __awaiter ( this , void 0 , void 0 , function * ( ) {
4592 const suggestedPort = yield getPort ( {
@@ -229,13 +276,28 @@ function serveApp(secret, apiPort, phpIniSettings) {
229276 ensureAppFoldersAreAvailable ( ) ;
230277 console . log ( 'Making sure app folders are available' ) ;
231278 const env = getDefaultEnvironmentVariables ( secret , apiPort ) ;
279+ const nightwatchToken = getNightwatchToken ( appPath ) ;
280+ let phpNightWatchPort ;
281+ if ( nightwatchToken && hasNightwatchInstalled ( appPath ) ) {
282+ phpNightWatchPort = yield getPhpPort ( ) ;
283+ env . NIGHTWATCH_TOKEN = nightwatchToken ;
284+ env . NIGHTWATCH_INGEST_URI = `127.0.0.1:${ phpNightWatchPort } ` ;
285+ }
286+ else if ( nightwatchToken ) {
287+ console . log ( "Skipping Nightwatch: package not installed." ) ;
288+ }
232289 const phpOptions = {
233290 cwd : appPath ,
234291 env
235292 } ;
236293 const store = new Store ( {
237294 name : 'nativephp' ,
238295 } ) ;
296+ if ( env . NIGHTWATCH_INGEST_URI && phpNightWatchPort ) {
297+ console . log ( 'Starting Nightwatch server...' ) ;
298+ callPhp ( [ 'artisan' , 'nightwatch:agent' , `--listen-on=${ env . NIGHTWATCH_INGEST_URI } ` ] , phpOptions , phpIniSettings ) ;
299+ console . log ( 'Nightwatch server started on port:' , phpNightWatchPort ) ;
300+ }
239301 if ( shouldOptimize ( store ) ) {
240302 console . log ( 'Caching view and routes...' ) ;
241303 let result = callPhpSync ( [ 'artisan' , 'optimize' ] , phpOptions , phpIniSettings ) ;
0 commit comments