From 86ae59df84cfdefd87436dc54a9f24479be9043a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Oliveira?= Date: Sat, 4 May 2024 03:12:51 +0100 Subject: [PATCH 1/2] 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 081b7ad2a..6a20d3a8f 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 c2e2ab719..7ccb46654 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) { From f01a16cdba4e9cd1f8db1ed68b20f3f120881e8d Mon Sep 17 00:00:00 2001 From: Vijay A Date: Sat, 4 May 2024 10:13:36 +0530 Subject: [PATCH 2/2] Replace 'await' calls with '*Sync' calls - which also removes eslint disables --- scripts/package.js | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/scripts/package.js b/scripts/package.js index 7ccb46654..a902c8c8b 100644 --- a/scripts/package.js +++ b/scripts/package.js @@ -56,15 +56,15 @@ const compress = (src, dest) => const outputFolder = path.join(repoRoot, 'archives'); const allJson = path.join(repoRoot, 'all.json'); const featuredFile = path.join(repoRoot, 'featured.json'); - const featuredRecipes = await fs.readJSON(featuredFile); + const featuredRecipes = fs.readJSONSync(featuredFile); let recipeList = []; let unsuccessful = 0; - await fs.ensureDir(outputFolder); - await fs.emptyDir(outputFolder); - await fs.ensureDir(tempFolder); - await fs.emptyDir(tempFolder); - await fs.remove(allJson); + fs.ensureDirSync(outputFolder); + fs.emptyDirSync(outputFolder); + fs.ensureDirSync(tempFolder); + fs.emptyDirSync(tempFolder); + fs.removeSync(allJson); const git = await simpleGit(repoRoot); const isGitRepo = await git.checkIsRepo(); @@ -84,8 +84,7 @@ const compress = (src, dest) => // Check that each mandatory file exists for (const file of mandatoryFiles) { const filePath = path.join(recipeSrc, file); - // eslint-disable-next-line no-await-in-loop - if (!(await fs.pathExists(filePath))) { + if (!fs.pathExistsSync(filePath)) { console.log( `⚠️ Couldn't package "${recipe}": Folder doesn't contain a "${file}".`, ); @@ -110,8 +109,7 @@ const compress = (src, dest) => // Check that user.js does not exist const userJs = path.join(recipeSrc, 'user.js'); - // eslint-disable-next-line no-await-in-loop - if (await fs.pathExists(userJs)) { + if (fs.pathExistsSync(userJs)) { console.log( `⚠️ Couldn't package "${recipe}": Folder contains a "user.js".`, ); @@ -121,8 +119,7 @@ const compress = (src, dest) => // Read package.json const packageJson = path.join(recipeSrc, 'package.json'); - // eslint-disable-next-line no-await-in-loop - const config = await fs.readJson(packageJson); + const config = fs.readJsonSync(packageJson); // Make sure it contains all required fields if (!config) { @@ -245,7 +242,6 @@ const compress = (src, dest) => const relativeRepoSrc = path.relative(repoRoot, recipeSrc); // Check for changes in recipe's directory, and if changes are present, then the changes should contain a version bump - // eslint-disable-next-line no-await-in-loop await git.diffSummary(relativeRepoSrc, (err, result) => { if (err) { configErrors.push( @@ -276,8 +272,9 @@ const compress = (src, dest) => } if (configErrors.length > 0) { - console.log(`⚠️ Couldn't package "${recipe}": There were errors in the recipe's package.json: - ${configErrors.reduce((str, err) => `${str}\n${err}`)}`); + console.log( + `⚠️ Couldn't package "${recipe}": There were errors in the recipe's package.json: ${configErrors.reduce((str, err) => `${str}\n${err}`)}`, + ); unsuccessful += 1; } @@ -289,29 +286,25 @@ const compress = (src, dest) => } // Copy recipe to temp folder - // eslint-disable-next-line no-await-in-loop - await fs.copy(recipeSrc, path.join(tempFolder, config.id), { + fs.copySync(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')))) { + if (!fs.existsSync(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( + const tempPackage = fs.readJSONSync( 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( + fs.writeJSONSync( path.join(tempFolder, config.id, 'package.json'), tempPackage, // JSON.stringify(tempPackage, null, 2), @@ -348,15 +341,15 @@ const compress = (src, dest) => recipeList = recipeList.sort((a, b) => { const textA = a.id.toLowerCase(); const textB = b.id.toLowerCase(); - return textA < textB ? -1 : textA > textB ? 1 : 0; + return textA < textB ? -1 : (textA > textB ? 1 : 0); }); - await fs.writeJson(allJson, recipeList, { + fs.writeJsonSync(allJson, recipeList, { spaces: 2, EOL: '\n', }); // Clean up - await fs.remove(tempFolder); + fs.removeSync(tempFolder); // Capture the end time const endTime = new Date();