|
1 | 1 | import {Browser, launch, LaunchOptions} from 'puppeteer'; |
2 | | -import {Observable} from 'rxjs'; |
3 | | -import {shareReplay, take} from 'rxjs/operators'; |
4 | | -import {log} from '../utils/log'; |
| 2 | +import {from, Observable} from 'rxjs'; |
| 3 | +import {shareReplay, switchMap, take} from 'rxjs/operators'; |
5 | 4 | import * as yargs from 'yargs'; |
6 | | -import {scullyConfig} from '../utils/config'; |
| 5 | +import {loadConfig, scullyConfig} from '../utils/config'; |
| 6 | +import {httpGetJson} from '../utils/httpGetJson'; |
| 7 | +import {log} from '../utils/log'; |
| 8 | +import {waitForIt} from './puppeteerRenderPlugin'; |
7 | 9 |
|
8 | 10 | const {showBrowser} = yargs |
9 | 11 | .boolean('sb') |
10 | 12 | .alias('sb', 'showBrowser') |
11 | 13 | .describe('sb', 'Shows the puppeteer controlled browser').argv; |
12 | | - |
13 | | -/** use shareReplay so the browser will stay in memory during the lifetime of the program */ |
14 | | -const launched = obsBrowser().pipe(shareReplay({refCount: false, bufferSize: 1})); |
| 14 | +/** |
| 15 | + * Returns an Observable with that will fire with the launched puppeteer in there. |
| 16 | + */ |
| 17 | +const launched = from(loadConfig).pipe( |
| 18 | + /** give the system a bit of breathing room, and prevent race */ |
| 19 | + switchMap(() => from(waitForIt(500))), |
| 20 | + switchMap(() => obsBrowser()), |
| 21 | + /** use shareReplay so the browser will stay in memory during the lifetime of the program */ |
| 22 | + shareReplay({refCount: false, bufferSize: 1}) |
| 23 | +); |
15 | 24 | export const launchedBrowser: () => Promise<Browser> = () => launched.pipe(take(1)).toPromise(); |
16 | 25 | let browser: Browser; |
17 | 26 |
|
| 27 | +/** |
| 28 | + * Function that creates an observable with the puppeteer browser inside |
| 29 | + * @param options |
| 30 | + */ |
18 | 31 | function obsBrowser(options: LaunchOptions = scullyConfig.puppeteerLaunchOptions || {}): Observable<Browser> { |
19 | 32 | if (showBrowser) { |
20 | 33 | options.headless = false; |
21 | 34 | } |
| 35 | + options.args = options.args || []; |
22 | 36 | // options.args = ['--no-sandbox', '--disable-setuid-sandbox']; |
23 | 37 |
|
24 | 38 | const {SCULLY_PUPPETEER_EXECUTABLE_PATH} = process.env; |
@@ -56,8 +70,9 @@ function exitHandler(options, exitCode) { |
56 | 70 | browser = undefined; |
57 | 71 | } |
58 | 72 | if (exitCode || exitCode === 0) { |
59 | | - // console.log(exitCode) |
| 73 | + // console.log(exitCode); |
60 | 74 | } |
| 75 | + // TODO: kill the server here. (but only if started from scully, not when started from another process) |
61 | 76 | if (options.exit) { |
62 | 77 | process.exit(); |
63 | 78 | } |
|
0 commit comments