add hasOverview and hasChangelog to Addon manager

pull/67/head
dperolio 3 years ago
parent ab67ba7cde
commit 2b935362e7
No known key found for this signature in database
GPG Key ID: 3E9BBAA710D3DDCE

@ -11,7 +11,7 @@ import { open as openModal } from '@vizality/modal';
import { isArray } from '@vizality/util/array';
import { Avatars, Events as _Events } from '@vizality/constants';
import http from 'isomorphic-git/http/node';
import { join, resolve, sep } from 'path';
import { join, resolve, sep, extname } from 'path';
import { clone } from 'isomorphic-git';
import { Messages } from '@vizality/i18n';
import { watch } from 'chokidar';
@ -138,12 +138,40 @@ export default class AddonManager extends Events {
hasScreenshots (addonId) {
try {
const addon = this.get(addonId);
return Boolean(addon?.screenshots);
return Boolean(addon?.manifest?.screenshots);
} catch (err) {
return this._error(`An error occurred while checking for screenshots for "${addonId}"!`, err);
}
}
/**
*
* @param {string} addonId Addon ID
* @returns
*/
hasOverview (addonId) {
try {
const addon = this.get(addonId);
return Boolean(addon?.sections?.overview);
} catch (err) {
return this._error(`An error occurred while checking for overview for "${addonId}"!`, err);
}
}
/**
*
* @param {string} addonId Addon ID
* @returns
*/
hasChangelog (addonId) {
try {
const addon = this.get(addonId);
return Boolean(addon?.sections?.changelog);
} catch (err) {
return this._error(`An error occurred while checking for changelog for "${addonId}"!`, err);
}
}
/**
*
* @returns
@ -228,6 +256,7 @@ export default class AddonManager extends Events {
});
this._setAddonIcon(addonId, manifest);
this._setScreenshotImages(addonId, manifest);
this._items.set(addonId, new Addon());
} catch (err) {
return this._error(`An error occurred while initializing "${addonId}"!`, err);
@ -664,6 +693,38 @@ export default class AddonManager extends Events {
}
}
/**
*
*/
_setScreenshotImages (addonId, manifest) {
try {
const validExtensions = [ '.png', '.jpg', '.jpeg', '.gif', '.webp' ];
const screenshotUrls = [];
if (isArray(manifest.screenshots)) {
manifest.screenshot.forEach(screenshot => {
if (!validExtensions.some(ext => screenshot.endsWith(ext))) {
this._warn(`${toTitleCase(this.type)} screenshots must be of type .png, .jpg, .jpeg, .gif, or .webp`);
} else {
screenshotUrls.push(`vz-${this.type}://${addonId}/${screenshot}`)
}
});
} else {
const screenshotsDir = join(this.dir, addonId, 'screenshots');
const hasScreenshots = existsSync(screenshotsDir) && lstatSync(screenshotsDir)?.isDirectory();
if (hasScreenshots) {
const previewImages = [];
const validExtensions = [ '.png', '.gif', '.jpg', '.jpeg', '.webp' ];
readdirSync(screenshotsDir)
.filter(file => validExtensions.indexOf(extname(file).toLowerCase()) !== -1)
.map(file => previewImages.push(`vz-${this.type}://${addonId}/screenshots/${file}`));
return manifest.screenshots = previewImages;
}
}
} catch (err) {
return this._error(err);
}
}
/**
* Enables the addon directory watcher.
* @private

Loading…
Cancel
Save