Professional Documents
Culture Documents
promises;
const sharp = require('sharp');
class ManifestService {
constructor(site) {
const baseTargetPath = GENERATED_FILES_PATHS.MANIFESTS;
if (!site) {
throw new Error('ManifestService: Missing site');
}
if (!site.config) {
throw new Error('ManifestService: Missing site config');
}
if (!site.config.options) {
throw new Error('ManifestService: Missing site options');
}
this.site = site;
this.siteId = site._id;
this.domain = site.domain;
this.siteOptions = site.config.options;
this.siteStyles = site.config.styles;
this.rootDirectory = `${baseTargetPath}/${this.siteId}`;
this.targetPath = `${baseTargetPath}/${site._id}.xml`;
}
async generate() {
try {
await fs.access(this.rootDirectory);
} catch (e) {
await fs.mkdir(this.rootDirectory, { recursive: true });
}
const manifest = {
theme_color: this.siteStyles.secondaryColor,
background_color: this.siteStyles.primaryColor,
display: 'standalone',
scope: '/',
start_url: '/',
name: this.siteOptions.siteTitle,
short_name: this.siteOptions.name,
description: this.siteOptions.siteDescription,
developer: {
name: 'MICA Project',
},
icons: iconSizes.map((size) => ({
src: `/icon-${size}x${size}.png`,
sizes: `${size}x${size}`,
type: 'image/png',
})),
};
try {
for (const size of iconSizes) {
await this.#generateFavicon(size);
}
} catch (error) {
console.error(error);
}
await fs.writeFile(
`${this.rootDirectory}/manifest.json`,
JSON.stringify(manifest, null, 2)
);
console.log(
`Manifest files for ${this.siteId} written at path : ${this.rootDirectory}`
);
}
async #generateFavicon(size) {
const imageService = new ImageService(this.siteId);
const completeFilePath = await imageService.getFullImagePath(
this.siteOptions.favicon?.file
);
await sharp(completeFilePath)
.resize(size, size)
.png()
.toFile(`${this.rootDirectory}/icon-${size}x${size}.png`);
return true;
}
}
module.exports = ManifestService;