import express from 'express' ; import _find from 'lodash/find' ; import path from 'path' ; import compression from 'compression' ; import pawAssets from 'pwa-assets' ; import server , { app as serverApp , beforeStart , afterStart } from './server' ; import pawConfig from '../config' ; import { assetsToArray } from '../utils/utils' ; const { jsDependencyMap , cssDependencyMap , ... assets } = pawAssets ; /**
 * defining the current dir
 */ let currentDir = __dirname ; const PAW_GLOBAL = { } ; const filename = _find ( process . argv , ( arg ) => arg . indexOf ( '/server.js' ) !== - 1 ) ; if ( filename ) { currentDir = path . dirname ( path . resolve ( filename ) ) ; } if ( ! currentDir ) currentDir = __dirname ; const app = express ( ) ; app . use ( compression ( { threshold : '2KB' } ) ) ; app . disable ( 'x-powered-by' ) ; app . use ( `${ pawConfig . appRootUrl }/sw.js` , express . static ( path . join ( currentDir , 'build' , 'sw.js' ) , { maxAge : 0 , setHeaders : ( res ) => { res . set ( 'Cache-Control' , 'no-cache' ) ; } , } ) ) ; const cacheTime = pawConfig . assetsMaxAge || 86400000 * 30 ; app . use ( pawConfig . appRootUrl || '/' , express . static ( path . join ( currentDir , 'build' ) , { maxAge : cacheTime , } ) ) ; serverApp . locals . assets = assetsToArray ( assets ) ; serverApp . locals . cssDependencyMap = cssDependencyMap ; serverApp . locals . jsDependencyMap = jsDependencyMap ; app . use ( ( req , res , next ) => server ( req , res , next , PAW_GLOBAL ) ) ; export default app ; const serverConfig = { port : pawConfig . port , host : pawConfig . host , } ; beforeStart ( serverConfig , PAW_GLOBAL , ( err ) => { if ( err ) { console . error ( err ) ; return ; } if ( ! pawConfig . singlePageApplication ) { app . listen ( parseInt ( serverConfig . port || '9090' , 10 ) , serverConfig . host || 'localhost' , ( ) => { console . log ( `Listening to http://${ serverConfig . host }:${ serverConfig . port }` ) ; afterStart ( PAW_GLOBAL ) ; } ) ; } } ) ;