Skip to content

Commit

Permalink
Enable passing of a custom userDataDir to launcher
Browse files Browse the repository at this point in the history
* rename TMP_DIR to a more accurate name. Since this is a private
internal value, this is non-breaking.

Fixes #2291
  • Loading branch information
samccone committed May 25, 2017
1 parent 7fece8f commit 1f0d10f
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions chrome-launcher/chrome-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface Options {
port?: number;
handleSIGINT?: boolean;
chromePath?: string;
userDataDir?: string;
}

export interface LaunchedChrome {
Expand Down Expand Up @@ -72,7 +73,7 @@ export class Launcher {
private pollInterval: number = 500;
private pidFile: string;
private startingUrl: string;
private TMP_PROFILE_DIR: string;
private userDataDir: string;
private outFile?: number;
private errFile?: number;
private chromePath?: string;
Expand All @@ -82,19 +83,19 @@ export class Launcher {
port?: number;
pid?: number;

constructor(opts: Options = {}) {
constructor(private opts: Options = {}) {
// choose the first one (default)
this.startingUrl = defaults(opts.startingUrl, 'about:blank');
this.chromeFlags = defaults(opts.chromeFlags, []);
this.requestedPort = defaults(opts.port, 0);
this.chromePath = opts.chromePath;
this.startingUrl = defaults(this.opts.startingUrl, 'about:blank');
this.chromeFlags = defaults(this.opts.chromeFlags, []);
this.requestedPort = defaults(this.opts.port, 0);
this.chromePath = this.opts.chromePath;
}

private get flags() {
const flags = DEFAULT_FLAGS.concat([
`--remote-debugging-port=${this.port}`,
// Place Chrome profile in a custom location we'll rm -rf later
`--user-data-dir=${this.TMP_PROFILE_DIR}`
`--user-data-dir=${this.userDataDir}`
]);

if (process.platform === 'linux') {
Expand All @@ -113,16 +114,16 @@ export class Launcher {
throw new Error(`Platform ${platform} is not supported`);
}

this.TMP_PROFILE_DIR = makeTmpDir();
this.userDataDir = this.opts.userDataDir || makeTmpDir();

this.outFile = fs.openSync(`${this.TMP_PROFILE_DIR}/chrome-out.log`, 'a');
this.errFile = fs.openSync(`${this.TMP_PROFILE_DIR}/chrome-err.log`, 'a');
this.outFile = fs.openSync(`${this.userDataDir}/chrome-out.log`, 'a');
this.errFile = fs.openSync(`${this.userDataDir}/chrome-err.log`, 'a');

// fix for Node4
// you can't pass a fd to fs.writeFileSync
this.pidFile = `${this.TMP_PROFILE_DIR}/chrome.pid`;
this.pidFile = `${this.userDataDir}/chrome.pid`;

log.verbose('ChromeLauncher', `created ${this.TMP_PROFILE_DIR}`);
log.verbose('ChromeLauncher', `created ${this.userDataDir}`);

this.tmpDirandPidFileReady = true;
}
Expand Down Expand Up @@ -248,7 +249,10 @@ export class Launcher {
return new Promise(resolve => {
if (this.chrome) {
this.chrome.on('close', () => {
this.destroyTmp().then(resolve);
// Only clean up the tmp dir if we created it.
if (this.opts.userDataDir === undefined) {
this.destroyTmp().then(resolve);
}
});

log.log('ChromeLauncher', 'Killing all Chrome Instances');
Expand All @@ -272,11 +276,11 @@ export class Launcher {

private destroyTmp() {
return new Promise(resolve => {
if (!this.TMP_PROFILE_DIR) {
if (!this.userDataDir) {
return resolve();
}

log.verbose('ChromeLauncher', `Removing ${this.TMP_PROFILE_DIR}`);
log.verbose('ChromeLauncher', `Removing ${this.userDataDir}`);

if (this.outFile) {
fs.closeSync(this.outFile);
Expand All @@ -288,7 +292,7 @@ export class Launcher {
delete this.errFile;
}

rimraf(this.TMP_PROFILE_DIR, () => resolve());
rimraf(this.userDataDir, () => resolve());
});
}
};

0 comments on commit 1f0d10f

Please sign in to comment.