From 15243c01a2c46c82d58ce5e68ecb441b8d2fde98 Mon Sep 17 00:00:00 2001 From: André Oliveira Date: Sat, 4 May 2024 03:12:51 +0100 Subject: chore: change icons to reduce bundle --- docs/configuration.md | 3 +++ scripts/package.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 081b7ad..6a20d3a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -32,6 +32,9 @@ Link to your Github, Gitlab or Bitbucket public repository. Not used in the appl `array[string]` **aliases**
The list of alternate names that this recipe can be called +`string` **defaultIcon**
(DEFAULT: undefined) +A URL for the default icon of the recipe. If not provided, then the user must add an SVG file to the root with the name `icon.svg` to serve as the default icon. + `object` **config** _mandatory_
This is the Ferdium-specific integration config. diff --git a/scripts/package.js b/scripts/package.js index c2e2ab7..7ccb466 100644 --- a/scripts/package.js +++ b/scripts/package.js @@ -1,5 +1,8 @@ /* eslint-disable no-console */ +// Capture the start time +const startTime = new Date(); + /** * Package all recipes */ @@ -13,7 +16,8 @@ const pkgVersionChangedMatcher = /\n\+.*version.*/; // Publicly availible link to this repository's recipe folder // Used for generating public icon URLs -const repo = 'https://cdn.jsdelivr.net/gh/ferdium/ferdium-recipes/recipes/'; +const repo = + 'https://cdn.jsdelivr.net/gh/ferdium/ferdium-recipes@main/recipes/'; // Helper: Compress src folder into dest file const compress = (src, dest) => @@ -25,7 +29,11 @@ const compress = (src, dest) => tar: { // Don't package .DS_Store files and .md files ignore(name) { - return path.basename(name) === '.DS_Store' || name.endsWith('.md'); + return ( + path.basename(name) === '.DS_Store' || + name.endsWith('.md') || + name.endsWith('.svg') + ); }, }, }, @@ -43,6 +51,7 @@ const compress = (src, dest) => (async () => { // Create paths to important files const repoRoot = path.join(__dirname, '..'); + const tempFolder = path.join(repoRoot, 'temp'); const recipesFolder = path.join(repoRoot, 'recipes'); const outputFolder = path.join(repoRoot, 'archives'); const allJson = path.join(repoRoot, 'all.json'); @@ -53,6 +62,8 @@ const compress = (src, dest) => await fs.ensureDir(outputFolder); await fs.emptyDir(outputFolder); + await fs.ensureDir(tempFolder); + await fs.emptyDir(tempFolder); await fs.remove(allJson); const git = await simpleGit(repoRoot); @@ -68,7 +79,7 @@ const compress = (src, dest) => for (const recipe of availableRecipes) { const recipeSrc = path.join(recipesFolder, recipe); - const mandatoryFiles = ['package.json', 'icon.svg', 'webview.js']; + const mandatoryFiles = ['package.json', 'webview.js']; // Check that each mandatory file exists for (const file of mandatoryFiles) { @@ -179,6 +190,7 @@ const compress = (src, dest) => 'repository', 'aliases', 'config', + 'defaultIcon', ]); const unrecognizedKeys = topLevelKeys.filter( x => !knownTopLevelKeys.has(x), @@ -276,8 +288,46 @@ const compress = (src, dest) => unsuccessful += 1; } + // Copy recipe to temp folder + // eslint-disable-next-line no-await-in-loop + await fs.copy(recipeSrc, path.join(tempFolder, config.id), { + filter: src => !src.endsWith('icon.svg'), + }); + + if (!config.defaultIcon) { + // Check if icon.svg exists + // eslint-disable-next-line no-await-in-loop + if (!(await fs.exists(path.join(recipeSrc, 'icon.svg')))) { + console.log( + `⚠️ Couldn't package "${recipe}": The recipe doesn't contain a "icon.svg" or "defaultIcon" in package.json`, + ); + unsuccessful += 1; + } + + // eslint-disable-next-line no-await-in-loop + const tempPackage = await fs.readJSON( + path.join(tempFolder, config.id, 'package.json'), + ); + tempPackage.defaultIcon = `${repo}${config.id}/icon.svg`; + + // eslint-disable-next-line no-await-in-loop + await fs.writeJSON( + path.join(tempFolder, config.id, 'package.json'), + tempPackage, + // JSON.stringify(tempPackage, null, 2), + { + spaces: 2, + EOL: '\n', + }, + ); + } + // Package to .tar.gz - compress(recipeSrc, path.join(outputFolder, `${config.id}.tar.gz`)); + // eslint-disable-next-line no-await-in-loop + await compress( + path.join(tempFolder, config.id), + path.join(outputFolder, `${config.id}.tar.gz`), + ); // Add recipe to all.json const isFeatured = featuredRecipes.includes(config.id); @@ -305,8 +355,14 @@ const compress = (src, dest) => EOL: '\n', }); + // Clean up + await fs.remove(tempFolder); + + // Capture the end time + const endTime = new Date(); + console.log( - `✅ Successfully packaged and added ${recipeList.length} recipes (${unsuccessful} unsuccessful recipes)`, + `✅ Successfully packaged and added ${recipeList.length} recipes (${unsuccessful} unsuccessful recipes) in ${(endTime - startTime) / 1000} seconds`, ); if (unsuccessful > 0) { -- cgit v1.2.3-54-g00ecf