Skip to content

Commit

Permalink
fix(puppet): clean data dir on close
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Jul 1, 2021
1 parent 9df1e90 commit 6c2b85f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Expand Up @@ -57,14 +57,12 @@ export function configureBrowserLaunchArgs(
);

if (options.showBrowser) {
engine.launchArguments.push(
`--user-data-dir=${Path.join(
os.tmpdir(),
engine.fullVersion.replace('.', '-'),
'-data',
String((sessionDirCounter += 1)),
)}`,
); // required to allow multiple browsers to be headed
const dataDir = Path.join(
os.tmpdir(),
engine.fullVersion.replace('.', '-'),
`${String(Date.now()).substr(0, 10)}-${(sessionDirCounter += 1)}`,
);
engine.launchArguments.push(`--user-data-dir=${dataDir}`); // required to allow multiple browsers to be headed

if (!options.disableDevtools) engine.launchArguments.push('--auto-open-devtools-for-tabs');
} else {
Expand Down
7 changes: 7 additions & 0 deletions puppet/lib/launchProcess.ts
Expand Up @@ -21,6 +21,7 @@ import * as Path from 'path';
import Log from '@secret-agent/commons/Logger';
import ILaunchedProcess from '@secret-agent/interfaces/ILaunchedProcess';
import Resolvable from '@secret-agent/commons/Resolvable';
import * as Fs from 'fs';
import { WebSocketTransport } from './WebSocketTransport';

const { log } = Log(module);
Expand All @@ -44,6 +45,11 @@ export default async function launchProcess(
env,
stdio,
});

const dataDir = processArguments
.find(x => x.startsWith('--user-data-dir='))
?.split('--user-data-dir=')
?.pop();
// Prevent Unhandled 'error' event.
launchedProcess.on('error', () => {});

Expand Down Expand Up @@ -109,6 +115,7 @@ export default async function launchProcess(
} else {
launchedProcess.kill('SIGKILL');
}
if (dataDir) Fs.rmdirSync(dataDir, { recursive: true });
return closed;
} catch (error) {
// might have already been kill off
Expand Down

0 comments on commit 6c2b85f

Please sign in to comment.