Skip to content

Commit

Permalink
feat: add errors and promise to SingletonLock
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Jul 21, 2023
1 parent bb9c2db commit 25b3fa8
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import * as ChromeLauncher from 'chrome-launcher';
import chromeVersion from 'chrome-version';
import * as fs from 'fs';
import * as path from 'path';
import { Browser, BrowserContext, Page, LaunchOptions } from 'puppeteer';
import {
Browser,
BrowserContext,
Page,
LaunchOptions,
PuppeteerLaunchOptions
} from 'puppeteer';
import puppeteer from 'puppeteer-extra';
import { options } from '../config';
import { CreateConfig } from '../config/create-config';
Expand Down Expand Up @@ -485,8 +491,7 @@ export async function initBrowser(
if (options.browserWS && options.browserWS !== '') {
return await puppeteer.connect({ browserWSEndpoint: options.browserWS });
} else {
console.log('aqui');
removeStoredSingletonLock(options.puppeteerOptions);
await removeStoredSingletonLock(options.puppeteerOptions);
return await puppeteer.launch(launchOptions);
}
} catch (e) {
Expand Down Expand Up @@ -594,14 +599,26 @@ function isChromeInstalled(executablePath: string): boolean {
}
}

function removeStoredSingletonLock(puppeteerOptions): boolean {
const platform = os.platform();
const { userDataDir } = puppeteerOptions;
try {
if (platform === 'win32') return false;
fs.unlinkSync(`${userDataDir}/SingletonLock`);
true;
} catch {
false;
}
function removeStoredSingletonLock(
puppeteerOptions: PuppeteerLaunchOptions
): Promise<boolean> {
return new Promise((resolve, reject) => {
const platform = os.platform();
const { userDataDir } = puppeteerOptions;
const singletonLockPath = path.join(process.cwd(), userDataDir, 'SingletonLock');

if (platform === 'win32') {
// No need to remove the lock on Windows, so resolve with true directly.
resolve(true);
} else {
fs.unlink(singletonLockPath, (error) => {
if (error) {
console.error('Error removing SingletonLock:', error);
reject(false);
} else {
resolve(true);
}
});
}
});
}

1 comment on commit 25b3fa8

@jonalan7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.