chore: optionally create downloads folder (#2188)

This commit is contained in:
Pavel Feldman 2020-05-11 14:42:13 -07:00 committed by GitHub
parent 8c083486a0
commit 9895cd0a31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 11 deletions

View File

@ -46,9 +46,9 @@ export class CRBrowser extends BrowserBase {
private _tracingClient: CRSession | undefined;
readonly _isHeadful: boolean;
static async connect(transport: ConnectionTransport, isPersistent: boolean, logger: InnerLogger, options: { slowMo?: number, headless?: boolean } = {}): Promise<CRBrowser> {
static async connect(transport: ConnectionTransport, isPersistent: boolean, logger: InnerLogger, options: { slowMo?: number, headless?: boolean, viewport?: types.Size | null } = {}): Promise<CRBrowser> {
const connection = new CRConnection(SlowMoTransport.wrap(transport, options.slowMo), logger);
const browser = new CRBrowser(connection, logger, isPersistent, !options.headless);
const browser = new CRBrowser(connection, logger, { persistent: isPersistent, headful: !options.headless, viewport: options.viewport });
const session = connection.rootSession;
if (!isPersistent) {
await session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true });
@ -83,14 +83,14 @@ export class CRBrowser extends BrowserBase {
return browser;
}
constructor(connection: CRConnection, logger: InnerLogger, isPersistent: boolean, isHeadful: boolean) {
constructor(connection: CRConnection, logger: InnerLogger, options: { headful?: boolean, persistent?: boolean, viewport?: types.Size | null } = {}) {
super(logger);
this._connection = connection;
this._session = this._connection.rootSession;
if (isPersistent)
this._defaultContext = new CRBrowserContext(this, null, validateBrowserContextOptions({}));
this._isHeadful = isHeadful;
if (options.persistent)
this._defaultContext = new CRBrowserContext(this, null, validateBrowserContextOptions({ viewport: options.viewport }));
this._isHeadful = !!options.headful;
this._connection.on(ConnectionEvents.Disconnected, () => {
for (const context of this._contexts.values())
context._browserClosed();

View File

@ -375,7 +375,7 @@ class FrameSession {
}
async _initialize(hasUIWindow: boolean) {
if (hasUIWindow) {
if (hasUIWindow && this._crPage._browserContext._options.viewport !== null) {
const { windowId } = await this._client.send('Browser.getWindowForTarget');
this._windowId = windowId;
}

View File

@ -70,7 +70,7 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
}
// 3. Install missing browsers for this package.
const myBrowsers = JSON.parse((await fsReadFileAsync(path.join(packagePath, 'browsers.json'))).toString())['browsers'];
const myBrowsers = JSON.parse((await fsReadFileAsync(path.join(packagePath, 'browsers.json'))).toString())['browsers'] as browserPaths.BrowserDescriptor[];
for (const browser of myBrowsers) {
const browserPath = browserPaths.browserDirectory(browsersPath, browser);
await browserFetcher.downloadBrowserWithProgressBar(browserPath, browser);

View File

@ -56,6 +56,9 @@ export type LaunchProcessOptions = {
pipe?: boolean,
tempDir?: string,
cwd?: string,
omitDownloads?: boolean,
// Note: attemptToGracefullyClose should reject if it does not close the browser.
attemptToGracefullyClose: () => Promise<any>,
onkill: (exitCode: number | null, signal: string | null) => void,
@ -81,7 +84,8 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
// @see https://nodejs.org/api/child_process.html#child_process_options_detached
detached: process.platform !== 'win32',
env: (options.env as {[key: string]: string}),
stdio
cwd: options.cwd,
stdio,
}
);
if (!spawnedProcess.pid) {
@ -104,7 +108,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
logger._log(browserStdErrLog, data);
});
const downloadsPath = await mkdtempAsync(DOWNLOADS_FOLDER);
const downloadsPath = options.omitDownloads ? '' : await mkdtempAsync(DOWNLOADS_FOLDER);
let processClosed = false;
const waitForProcessToClose = new Promise((fulfill, reject) => {
@ -115,7 +119,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
options.onkill(exitCode, signal);
// Cleanup as processes exit.
Promise.all([
removeFolderAsync(downloadsPath),
options.omitDownloads ? Promise.resolve() : removeFolderAsync(downloadsPath),
options.tempDir ? removeFolderAsync(options.tempDir) : Promise.resolve()
]).catch((err: Error) => console.error(err)).then(fulfill);
});