|
| 1 | +/* eslint-disable no-await-in-loop */ |
1 | 2 | import { spawn } from 'child_process'; |
2 | 3 | import { createWriteStream } from 'fs'; |
3 | 4 | import { writeFile, readFile, mkdir, rename, stat, rm } from 'fs/promises'; |
4 | 5 | import path, { normalize } from 'path'; |
5 | | -import { dialog, BrowserWindow, shell, Notification } from 'electron'; |
6 | | -import log from 'electron-log/main'; |
| 6 | +import { setTimeout } from 'timers/promises'; |
| 7 | +import { uuid } from '@tanstack/react-form'; |
| 8 | +import { load } from 'cheerio'; |
| 9 | +import { dialog, BrowserWindow, shell, Notification, session, net } from 'electron'; |
| 10 | +import log from 'electron-log'; |
7 | 11 | import Store from 'electron-store'; |
8 | 12 | import JSZip from 'jszip'; |
9 | 13 | import Loki from 'lokijs'; |
10 | 14 | import StreamZip from 'node-stream-zip'; |
11 | | -import { BundleMetaFile, StoreSchema, MainIpcGetter, previewTypes } from '../../shared/constants'; |
| 15 | +import { |
| 16 | + BundleMetaFile, |
| 17 | + StoreSchema, |
| 18 | + MainIpcGetter, |
| 19 | + previewTypes, |
| 20 | + HUMBLE_PARTITION, |
| 21 | + ImportType, |
| 22 | + LoginProvider, |
| 23 | +} from '../../shared/constants'; |
12 | 24 | import { addTask } from '../managers/task-manager'; |
13 | | -import { FilePath, getProjectDir, getRandom, ignoredFilesMatch } from '../util'; |
| 25 | +import { FilePath, foreachAsync, getProjectDir, getRandom, ignoredFilesMatch } from '../util'; |
14 | 26 | import { |
15 | 27 | findBundlePath, |
16 | 28 | forAllAssetsInProject, |
@@ -219,21 +231,24 @@ export async function getVirtualBundles( |
219 | 231 | bundle: b, |
220 | 232 | isVirtual: true, |
221 | 233 | date: new Date(b.date), |
| 234 | + sourceType: b.sourceType, |
222 | 235 | }) satisfies BundleInfo as BundleInfo, |
223 | 236 | ); |
224 | 237 | } |
225 | 238 |
|
226 | 239 | export async function getBundles( |
227 | 240 | store: Store<StoreSchema>, |
228 | | - virtualBundles: Collection<VirtualBundle>, |
| 241 | + virtualBundles?: Collection<VirtualBundle>, |
229 | 242 | ): Promise<BundleInfo[]> { |
230 | 243 | const bundles: BundleInfo[] = []; |
231 | 244 | const projectDir = getProjectDir(store); |
232 | 245 | if (projectDir) { |
233 | 246 | await findChildrenBundles(new FilePath(projectDir, ''), bundles); |
234 | 247 | } |
235 | 248 |
|
236 | | - bundles.push(...(await getVirtualBundles(virtualBundles))); |
| 249 | + if (virtualBundles) { |
| 250 | + bundles.push(...(await getVirtualBundles(virtualBundles))); |
| 251 | + } |
237 | 252 |
|
238 | 253 | return bundles.sort((a, b) => b.date?.getTime() - a.date?.getTime()); |
239 | 254 | } |
@@ -369,9 +384,18 @@ async function exportBundle( |
369 | 384 | } |
370 | 385 | } |
371 | 386 |
|
| 387 | +async function getHumbleCookieHeader(): Promise<string> { |
| 388 | + const cookies = await session.defaultSession.cookies.get({ |
| 389 | + domain: '.humblebundle.com', |
| 390 | + }); |
| 391 | + |
| 392 | + return cookies.map((c) => `${c.name}=${c.value}`).join('; '); |
| 393 | +} |
| 394 | + |
372 | 395 | /* ----------------------------- Initialize API ----------------------------- */ |
373 | 396 |
|
374 | 397 | export default function InitializeBundlesApi( |
| 398 | + importers: Record<LoginProvider, BundleImporter>, |
375 | 399 | api: MainIpcGetter, |
376 | 400 | store: Store<StoreSchema>, |
377 | 401 | db: Loki, |
@@ -447,6 +471,7 @@ export default function InitializeBundlesApi( |
447 | 471 | bundle: virtualBundle as Bundle, |
448 | 472 | previewUrl: virtualBundle.previewUrl, |
449 | 473 | name: virtualBundle.name, |
| 474 | + sourceType: virtualBundle.sourceType, |
450 | 475 | } as BundleInfo; |
451 | 476 | } |
452 | 477 | return tryGetBundleEntryFromFolderPath(FilePath.fromStore(store, id)); |
@@ -502,6 +527,15 @@ export default function InitializeBundlesApi( |
502 | 527 | } |
503 | 528 |
|
504 | 529 | api.updateBundle = updateBundle; |
| 530 | + api.importBundles = async (type) => { |
| 531 | + const importer = importers[type]; |
| 532 | + if (importer) { |
| 533 | + return addTask('Importing Bundles', async (abort, progress) => |
| 534 | + importer.import(abort, progress), |
| 535 | + ); |
| 536 | + } |
| 537 | + throw new Error(`No Importer of type ${type}`); |
| 538 | + }; |
505 | 539 | api.getBundles = () => getBundles(store, virtualBundles); |
506 | 540 | api.getHomeBundles = getHomeBundles; |
507 | 541 | api.createVirtualBundle = createVirtualBundle; |
|
0 commit comments