1+ const shell = require ( "child_process" ) ;
2+
3+ // Load cli
4+ let cli = require ( "./lib/cli" ) ( ) ;
5+ let config = new ( require ( "./lib/config" ) ) ( ) ;
6+
7+ // Check if we need to create a config
8+ if ( cli . createConfig ) {
9+ config . create ( ) ;
10+ process . exit ( ) ;
11+ }
12+
13+ // Get the existing config
14+ try {
15+ config = config . get ( ) ;
16+ }
17+ catch ( ex ) {
18+ console . log ( "Could not load config." ) ;
19+ process . exit ( ) ;
20+ }
21+
22+ // Remove the directory first if -f is set
23+ if ( cli . force && config . output && config . output . directory ) {
24+ console . log ( "Cleaning..." ) ;
25+ shell . execSync ( "rm -rf " + config . output . directory )
26+ }
27+
28+ // Start the engine
29+
30+ /// Check for input server command
31+ let server = false ;
32+ let serverStarted = false ;
33+ let downloadStarted = false ;
34+ if ( config . input && config . input . directory && config . input . runcmd ) {
35+
36+ // Run the command
37+ console . log ( "Starting server..." ) ;
38+ let cmd = config . input . runcmd ;
39+ server = shell . exec ( cmd , {
40+ "cwd" : config . input . directory
41+ } ) ;
42+
43+ server . stdout . on ( "data" , ( data ) => {
44+ serverStarted = true ;
45+ download ( ) ;
46+ } ) ;
47+ server . stderr . on ( "data" , ( data ) => {
48+ console . log ( `Server Error: ${ data } ` ) ;
49+ process . kill ( ) ;
50+ } ) ;
51+ server . on ( "close" , ( code ) => {
52+ console . log ( `Server exited with code ${ code } ` ) ;
53+ serverStarted = false ;
54+ process . kill ( ) ;
55+ } )
56+ server . on ( "exit" , ( code ) => {
57+ console . log ( `Server exited with code ${ code } ` ) ;
58+ serverStarted = false ;
59+ process . kill ( ) ;
60+ } )
61+
62+ }
63+
64+ /**
65+ * Run the download
66+ */
67+ function download ( ) {
68+
69+ if ( downloadStarted ) {
70+ return ;
71+ }
72+ else {
73+ downloadStarted = true ;
74+ }
75+
76+ /// Create wget options
77+ let wgetOptions = "" ;
78+ if ( config . options && config . options . length ) {
79+ wgetOptions = config . options . join ( " " ) + " " ;
80+ }
81+
82+ if ( config . output && config . output . directory ) {
83+ wgetOptions += "--directory-prefix=" + config . output . directory + " " ;
84+ wgetOptions += "--no-host-directories " ;
85+ }
86+
87+ let wgetDomains = "" ;
88+ if ( config . domains && config . domains . length ) {
89+ wgetDomains = config . domains . join ( " " ) ;
90+
91+ wgetOptions += "-d " + wgetDomains + " " ;
92+ }
93+
94+ let wgetcmd = "" ;
95+ if ( wgetOptions . length && wgetDomains . length ) {
96+ wgetcmd = "wget " + wgetOptions + wgetDomains
97+ }
98+ else if ( ! wgetOptions . length ) {
99+ throw "Could not read options" ;
100+ }
101+ else if ( ! wgetDomains . length ) {
102+ throw "Could not read domains." ;
103+ }
104+
105+ /// Run wget
106+ if ( wgetcmd . length && serverStarted ) {
107+ console . log ( "Running " + wgetcmd ) ;
108+
109+ try {
110+ shell . execSync ( wgetcmd ) ;
111+ console . log ( "Done." ) ;
112+ }
113+ catch ( ex ) {
114+ console . log ( "Error: " , ex ) ;
115+ }
116+
117+ }
118+ else {
119+ // Server not started
120+ console . log ( "Server wasn't started." ) ;
121+ }
122+
123+ if ( server ) {
124+ server . kill ( ) ;
125+ }
126+ process . exit ( ) ;
127+
128+ }
0 commit comments