Skip to content

Commit

Permalink
fix: add guard for resources commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Jul 30, 2019
1 parent 582d7dd commit c3db270
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/main/store/modules/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ function getRegularName(type, meta) {
switch (type) {
case 'forge':
fmeta = meta[0];
if (!fmeta) {
return undefined;
}
if (typeof (fmeta.name || fmeta.modid) !== 'string'
|| typeof fmeta.mcversion !== 'string'
|| typeof fmeta.version !== 'string') return undefined;
Expand Down Expand Up @@ -179,6 +182,12 @@ const mod = {
async init(context) {
context.dispatch('refreshResources');
},
async refreshResource(context, res) {
const resource = typeof res === 'string' ? context.getters.getResource(res) : res;
if (!resource) return;
const newOne = await parseResource(resource.path, resource.hash, resource.ext, await fs.readFile(resource.path), resource.source, resource.type);
context.commit('resource', newOne);
},
async refreshResources(context) {
const task = Task.create('refreshResource', async (ctx) => {
const modsDir = context.rootGetters.path('mods');
Expand All @@ -194,7 +203,6 @@ const mod = {
const touched = {};
const total = modsFiles.length + resourcePacksFiles.length + modpacksFiles.length;
let finished = 0;
const emptyResource = { path: '', name: '', hash: '', ext: '', metadata: {}, domain: '', type: '', source: { path: '', date: '' } };

/**
* @type {import('universal/store/modules/resource').Resource<any>[]}
Expand Down Expand Up @@ -270,7 +278,7 @@ const mod = {
}

if (resources.length > 0) {
context.commit('resources', resources.filter(resource => resource !== emptyResource));
context.commit('resources', resources.filter(resource => resource !== undefined));
}
console.log(`refreshed ${resources.length} resources`);
});
Expand Down
4 changes: 3 additions & 1 deletion src/universal/store/modules/resource.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ export declare namespace ResourceModule {
type C = Context<State, Getters, Mutations, Actions>;

interface Actions {
refreshResources(context: C): Promise<void>
refreshResources(context: C): Promise<void>;
deployResources(context: C, payload: { resources: Resource<any>[], profile: string }): Promise<void>;
readForgeLogo(context: C, id: string): Promise<string>;
removeResource(context: C, resource: string | AnyResource): Promise<void>;

refreshResource(context:C, resource: string | AnyResource): Promise<void>;

importResource(context: C, option: ImportOption): Promise<TaskHandle>;
exportResource(context: C, option: { resources: (string | AnyResource)[], targetDirectory: string }): Promise<void>;
}
Expand Down
22 changes: 13 additions & 9 deletions src/universal/store/modules/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ const mod = {
},
resources: (state, all) => {
for (const res of all) {
switch (res.domain) {
case 'mods':
case 'resourcepacks':
case 'saves':
case 'modpacks':
Vue.set(state[res.domain], res.hash, res);
break;
default:
console.error(`Cannot accept resource for unknown domain [${res.domain}]`);
if (!res) {
console.error('Cannot import undefined resource.');
} else {
switch (res.domain) {
case 'mods':
case 'resourcepacks':
case 'saves':
case 'modpacks':
Vue.set(state[res.domain], res.hash, res);
break;
default:
console.error(`Cannot accept resource for unknown domain [${res.domain}]`);
}
}
}
},
Expand Down

0 comments on commit c3db270

Please sign in to comment.