Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/install/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@
import fs from 'fs';
import path from 'path';
import util from 'util';
import removeFolder from 'rimraf';
import lockfile from 'proper-lockfile';
import {Registry, allBrowserNames, isBrowserDirectory, BrowserName, registryDirectory} from '../utils/registry';
import * as browserFetcher from './browserFetcher';
import { getAsBooleanFromENV, calculateSha1 } from '../utils/utils';
import { getAsBooleanFromENV, calculateSha1, removeFolders } from '../utils/utils';

const fsMkdirAsync = util.promisify(fs.mkdir.bind(fs));
const fsReaddirAsync = util.promisify(fs.readdir.bind(fs));
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
const fsExistsAsync = (filePath: string) => fsReadFileAsync(filePath).then(() => true).catch(e => false);
const fsUnlinkAsync = util.promisify(fs.unlink.bind(fs));
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
const removeFolderAsync = util.promisify(removeFolder);

const PACKAGE_PATH = path.join(__dirname, '..', '..');

Expand Down Expand Up @@ -101,10 +99,9 @@ async function validateCache(linksDir: string, browserNames: BrowserName[]) {
const directories = new Set<string>(downloadedBrowsers);
for (const browserDirectory of usedBrowserPaths)
directories.delete(browserDirectory);
for (const directory of directories) {
for (const directory of directories)
browserFetcher.logPolitely('Removing unused browser at ' + directory);
await removeFolderAsync(directory).catch(e => {});
}
await removeFolders([...directories]);
}

// 3. Install missing browsers for this package.
Expand Down
10 changes: 0 additions & 10 deletions src/server/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
*/

import { EventEmitter } from 'events';
import removeFolder from 'rimraf';
import util from 'util';
import * as types from './types';
import { Progress } from './progress';
import { debugLogger } from '../utils/debugLogger';

const removeFolderAsync = util.promisify(removeFolder);

export type RegisteredListener = {
emitter: EventEmitter;
eventName: (string | symbol);
Expand Down Expand Up @@ -77,12 +73,6 @@ class Helper {
return null;
}

static async removeFolders(dirs: string[]) {
await Promise.all(dirs.map(dir => {
return removeFolderAsync(dir).catch((err: Error) => console.error(err));
}));
}

static waitForEvent(progress: Progress | null, emitter: EventEmitter, event: string | symbol, predicate?: Function): { promise: Promise<any>, dispose: () => void } {
const listeners: RegisteredListener[] = [];
const promise = new Promise((resolve, reject) => {
Expand Down
11 changes: 3 additions & 8 deletions src/server/processLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

import * as childProcess from 'child_process';
import * as readline from 'readline';
import * as removeFolder from 'rimraf';
import { helper } from './helper';
import * as types from './types';
import { isUnderTest } from '../utils/utils';
import { isUnderTest, removeFolders } from '../utils/utils';

export type Env = {[key: string]: string | number | boolean | undefined};

Expand Down Expand Up @@ -62,7 +61,7 @@ if (maxListeners !== 0)
process.setMaxListeners(Math.max(maxListeners || 0, 100));

export async function launchProcess(options: LaunchProcessOptions): Promise<LaunchResult> {
const cleanup = () => helper.removeFolders(options.tempDirectories);
const cleanup = () => removeFolders(options.tempDirectories);

const stdio: ('ignore' | 'pipe')[] = options.stdio === 'pipe' ? ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'] : ['pipe', 'pipe', 'pipe'];
options.log(`<launching> ${options.executablePath} ${options.args.join(' ')}`);
Expand Down Expand Up @@ -172,11 +171,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
// the process might have already stopped
}
}
try {
// Attempt to remove temporary directories to avoid littering.
for (const dir of options.tempDirectories)
removeFolder.sync(dir);
} catch (e) { }
cleanup();
}

function killAndWait() {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import path from 'path';
import fs from 'fs';
import removeFolder from 'rimraf';
import * as util from 'util';
import * as crypto from 'crypto';

Expand Down Expand Up @@ -145,3 +146,15 @@ export function calculateSha1(buffer: Buffer | string): string {
export function createGuid(): string {
return crypto.randomBytes(16).toString('hex');
}

export async function removeFolders(dirs: string[]) {
await Promise.all(dirs.map((dir: string) => {
return new Promise<void>(fulfill => {
removeFolder(dir, { maxBusyTries: 10 }, error => {
if (error)
console.error(error); // eslint-disable no-console
fulfill();
});
});
}));
}
1 change: 1 addition & 0 deletions test/defaultbrowsercontext-2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,5 @@ it('should connect to a browser with the default page', (test, { mode }) => {
const options = { ...browserOptions, __testHookOnConnectToBrowser: () => new Promise(f => setTimeout(f, 3000)) };
const context = await browserType.launchPersistentContext(await createUserDataDir(), options);
expect(context.pages().length).toBe(1);
await context.close();
});
4 changes: 2 additions & 2 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import { folio as httpFolio } from './http.fixtures';
import { folio as playwrightFolio } from './playwright.fixtures';
import { PlaywrightClient } from '../lib/remote/playwrightClient';
import { start } from '../lib/outofprocess';
import { removeFolders } from '../lib/utils/utils';
export { expect, config } from 'folio';

const removeFolderAsync = util.promisify(require('rimraf'));
const mkdtempAsync = util.promisify(fs.mkdtemp);

const getExecutablePath = browserName => {
Expand Down Expand Up @@ -69,7 +69,7 @@ fixtures.createUserDataDir.init(async ({ }, run) => {
return dir;
}
await run(createUserDataDir);
await Promise.all(dirs.map(dir => removeFolderAsync(dir).catch(e => { })));
await removeFolders(dirs);
});

fixtures.launchPersistent.init(async ({ createUserDataDir, browserOptions, browserType }, run) => {
Expand Down