Skip to content

Commit

Permalink
refactor: add option to deploy to set instance path
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Sep 3, 2020
1 parent 3eb438c commit 582b692
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/main/service/DiagnoseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export default class DiagnoseService extends Service {
if (!resource) {
throw new Error('Cannot find custom skin loader event we try to download it!');
}
await this.instanceResourceService.deploy([resource]);
await this.instanceResourceService.deploy({ resources: [resource] });
}
} else {
if (!fabricLoader) {
Expand All @@ -207,7 +207,7 @@ export default class DiagnoseService extends Service {
if (!resource) {
throw new Error('Cannot find custom skin loader event we try to download it!');
}
await this.instanceResourceService.deploy([resource]);
await this.instanceResourceService.deploy({ resources: [resource] });
}
}
},
Expand Down
18 changes: 13 additions & 5 deletions src/main/service/InstanceResourceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import { basename, join } from 'path';
import ResourceService from './ResourceService';
import Service, { Inject, MutationTrigger, Singleton } from './Service';

export interface DeployOptions {
resources: Resource[];
/**
* The instance path to deploy. This will be the current path by default.
*/
path?: string;
}

export default class InstanceResourceService extends Service {
@Inject('ResourceService')
private resourceService!: ResourceService;
Expand Down Expand Up @@ -118,12 +126,12 @@ export default class InstanceResourceService extends Service {
}
}

async deploy(resources: Resource[]) {
async deploy({ resources, path = this.state.instance.path }: DeployOptions) {
let promises: Promise<void>[] = [];
this.log(`Deploy ${resources.length} to ${this.state.instance.path}`);
this.log(`Deploy ${resources.length} to ${path}`);
for (let resource of resources) {
let path = join(this.state.instance.path, resource.domain, basename(resource.path));
promises.push(link(resource.path, path).catch(() => copyFile(resource.path, path)));
const resourcePath = join(path, resource.domain, basename(resource.path));
promises.push(link(resource.path, resourcePath).catch(() => copyFile(resource.path, resourcePath)));
}
await Promise.all(promises);
}
Expand All @@ -135,7 +143,7 @@ export default class InstanceResourceService extends Service {
const toBeDeploiedPacks = allPacks.filter(p => !deploiedPacks.find((r) => r.hash === p.hash));
this.log(`Deploying ${toBeDeploiedPacks.length} resource packs`);

await this.deploy(toBeDeploiedPacks);
await this.deploy({ resources: toBeDeploiedPacks });
}

async undeploy(resources: InstanceResource[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/hooks/useInstanceMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function useInstanceMods() {
const disabled = items.filter(m => !m.enabled);

return Promise.all([
deploy(enabled.map(m => m.resource)),
deploy({ resources: enabled.map(m => m.resource) }),
undeploy(disabled.map(m => m.resource as InstanceResource)),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/windows/main/hooks/useIssues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function useIssueHandler() {
register('requireFabricAPI', () => {
let fabric = resources.value.find((r) => r.type === 'fabric' && r.metadata.id === 'fabric');
if (fabric) {
deploy([fabric]);
deploy({ resources: [fabric] });
} else {
replace('/curseforge/mc-mods/306612');
}
Expand Down

0 comments on commit 582b692

Please sign in to comment.