@@ -4,36 +4,35 @@ const api = require('./api/');
44const projectSourceWatcher = require ( './project-source/' ) ;
55const { SERVER_PORT , SOCKET_EVENT_TYPE } = require ( '../shared/constants' ) ;
66
7- // TODO: should come as params
8- const ROOT_DIR = 'example-project' ;
9- const PROJECT_DIR = `${ ROOT_DIR } /src` ; //, src/public/js, example-project, get as param to server script
10- const ENTRY = `${ PROJECT_DIR } /index.js` ; //, index.js, get as param to server script
11- const WEBPACK_CONFIG_FILE_PATH = `${ ROOT_DIR } /webpack.config.js` ;
12- // -- end params --
13-
14- const httpServer = http . createServer ( api . handleRequests ( PROJECT_DIR ) ) ;
15- httpServer . listen ( SERVER_PORT , ( ) => {
16- console . log ( new Date ( ) + `Server is listening localhost: ${ SERVER_PORT } .` ) ;
17- } ) ;
7+ const run = ( { projectDir, entryFile, webpackConfigFile } ) => {
8+ const httpServer = http . createServer ( api . handleRequests ( projectDir ) ) ;
9+ httpServer . listen ( SERVER_PORT , ( ) => {
10+ console . log ( new Date ( ) + `API server is listening: ${ SERVER_PORT } .` ) ;
11+ } ) ;
1812
19- const webSocketServer = new WebSocketServer ( { httpServer } ) ;
20- webSocketServer . on ( 'request' , request => {
21- const connection = request . accept ( null , request . origin ) ;
13+ const webSocketServer = new WebSocketServer ( { httpServer } ) ;
14+ webSocketServer . on ( 'request' , request => {
15+ const connection = request . accept ( null , request . origin ) ;
2216
23- projectSourceWatcher . subscribeOnChange ( PROJECT_DIR , ENTRY , WEBPACK_CONFIG_FILE_PATH , {
24- onInit : data =>
25- connection . sendUTF (
26- JSON . stringify ( {
27- type : SOCKET_EVENT_TYPE . INIT_SOURCE_FILES_SYNC ,
28- data
29- } )
30- ) ,
31- onChange : data =>
32- connection . sendUTF (
33- JSON . stringify ( {
34- type : SOCKET_EVENT_TYPE . UPDATE_SOURCE_FILE_SYNC ,
35- data
36- } )
37- )
17+ projectSourceWatcher . subscribeOnChange ( projectDir , entryFile , webpackConfigFile , {
18+ onInit : data =>
19+ connection . sendUTF (
20+ JSON . stringify ( {
21+ type : SOCKET_EVENT_TYPE . INIT_SOURCE_FILES_SYNC ,
22+ data
23+ } )
24+ ) ,
25+ onChange : data =>
26+ connection . sendUTF (
27+ JSON . stringify ( {
28+ type : SOCKET_EVENT_TYPE . UPDATE_SOURCE_FILE_SYNC ,
29+ data
30+ } )
31+ )
32+ } ) ;
3833 } ) ;
39- } ) ;
34+ } ;
35+
36+ module . exports = {
37+ run
38+ } ;
0 commit comments