diff options
Diffstat (limited to 'scripts/update.js')
-rw-r--r-- | scripts/update.js | 125 |
1 files changed, 0 insertions, 125 deletions
diff --git a/scripts/update.js b/scripts/update.js deleted file mode 100644 index 7538019..0000000 --- a/scripts/update.js +++ /dev/null | |||
@@ -1,125 +0,0 @@ | |||
1 | /** | ||
2 | * Update recipes from a Ferdi-compatible server | ||
3 | */ | ||
4 | const fetch = require('node-fetch'); | ||
5 | const targz = require('targz'); | ||
6 | const fs = require('fs-extra'); | ||
7 | const path = require('path'); | ||
8 | const semver = require('semver'); | ||
9 | |||
10 | console.log("Ferdi Recipe Repository Updater v1.0.0"); | ||
11 | |||
12 | // Server to update from | ||
13 | const server = "http://api.franzinfra.com/v1"; | ||
14 | |||
15 | // Create paths to important files | ||
16 | const allJson = path.join('../', 'all.json'); | ||
17 | |||
18 | // Helper: Download file to filesystem | ||
19 | const downloadFile = (async (url, path) => { | ||
20 | const res = await fetch(url); | ||
21 | const fileStream = fs.createWriteStream(path); | ||
22 | await new Promise((resolve, reject) => { | ||
23 | res.body.pipe(fileStream); | ||
24 | res.body.on("error", (err) => { | ||
25 | reject(err); | ||
26 | }); | ||
27 | fileStream.on("finish", function () { | ||
28 | resolve(); | ||
29 | }); | ||
30 | }); | ||
31 | }); | ||
32 | |||
33 | // Helper: Decompress .tar.gz file | ||
34 | const decompress = (src, dest) => { | ||
35 | return new Promise(resolve => { | ||
36 | targz.decompress({ | ||
37 | src, | ||
38 | dest | ||
39 | }, function (err) { | ||
40 | if (err) { | ||
41 | console.log('Error while decompressing recipe:', err); | ||
42 | } | ||
43 | resolve(); | ||
44 | }); | ||
45 | }) | ||
46 | } | ||
47 | |||
48 | // Let us work in an async environment | ||
49 | (async () => { | ||
50 | // Get current recipes from server | ||
51 | const serverRecipes = await (await fetch(server + '/recipes')).json(); | ||
52 | |||
53 | // Get current local recipes | ||
54 | const localRecipes = await fs.readJson(allJson); | ||
55 | |||
56 | for (const recipe of serverRecipes) { | ||
57 | // Find local recipe info | ||
58 | const localRecipe = localRecipes.find(e => e.id === recipe.id); | ||
59 | |||
60 | if (!localRecipe || semver.gt(recipe.version, localRecipe.version)) { | ||
61 | // Update is availible | ||
62 | console.log(`Updating ${recipe.id} from ${localRecipe ? localRecipe.version : '-1'} to ${recipe.version}`); | ||
63 | |||
64 | const compressed = path.join('../archives', `${recipe.id}.tar.gz`); | ||
65 | const uncompressed = path.join('../uncompressed', recipe.id); | ||
66 | |||
67 | // Download recipe to filesystem | ||
68 | try { | ||
69 | console.log("Downloading " + server + '/recipes/download/' + recipe.id); | ||
70 | await downloadFile( | ||
71 | server + '/recipes/download/' + recipe.id, | ||
72 | compressed | ||
73 | ); | ||
74 | } catch(e) { | ||
75 | console.log(`Could not download ${recipe.id}`); | ||
76 | return; | ||
77 | } | ||
78 | |||
79 | // Extract recipe | ||
80 | await decompress(compressed, uncompressed) | ||
81 | |||
82 | // Make sure we have all icons | ||
83 | const iconPng = path.join(uncompressed, '/icon.png'); | ||
84 | const iconSvg = path.join(uncompressed, '/icon.svg'); | ||
85 | if (!await fs.exists(iconPng)) { | ||
86 | downloadFile(recipe.icons.png, iconPng); | ||
87 | } | ||
88 | if (!await fs.exists(iconSvg)) { | ||
89 | downloadFile(recipe.icons.svg, iconSvg); | ||
90 | } | ||
91 | |||
92 | // Update entry in all.json | ||
93 | // Check if package ID already exists | ||
94 | const recipeIndex = localRecipes.findIndex(e => e.id === recipe.id); | ||
95 | |||
96 | const recipeInfo = recipe; | ||
97 | recipeInfo.icons.png = recipeInfo.icons.png.replace( | ||
98 | 'https://cdn.franzinfra.com/recipes/dist/', | ||
99 | 'https://cdn.jsdelivr.net/gh/getferdi/recipes/uncompressed/' | ||
100 | ).replace( | ||
101 | 'src/icon.png', | ||
102 | 'icon.png' | ||
103 | ); | ||
104 | recipeInfo.icons.svg = recipeInfo.icons.svg.replace( | ||
105 | 'https://cdn.franzinfra.com/recipes/dist/', | ||
106 | 'https://cdn.jsdelivr.net/gh/getferdi/recipes/uncompressed/' | ||
107 | ).replace( | ||
108 | 'src/icon.svg', | ||
109 | 'icon.svg' | ||
110 | ); | ||
111 | |||
112 | if (recipeIndex !== -1) { | ||
113 | localRecipes[recipeIndex] = recipeInfo; | ||
114 | } else { | ||
115 | localRecipes.push(recipeInfo); | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | |||
120 | // Write updated package info to all.json | ||
121 | await fs.writeJson(allJson, localRecipes, { | ||
122 | spaces: 2, | ||
123 | EOL: '\n', | ||
124 | }); | ||
125 | })(); \ No newline at end of file | ||