|
1 | 1 | #!/usr/bin/env node |
2 | | -// the above line is needed for node bin running |
3 | | - |
4 | | -import {spawn} from 'child_process'; |
5 | | -import {existsSync} from 'fs-extra'; |
| 2 | +import {readFileSync} from 'fs-extra'; |
6 | 3 | import {join} from 'path'; |
7 | 4 | import * as yargs from 'yargs'; |
8 | 5 | import './pluginManagement/systemPlugins'; |
9 | 6 | import {routeContentRenderer} from './renderPlugins/routeContentRenderer'; |
| 7 | +import {startBackgroundServer} from './startBackgroundServer'; |
10 | 8 | import {loadConfig} from './utils/config'; |
11 | | -import {checkChangeAngular, existDistAngular, moveDistAngular} from './utils/fsAngular'; |
12 | | -import {checkStaticFolder} from './utils/fsFolder'; |
| 9 | +import {moveDistAngular} from './utils/fsAngular'; |
13 | 10 | import {httpGetJson} from './utils/httpGetJson'; |
14 | | -import {RouteTypes, ScullyConfig} from './utils/interfacesandenums'; |
| 11 | +import {RouteTypes} from './utils/interfacesandenums'; |
15 | 12 | import {isPortTaken} from './utils/isPortTaken'; |
16 | 13 | import {logError} from './utils/log'; |
17 | 14 | import {startScully} from './utils/startup'; |
18 | | -import {closeExpress, staticServer} from './utils/staticServer'; |
19 | 15 | import {waitForServerToBeAvailable} from './utils/waitForServerToBeAvailable'; |
| 16 | +import {bootServe, isBuildThere, watchMode} from './watchMode'; |
20 | 17 |
|
21 | 18 | /** the default of 10 is too shallow for generating pages. */ |
22 | 19 | require('events').defaultMaxListeners = 100; |
23 | 20 |
|
24 | 21 | let port; |
25 | 22 | // tslint:disable-next-line:variable-name |
26 | | -let _options = {}; |
| 23 | +export let _options = {}; |
| 24 | + |
| 25 | +const {argv: options} = yargs |
| 26 | + .option('path', { |
| 27 | + alias: 'p', |
| 28 | + type: 'string', |
| 29 | + description: 'The path to generate', |
| 30 | + }) |
| 31 | + .option('type', { |
| 32 | + alias: 't', |
| 33 | + type: 'string', |
| 34 | + description: 'The type to generate', |
| 35 | + }) |
| 36 | + .option('port', { |
| 37 | + alias: 'p', |
| 38 | + type: 'number', |
| 39 | + description: 'The port to run on', |
| 40 | + }) |
| 41 | + .option('folder', { |
| 42 | + type: 'string', |
| 43 | + description: 'home folder', |
| 44 | + }); |
| 45 | + |
| 46 | +if (process.argv.includes('version')) { |
| 47 | + const {version} = JSON.parse(readFileSync(join(__dirname, './package.json')).toString()); |
| 48 | + console.log('version:', version); |
| 49 | + process.exit(0); |
| 50 | +} |
27 | 51 |
|
28 | 52 | (async () => { |
| 53 | + if (process.argv.includes('killServer')) { |
| 54 | + await httpGetJson('http://localhost:1864/killMe', {suppressErrors: true}); |
| 55 | + process.exit(0); |
| 56 | + return; |
| 57 | + } |
29 | 58 | /** make sure not to do something before the config is ready */ |
30 | 59 | const scullyConfig = await loadConfig; |
31 | 60 | await isBuildThere(scullyConfig); |
32 | 61 |
|
33 | | - const {argv: options} = yargs |
34 | | - .option('path', { |
35 | | - alias: 'p', |
36 | | - type: 'string', |
37 | | - description: 'The path to generate', |
38 | | - }) |
39 | | - .option('type', { |
40 | | - alias: 't', |
41 | | - type: 'string', |
42 | | - description: 'The type to generate', |
43 | | - }) |
44 | | - .option('port', { |
45 | | - alias: 'p', |
46 | | - type: 'number', |
47 | | - description: 'The port to run on', |
48 | | - }); |
49 | | - |
50 | 62 | if (process.argv.includes('serve')) { |
51 | | - port = options.path; |
52 | | - console.log('starting static server...'); |
53 | | - process.title = 'ScullyServer'; |
54 | | - checkChangeAngular(options.path); |
55 | | - restartStaticServer(); |
| 63 | + await bootServe(scullyConfig); |
56 | 64 | } else { |
57 | 65 | const folder = join(scullyConfig.homeFolder, scullyConfig.distFolder); |
| 66 | + /** copy in current buildfile */ |
| 67 | + await moveDistAngular(folder, scullyConfig.outDir, {removeStaticDist: true, reset: false}); |
| 68 | + |
| 69 | + /** server already up and running? */ |
| 70 | + const isTaken = await isPortTaken(scullyConfig.staticport); |
| 71 | + if (!isTaken) { |
| 72 | + startBackgroundServer(scullyConfig); |
| 73 | + } else { |
| 74 | + // debug only |
| 75 | + console.log(`Background servers already running.`); |
| 76 | + } |
58 | 77 |
|
59 | | - if (!existDistAngular(scullyConfig.homeFolder)) { |
| 78 | + if (!(await waitForServerToBeAvailable().catch(e => false))) { |
| 79 | + logError('Could not connect to server'); |
60 | 80 | process.exit(15); |
61 | 81 | } |
62 | 82 |
|
63 | | - await moveDistAngular(folder, scullyConfig.outDir, {removeStaticDist: true, reset: false}); |
| 83 | + console.log('servers available'); |
| 84 | + await startScully(); |
64 | 85 |
|
65 | | - if (options.path && options.type) { |
66 | | - routeContentRenderer({ |
67 | | - route: options.path, |
68 | | - type: (options.type as unknown) as RouteTypes, |
69 | | - }); |
70 | | - if (process.argv.includes('watch')) { |
71 | | - _options = options; |
72 | | - watchMode(); |
73 | | - } |
| 86 | + if (process.argv.includes('watch')) { |
| 87 | + _options = options; |
| 88 | + watchMode(); |
74 | 89 | } else { |
75 | | - /** server already up and running? */ |
76 | | - const isTaken = await isPortTaken(scullyConfig.staticport); |
77 | 90 | if (!isTaken) { |
78 | | - spawn('node', [join(scullyConfig.homeFolder, './node_modules/.bin/scully'), 'serve'], { |
79 | | - detached: true, |
80 | | - }).on('close', err => { |
81 | | - if (+err > 0) { |
82 | | - spawn( |
83 | | - 'node', |
84 | | - [join(scullyConfig.homeFolder, './node_modules/@scullyio/scully/scully.js'), 'serve'], |
85 | | - { |
86 | | - detached: true, |
87 | | - } |
88 | | - ).on('close', err2 => { |
89 | | - if (+err2 > 0) { |
90 | | - spawn('node', [join(scullyConfig.homeFolder, '/dist/scully/scully'), 'serve'], { |
91 | | - detached: true, |
92 | | - }); |
93 | | - } |
94 | | - }); |
95 | | - } |
96 | | - }); |
97 | | - |
98 | | - console.log('started servers in background'); |
99 | | - } else { |
100 | | - // debug only |
101 | | - console.log(`Background servers already running.`); |
102 | | - } |
103 | | - |
104 | | - if (!(await waitForServerToBeAvailable().catch(e => false))) { |
105 | | - logError('Could not connect to server'); |
106 | | - process.exit(15); |
| 91 | + // kill serve ports |
| 92 | + await httpGetJson('http://localhost:1864/killMe', {suppressErrors: true}); |
107 | 93 | } |
108 | | - |
109 | | - console.log('servers available'); |
110 | | - await startScully(); |
111 | | - |
112 | | - if (process.argv.includes('watch')) { |
113 | | - _options = options; |
114 | | - watchMode(); |
115 | | - } else { |
116 | | - if (!isTaken) { |
117 | | - // kill serve ports |
118 | | - await httpGetJson('http://localhost:1864/killMe', {suppressErrors: true}); |
119 | | - } |
120 | | - /** done, stop the program */ |
121 | | - process.exit(0); |
122 | | - } |
123 | | - } |
124 | | - } |
125 | | -})(); |
126 | | - |
127 | | -// TODO : we need rewrite this to observables for don't have memory leaks |
128 | | -async function watchMode() { |
129 | | - await checkStaticFolder(); |
130 | | - // g for generate and the q for quit |
131 | | - checkForManualRestart(); |
132 | | - // @ts-ignore |
133 | | - await checkChangeAngular(_options.path, false, true); |
134 | | -} |
135 | | - |
136 | | -export function checkForManualRestart() { |
137 | | - const readline = require('readline').createInterface({ |
138 | | - input: process.stdin, |
139 | | - output: process.stdout, |
140 | | - }); |
141 | | - |
142 | | - readline.question(`Press g for manual regenerate, or q for close the server. \n`, command => { |
143 | | - if (command.toLowerCase() === 'g') { |
144 | | - startScully().then(() => checkForManualRestart()); |
145 | | - } else if (command.toLowerCase() === 'q') { |
146 | | - readline.close(); |
| 94 | + /** done, stop the program */ |
147 | 95 | process.exit(0); |
148 | | - } else { |
149 | | - readline.close(); |
150 | | - checkForManualRestart(); |
151 | 96 | } |
152 | | - }); |
153 | | -} |
154 | | - |
155 | | -export function startScullyWatchMode() { |
156 | | - startScully(); |
157 | | -} |
158 | | - |
159 | | -function startStaticServer() { |
160 | | - staticServer(); |
161 | | -} |
162 | | - |
163 | | -let restartTimer: NodeJS.Timer; |
164 | | -export function restartStaticServer() { |
165 | | - // tslint:disable-next-line: no-unused-expression |
166 | | - restartTimer && clearTimeout(restartTimer); |
167 | | - restartTimer = setTimeout(() => { |
168 | | - closeExpress(); |
169 | | - startStaticServer(); |
170 | | - }, 500); |
171 | | -} |
172 | | - |
173 | | -export async function isBuildThere(config: ScullyConfig) { |
174 | | - const dist = join(config.homeFolder, config.distFolder); |
175 | | - if (existsSync(dist) && existsSync(join(dist, 'index.html'))) { |
176 | | - return true; |
177 | 97 | } |
178 | | - logError(`Angular distribution files not found, run "ng build" first`); |
179 | | - process.exit(15); |
180 | | -} |
| 98 | +})(); |
0 commit comments