From 8a6f31efaab75d16797126fe9e9e03ba45877b91 Mon Sep 17 00:00:00 2001 From: vantezzen Date: Wed, 15 Apr 2020 19:41:54 +0200 Subject: Improve recipe packaging script This commit will improve the usability of the packaging script, especially for beginners. It will output better error messages if there are any problems with the recipe. It will also add more tests to improve detecting those problems. This way, more problems can be detected before even needing to create a PR. --- scripts/package.js | 106 +---------------------------------------------------- 1 file changed, 2 insertions(+), 104 deletions(-) (limited to 'scripts/package.js') diff --git a/scripts/package.js b/scripts/package.js index a241edc..3726407 100644 --- a/scripts/package.js +++ b/scripts/package.js @@ -1,108 +1,6 @@ /** * Package recipe into tar.gz file */ -const targz = require('targz'); -const fs = require('fs-extra'); -const path = require('path'); +const packageRecipe = require('./api/package'); -console.log('Ferdi Recipe Packager v1.0.0'); - -// Publicly availible link to this repository's uncompressed folder -// Used for generating public icon URLs -const repo = 'https://cdn.jsdelivr.net/gh/getferdi/recipes/uncompressed/'; - -// Helper: Compress src folder into dest file -const compress = (src, dest) => new Promise((resolve, reject) => { - targz.compress({ - src, - dest, - tar: { - // Don't package .DS_Store files - ignore: function(name) { - return path.basename(name) === '.DS_Store' - } - }, - }, (err) => { - if (err) { - reject(err); - } else { - resolve(dest); - } - }); -}); - -// Create paths to important files -const recipeSrc = path.join(__dirname, 'recipe_src'); -const packageJson = path.join(recipeSrc, 'package.json'); -const svgIcon = path.join(recipeSrc, 'icon.svg'); -const pngIcon = path.join(recipeSrc, 'icon.png'); -const allJson = path.join('../', 'all.json'); - -// Let us work in an async environment -(async () => { - // Check that package.json exists - if (!await fs.pathExists(packageJson)) { - console.log(`Error: Please add your recipe to ${recipeSrc}. (package.json not found)`); - return; - } - // Check that icons exist - if (!await fs.pathExists(svgIcon) || !await fs.pathExists(pngIcon)) { - console.log(`Error: Please make sure your recipe contains an icon.png and an icon.svg file.`); - return; - } - - // Read package.json - const config = await fs.readJson(packageJson) - - // Make sure it contains all required fields - if (!config || !config.id || !config.name || !config.config) { - console.log(`Error: Your package.json does not contain all required fields. -Please make sure it contains: id, name, config`); - return; - } - - // Package to .tar.gz - console.log(`Packaging ${config.id}...`); - compress(recipeSrc, path.join('../', 'archives', `${config.id}.tar.gz`)); - - // Copy recipe src folder to /uncompressed/:id folder - console.log('Copying to uncompressed recipes'); - await fs.copy('recipe_src', path.join('../', 'uncompressed', `${config.id}`)); - - // Add recipe to all.json - console.log('Adding to all.json'); - const packageInfo = { - "author": config.author || '', - "featured": false, - "id": config.id, - "name": config.name, - "version": config.version || '1.0.0', - "icons": { - "png": `${repo}${config.id}/icon.png`, - "svg": `${repo}${config.id}/icon.svg`, - }, - }; - let all = await fs.readJson(allJson); - // Check if package ID already exists - const packageIndex = all.findIndex(e => e.id === config.id) - if (packageIndex !== -1) { - console.log('Package with ID already exists - overwriting'); - all[packageIndex] = packageInfo; - } else { - console.log('No package with ID found - creating new.'); - all.push(packageInfo); - } - - // Sort package list alphabetically - all = all.sort((a, b) => { - var textA = a.id.toLowerCase(); - var textB = b.id.toLowerCase(); - return (textA < textB) ? -1 : (textA > textB) ? 1 : 0; - }); - await fs.writeJson(allJson, all, { - spaces: 2, - EOL: '\n', - }); - - console.log(`Successfully packaged and added new package ${config.id}`); -})(); \ No newline at end of file +packageRecipe(); \ No newline at end of file -- cgit v1.2.3-70-g09d2