diff options
Diffstat (limited to 'scripts/package.js')
-rw-r--r-- | scripts/package.js | 106 |
1 files changed, 2 insertions, 104 deletions
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 @@ | |||
1 | /** | 1 | /** |
2 | * Package recipe into tar.gz file | 2 | * Package recipe into tar.gz file |
3 | */ | 3 | */ |
4 | const targz = require('targz'); | 4 | const packageRecipe = require('./api/package'); |
5 | const fs = require('fs-extra'); | ||
6 | const path = require('path'); | ||
7 | 5 | ||
8 | console.log('Ferdi Recipe Packager v1.0.0'); | 6 | packageRecipe(); \ No newline at end of file |
9 | |||
10 | // Publicly availible link to this repository's uncompressed folder | ||
11 | // Used for generating public icon URLs | ||
12 | const repo = 'https://cdn.jsdelivr.net/gh/getferdi/recipes/uncompressed/'; | ||
13 | |||
14 | // Helper: Compress src folder into dest file | ||
15 | const compress = (src, dest) => new Promise((resolve, reject) => { | ||
16 | targz.compress({ | ||
17 | src, | ||
18 | dest, | ||
19 | tar: { | ||
20 | // Don't package .DS_Store files | ||
21 | ignore: function(name) { | ||
22 | return path.basename(name) === '.DS_Store' | ||
23 | } | ||
24 | }, | ||
25 | }, (err) => { | ||
26 | if (err) { | ||
27 | reject(err); | ||
28 | } else { | ||
29 | resolve(dest); | ||
30 | } | ||
31 | }); | ||
32 | }); | ||
33 | |||
34 | // Create paths to important files | ||
35 | const recipeSrc = path.join(__dirname, 'recipe_src'); | ||
36 | const packageJson = path.join(recipeSrc, 'package.json'); | ||
37 | const svgIcon = path.join(recipeSrc, 'icon.svg'); | ||
38 | const pngIcon = path.join(recipeSrc, 'icon.png'); | ||
39 | const allJson = path.join('../', 'all.json'); | ||
40 | |||
41 | // Let us work in an async environment | ||
42 | (async () => { | ||
43 | // Check that package.json exists | ||
44 | if (!await fs.pathExists(packageJson)) { | ||
45 | console.log(`Error: Please add your recipe to ${recipeSrc}. (package.json not found)`); | ||
46 | return; | ||
47 | } | ||
48 | // Check that icons exist | ||
49 | if (!await fs.pathExists(svgIcon) || !await fs.pathExists(pngIcon)) { | ||
50 | console.log(`Error: Please make sure your recipe contains an icon.png and an icon.svg file.`); | ||
51 | return; | ||
52 | } | ||
53 | |||
54 | // Read package.json | ||
55 | const config = await fs.readJson(packageJson) | ||
56 | |||
57 | // Make sure it contains all required fields | ||
58 | if (!config || !config.id || !config.name || !config.config) { | ||
59 | console.log(`Error: Your package.json does not contain all required fields. | ||
60 | Please make sure it contains: id, name, config`); | ||
61 | return; | ||
62 | } | ||
63 | |||
64 | // Package to .tar.gz | ||
65 | console.log(`Packaging ${config.id}...`); | ||
66 | compress(recipeSrc, path.join('../', 'archives', `${config.id}.tar.gz`)); | ||
67 | |||
68 | // Copy recipe src folder to /uncompressed/:id folder | ||
69 | console.log('Copying to uncompressed recipes'); | ||
70 | await fs.copy('recipe_src', path.join('../', 'uncompressed', `${config.id}`)); | ||
71 | |||
72 | // Add recipe to all.json | ||
73 | console.log('Adding to all.json'); | ||
74 | const packageInfo = { | ||
75 | "author": config.author || '', | ||
76 | "featured": false, | ||
77 | "id": config.id, | ||
78 | "name": config.name, | ||
79 | "version": config.version || '1.0.0', | ||
80 | "icons": { | ||
81 | "png": `${repo}${config.id}/icon.png`, | ||
82 | "svg": `${repo}${config.id}/icon.svg`, | ||
83 | }, | ||
84 | }; | ||
85 | let all = await fs.readJson(allJson); | ||
86 | // Check if package ID already exists | ||
87 | const packageIndex = all.findIndex(e => e.id === config.id) | ||
88 | if (packageIndex !== -1) { | ||
89 | console.log('Package with ID already exists - overwriting'); | ||
90 | all[packageIndex] = packageInfo; | ||
91 | } else { | ||
92 | console.log('No package with ID found - creating new.'); | ||
93 | all.push(packageInfo); | ||
94 | } | ||
95 | |||
96 | // Sort package list alphabetically | ||
97 | all = all.sort((a, b) => { | ||
98 | var textA = a.id.toLowerCase(); | ||
99 | var textB = b.id.toLowerCase(); | ||
100 | return (textA < textB) ? -1 : (textA > textB) ? 1 : 0; | ||
101 | }); | ||
102 | await fs.writeJson(allJson, all, { | ||
103 | spaces: 2, | ||
104 | EOL: '\n', | ||
105 | }); | ||
106 | |||
107 | console.log(`Successfully packaged and added new package ${config.id}`); | ||
108 | })(); \ No newline at end of file | ||