Skip to content

Commit

Permalink
fix: clean puppeteer_dev_chrome_profile
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-humann committed Jul 25, 2023
1 parent af43406 commit 39c3f05
Show file tree
Hide file tree
Showing 3 changed files with 1,094 additions and 1,187 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"author": "Jean Humann <[email protected]> (https://github.com/jean-humann)",
"license": "MIT",
"dependencies": {
"@types/fs-extra": "^11.0.1",
"chalk": "^4.1.2",
"commander": "^11.0.0",
"console-stamp": "^3.1.1",
Expand Down
16 changes: 15 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chalk from 'chalk';
import console_stamp from 'console-stamp';
import * as puppeteer from 'puppeteer-core';
import { scrollPageToBottom } from 'puppeteer-autoscroll-down';
import * as fs from 'fs-extra';

console_stamp(console);

Expand Down Expand Up @@ -50,13 +51,17 @@ export async function generatePDF({
protocolTimeout,
}: GeneratePDFOptions): Promise<void> {
const execPath = process.env.PUPPETEER_EXECUTABLE_PATH ?? puppeteer.executablePath('chrome');
console.log(chalk.cyan(`Using Chromium from ${execPath}`));
console.debug(chalk.cyan(`Using Chromium from ${execPath}`));
const browser = await puppeteer.launch({
headless: 'new',
executablePath: execPath,
args: puppeteerArgs,
protocolTimeout: protocolTimeout,
});

const chromeTmpDataDir = browser.process()?.spawnargs.find((arg) => arg.startsWith('--user-data-dir'))?.split('=')[1] as string;
console.debug(chalk.cyan(`Chrome user data dir: ${chromeTmpDataDir}`));

const page = await browser.newPage();

for (const url of initialDocURLs) {
Expand Down Expand Up @@ -152,6 +157,15 @@ export async function generatePDF({
footerTemplate,
timeout: 0,
});

console.log(chalk.green(`PDF generated at ${outputPDFFilename}`));
await browser.close();
console.log(chalk.green('Browser closed'));

if (chromeTmpDataDir !== null) {
fs.removeSync(chromeTmpDataDir);
}
console.debug(chalk.cyan('Chrome user data dir removed'));
}

/**
Expand Down
Loading

0 comments on commit 39c3f05

Please sign in to comment.