Skip to content

Commit

Permalink
Partially implemented downloadPackage as part of CliProviderImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
ebkr committed Nov 14, 2023
1 parent a098ab9 commit 75b3985
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/cli/CliProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ export type CliStateCallback = {

export interface CliProvider {

isPackageInCache: (packageName: string, version: VersionNumber) => boolean;
hasPackageExtractedSuccessfully: (packageName: string, version: VersionNumber) => boolean;
isPackageInCache: (packageName: string, version: VersionNumber) => Promise<boolean>;
hasPackageExtractedSuccessfully: (packageName: string, version: VersionNumber) => Promise<boolean>;
// Returns an array of package full names to install
// EG: author-modname-1.2.3
getRequiredPackageUpdates: (profile: Profile) => string[];
getRequiredPackageUpdates: (profile: Profile) => Promise<string[]>;

downloadPackage: (
packageName: string,
version: VersionNumber,
downloadEventManager: EventManager<CliProgressCallback>,
writeToDiskEventManager: () => EventManager<CliProgressCallback>,
successStateManager: () => EventManager<CliStateCallback>
) => void;
writeToDiskEventManager: EventManager<CliProgressCallback>,
successStateManager: EventManager<CliStateCallback>
) => Promise<void>;

installPackage: (
packageName: string,
version: VersionNumber,
writeToDiskEventManager: () => EventManager<CliProgressCallback>,
successStateManager: () => EventManager<CliStateCallback>
) => void;
) => Promise<void>;

}
34 changes: 34 additions & 0 deletions src/cli/CliProviderImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CliProgressCallback, CliProvider, CliStateCallback } from 'src/cli/CliProvider';
import VersionNumber from 'src/model/VersionNumber';
import { EventManager } from 'src/cli/EventManager';
import Profile from 'src/model/Profile';

export default class CliProviderImpl implements CliProvider {

downloadPackage(packageName: string, version: VersionNumber, downloadEventManager: EventManager<CliProgressCallback>, writeToDiskEventManager: EventManager<CliProgressCallback>, successStateManager: EventManager<CliStateCallback>): Promise<void> {
return Promise.reject("Not yet implemented");
}

getRequiredPackageUpdates(profile: Profile): Promise<string[]> {
return Promise.reject("Not yet implemented");
}

hasPackageExtractedSuccessfully(packageName: string, version: VersionNumber): Promise<boolean> {
return Promise.reject("Not yet implemented");
}

installPackage(
packageName: string,
version: VersionNumber,
writeToDiskEventManager: () => EventManager<CliProgressCallback>,
successStateManager: () => EventManager<CliStateCallback>)
: Promise<void>
{
return Promise.reject("Not yet implemented");
}

isPackageInCache(packageName: string, version: VersionNumber): Promise<boolean> {
return Promise.reject("Not yet implemented");
}

}
54 changes: 54 additions & 0 deletions src/cli/PackageDownloader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import VersionNumber from 'src/model/VersionNumber';
import { EventManager } from 'src/cli/EventManager';
import { CliProgressCallback, CliStateCallback } from 'src/cli/CliProvider';
import ThunderstorePackages from 'src/r2mm/data/ThunderstorePackages';
import R2Error from 'src/model/errors/R2Error';
import axios from 'axios';

export default class PackageDownloader {

public downloadPackage(
packageName: string,
version: VersionNumber,
downloadEventManager: EventManager<CliProgressCallback>,
writeToDiskEventManager: EventManager<CliProgressCallback>,
successStateManager: EventManager<CliStateCallback>)
: void {

const packageMap = ThunderstorePackages.PACKAGES_MAP;
if (!packageMap.has(packageName)) {
successStateManager.publish({
finished: false,
error: new R2Error(`${packageName} not found`, `Unable to find package ${packageName}`, 'Refresh the mod list via the settings screen or try again later')
} as CliStateCallback);
return;
}

const pkg = packageMap.get(packageName)!;
const pkgVersion = pkg.getVersions()
.find(pv => pv.getVersionNumber().isEqualTo(version));

if (pkgVersion === undefined) {
successStateManager.publish({
finished: false,
error: new R2Error(`${packageName}-${version.toString()} not found`, `No version of ${packageName} matching ${version.toString()} was found`, 'Refresh the mod list via the settings screen or try again later')
} as CliStateCallback);
return;
}

axios.get(pkgVersion.getDownloadUrl(), {
onDownloadProgress: progress => {
downloadEventManager.publish({
currentBytes: progress.loaded,
totalBytes: progress.total
} as CliProgressCallback);
},
responseType: 'arraybuffer',
headers: {
'Content-Type': 'application/zip',
'Access-Control-Allow-Origin': '*'
}
})
}

}
3 changes: 3 additions & 0 deletions src/pages/Manager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ import LocalFileImportModal from '../components/importing/LocalFileImportModal.v
import { PackageLoader } from '../model/installing/PackageLoader';
import GameInstructions from '../r2mm/launching/instructions/GameInstructions';
import CategoryFilterModal from '../components/modals/CategoryFilterModal.vue';
import ThunderstorePackages from 'src/r2mm/data/ThunderstorePackages';
@Component({
components: {
Expand Down Expand Up @@ -671,6 +672,8 @@ import CategoryFilterModal from '../components/modals/CategoryFilterModal.vue';
.catch(this.showError);
});
console.log("TS package map", ThunderstorePackages.PACKAGES_MAP);
this.isManagerUpdateAvailable();
}
}
Expand Down

0 comments on commit 75b3985

Please sign in to comment.