From ac664e212e656f9e8880a0407ecb5ef8de425ba6 Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Wed, 18 Aug 2021 01:42:07 -0700 Subject: [PATCH] fix: disable broken app and add safeguards (#1859) * chore: disable snailfm app * fix: prevent disabled apps from being processed --- apps/snailfm/snailfm.yml | 1 + lib/raw-app-list.js | 21 ++++++++------ script/pack.js | 8 +++++- script/resize.js | 62 ++++++++++++++++++++++++++-------------- 4 files changed, 61 insertions(+), 31 deletions(-) diff --git a/apps/snailfm/snailfm.yml b/apps/snailfm/snailfm.yml index 2c02f0306db..e7a2ac89e70 100644 --- a/apps/snailfm/snailfm.yml +++ b/apps/snailfm/snailfm.yml @@ -7,3 +7,4 @@ keywords: - music - NCS - simple +disabled: true # broken icon diff --git a/lib/raw-app-list.js b/lib/raw-app-list.js index 4a3e82e1c67..71ddb5742ba 100644 --- a/lib/raw-app-list.js +++ b/lib/raw-app-list.js @@ -11,15 +11,20 @@ module.exports = function getSlugs() { .isDirectory() }) .sort() - .map((slug) => { + .reduce((slugs, slug) => { const yamlFile = path.join(__dirname, `../apps/${slug}/${slug}.yml`) - const app = Object.assign( - { + const meta = yaml.load(fs.readFileSync(yamlFile)) + + if (meta.disabled) { + return slugs + } else { + const app = { slug: slug, iconPath: path.join(__dirname, `../apps/${slug}/${slug}-icon.png`), - }, - yaml.load(fs.readFileSync(yamlFile)) - ) - return app - }) + ...meta + } + return [...slugs, app] + } + + }, []) } diff --git a/script/pack.js b/script/pack.js index 83387b15c92..e24d55dbb41 100755 --- a/script/pack.js +++ b/script/pack.js @@ -16,9 +16,15 @@ fs.readdirSync(path.join(__dirname, '../apps')) }) .forEach((slug) => { const yamlFile = path.join(__dirname, `../apps/${slug}/${slug}.yml`) + const meta = yaml.load(fs.readFileSync(yamlFile)) + + if (meta.disabled) { + return; + } + const app = Object.assign( { slug: slug }, - yaml.load(fs.readFileSync(yamlFile)), + meta, { icon: `${slug}-icon.png`, icon32: `${slug}-icon-32.png`, diff --git a/script/resize.js b/script/resize.js index 39cb4c4c65b..288c457f294 100755 --- a/script/resize.js +++ b/script/resize.js @@ -4,13 +4,9 @@ const fs = require('fs') const recursiveReadSync = require('recursive-readdir-sync') const imagemin = require('imagemin') const imageminPngquant = require('imagemin-pngquant') -const icons = recursiveReadSync(path.join(__dirname, '../apps')).filter( - (file) => file.match(/icon\.png/) -) +const yaml = require('js-yaml') -process.stdout.write(`Resizing ${icons.length} icons...`) - -function resize(file, size) { +async function resize(file, size) { const newFile = file.replace('.png', `-${size}.png`) // skip files that are up to date @@ -30,19 +26,41 @@ function resize(file, size) { .then((buf) => fs.writeFileSync(newFile, buf)) } -const resizes = icons - .map((icon) => resize(icon, 32)) - .concat(icons.map((icon) => resize(icon, 64))) - .concat(icons.map((icon) => resize(icon, 128))) - .concat(icons.map((icon) => resize(icon, 256))) - -Promise.all(resizes) - .then(function (results) { - process.stdout.write(` Done.`) - process.exit() - }) - .catch(function (err) { - console.error('Error resizing icons!') - console.error(err) - process.exit() - }) +async function main() { + const icons = recursiveReadSync(path.join(__dirname, '../apps')).filter( + (file) => file.match(/icon\.png/) + ) + + console.log(`Resizing ${icons.length} icons...`) + const resizes = icons + .reduce((acc, icon) => { + const iconName = path.basename(icon) + + // skip disabled app + const yamlFile = path.join(icon.replace('-icon.png', '.yml')) + const { disabled } = yaml.load(fs.readFileSync(yamlFile)) + if (disabled) { + return acc + } + + return { + ...acc, + [iconName]: [resize(icon, 32), resize(icon, 64), resize(icon, 128), resize(icon, 256)] + } + }, {}) + + for (const icon in resizes) { + const promises = await Promise.allSettled(Object.values(resizes[icon])) + const failed = promises.filter(p => p.status === 'rejected') + + if (failed.length > 0) { + console.error(`🔴 Failed to resize icons for icon "${icon}"!`) + for (const { reason } of failed) { + console.log(reason) + } + process.exit(1) + } + } +} + +main();