Skip to content

Commit

Permalink
Replace download-chromium with puppeteer-core
Browse files Browse the repository at this point in the history
  • Loading branch information
baruchiro committed Jan 1, 2021
1 parent 423f071 commit 359ac7f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 257 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
"scripts": {
"serve": "vue-cli-service electron:serve",
"build": "vue-cli-service electron:build",

"postinstall": "electron-builder install-app-deps",
"postuninstall": "electron-builder install-app-deps",
"prepare": "node scripts/copyCategoryCalculationScript.js",

"lint": "eslint --ext .js,.vue,.ts -f ./node_modules/eslint-friendly-formatter src test",
"lint:fix": "yarn lint --fix",
"typeCheck": "tsc --noEmit",

"unit": "vue-cli-service test:unit",
"e2e": "vue-cli-service test:unit --config='test/e2e/jest.e2e.config.js'",
"test": "yarn unit && yarn e2e"
Expand All @@ -41,14 +38,14 @@
"csv-parse": "^4.14.1",
"csv-stringify": "^5.5.3",
"direct-vuex": "^0.12.0",
"download-chromium": "^2.2.0",
"electron-log": "^4.1.1",
"emittery": "^0.7.1",
"googleapis": "^59.0.0",
"israeli-bank-scrapers-core": "^1.0.1",
"keytar": "^5.2.0",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"puppeteer-core": "^5.5.0",
"vue": "^2.6.10",
"vue-router": "^3.1.6",
"vuetify": "^2.2.22",
Expand All @@ -65,6 +62,7 @@
"@types/jest": "^26.0.10",
"@types/lodash": "^4.14.158",
"@types/node": "12",
"@types/puppeteer-core": "^5.4.0",
"@types/webdriverio": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.2.0",
Expand Down
6 changes: 5 additions & 1 deletion src/backend/eventEmitters/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export enum AccountType {
}

export enum AccountStatus {
IDLE, PENDING, IN_PROGRESS, DONE, ERROR
IDLE = 'IDLE',
PENDING = 'PENDING',
IN_PROGRESS = 'IN_PROGRESS',
DONE = 'DONE',
ERROR = 'ERROR'
}

export class BudgetTrackingEvent {
Expand Down
30 changes: 17 additions & 13 deletions src/backend/import/downloadChromium.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import download, { OnProgress } from 'download-chromium';
import { getPuppeteerConfig } from 'israeli-bank-scrapers-core';
import puppeteer from 'puppeteer-core';

const getIntegerPercent = (callback: OnProgress): OnProgress => {
type PuppeteerProgressCallback = (downloadBytes: number, totalBytes: number) => void
type PercentCallback = (percent: number) => void

const getIntegerPercent = (callback: PercentCallback): PuppeteerProgressCallback => {
let prevPercent = -1;

return ({ percent, ...rest }) => {
const p = Math.floor(percent * 10);
return (downloadBytes: number, totalBytes: number) => {
const p = Math.floor((downloadBytes / totalBytes) * 100);
if (p > prevPercent) {
prevPercent = p;
callback({ percent: p, ...rest });
callback(p);
}
};
};

const revision = getPuppeteerConfig().chromiumRevision;

let downloadProm: ReturnType<typeof download>;
let downloadProm: ReturnType<typeof downloadChromium> | null = null;

export default async function downloadChromium(installPath?: string, onProgress?: OnProgress) {
export default async function downloadChromium(installPath?: string, onProgress?: PercentCallback): Promise<string> {
if (downloadProm) return downloadProm;

downloadProm = download({
revision,
installPath,
onProgress: onProgress && getIntegerPercent(onProgress),
log: true
});
const progressCallback = onProgress && getIntegerPercent(onProgress);

const browserFetcher = puppeteer.createBrowserFetcher({ path: installPath });
downloadProm = browserFetcher.download(revision, progressCallback)
.then(({ executablePath }) => executablePath);

downloadProm.then(() => downloadProm = null);

return downloadProm;
}
2 changes: 1 addition & 1 deletion src/backend/import/importTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TRANSACTION_STATUS_COMPLETED = 'completed';
export async function scrapeFinancialAccountsAndFetchTransactions(scrapingConfig: ScrapingConfig, startDate: Date, eventPublisher: EventPublisher) {
const companyIdToTransactions: Record<string, EnrichedTransaction[]> = {};

const dowloadedChrome = await getChrome(userDataPath, ({ percent }) => emitChromeDownload(eventPublisher, percent));
const dowloadedChrome = await getChrome(userDataPath, (percent) => emitChromeDownload(eventPublisher, percent));

const accountsToScrape = scrapingConfig.accountsToScrape.filter((accountToScrape) => accountToScrape.active !== false);
const scrapingPromises = accountsToScrape.map(async (accountToScrape) => {
Expand Down
Loading

0 comments on commit 359ac7f

Please sign in to comment.