diff options
Diffstat (limited to 'scripts/package.js')
-rw-r--r-- | scripts/package.js | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/scripts/package.js b/scripts/package.js new file mode 100644 index 0000000..581d23c --- /dev/null +++ b/scripts/package.js | |||
@@ -0,0 +1,101 @@ | |||
1 | /** | ||
2 | * Package recipe into tar.gz file | ||
3 | */ | ||
4 | const targz = require('targz'); | ||
5 | const fs = require('fs-extra'); | ||
6 | const path = require('path'); | ||
7 | |||
8 | console.log('Ferdi Recipe Packager v1.0.0'); | ||
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 | }, (err) => { | ||
20 | if (err) { | ||
21 | reject(err); | ||
22 | } else { | ||
23 | resolve(dest); | ||
24 | } | ||
25 | }); | ||
26 | }); | ||
27 | |||
28 | // Create paths to important files | ||
29 | const recipeSrc = path.join(__dirname, 'recipe_src'); | ||
30 | const packageJson = path.join(recipeSrc, 'package.json'); | ||
31 | const svgIcon = path.join(recipeSrc, 'icon.svg'); | ||
32 | const pngIcon = path.join(recipeSrc, 'icon.png'); | ||
33 | const allJson = path.join('../', 'all.json'); | ||
34 | |||
35 | // Let us work in an async environment | ||
36 | (async () => { | ||
37 | // Check that package.json exists | ||
38 | if (!await fs.pathExists(packageJson)) { | ||
39 | console.log(`Error: Please add your recipe to ${recipeSrc}. (package.json not found)`); | ||
40 | return; | ||
41 | } | ||
42 | // Check that icons exist | ||
43 | if (!await fs.pathExists(svgIcon) || !await fs.pathExists(pngIcon)) { | ||
44 | console.log(`Error: Please make sure your recipe contains an icon.png and an icon.svg file.`); | ||
45 | return; | ||
46 | } | ||
47 | |||
48 | // Read package.json | ||
49 | const config = await fs.readJson(packageJson) | ||
50 | |||
51 | // Make sure it contains all required fields | ||
52 | if (!config || !config.id || !config.name || !config.config) { | ||
53 | console.log(`Error: Your package.json does not contain all required fields. | ||
54 | Please make sure it contains: id, name, config`); | ||
55 | return; | ||
56 | } | ||
57 | |||
58 | // Move readme.txt outside of recipe_src folder | ||
59 | await fs.move(path.join(recipeSrc, 'readme.txt'), './readme.txt'); | ||
60 | |||
61 | // Package to .tar.gz | ||
62 | console.log(`Packaging ${config.id}...`); | ||
63 | compress(recipeSrc, path.join('../', `${config.id}.tar.gz`)); | ||
64 | |||
65 | // Copy recipe src folder to /uncompressed/:id folder | ||
66 | console.log('Copying to uncompressed recipes'); | ||
67 | await fs.copy('recipe_src', path.join('../', 'uncompressed', `${config.id}`)); | ||
68 | |||
69 | // Add recipe to all.json | ||
70 | console.log('Adding to all.json'); | ||
71 | const packageInfo = { | ||
72 | "author": config.author || '', | ||
73 | "featured": false, | ||
74 | "id": config.id, | ||
75 | "name": config.name, | ||
76 | "version": config.version || '1.0.0', | ||
77 | "icons": { | ||
78 | "png": `${repo}${config.id}/icon.png`, | ||
79 | "svg": `${repo}${config.id}/icon.svg`, | ||
80 | }, | ||
81 | }; | ||
82 | let all = await fs.readJson(allJson); | ||
83 | // Check if package ID already exists | ||
84 | const packageIndex = all.findIndex(e => e.id === config.id) | ||
85 | if (packageIndex !== -1) { | ||
86 | console.log('Package with ID already exists - overwriting'); | ||
87 | all[packageIndex] = packageInfo; | ||
88 | } else { | ||
89 | console.log('No package with ID found - creating new.'); | ||
90 | all.push(packageInfo); | ||
91 | } | ||
92 | await fs.writeJson(allJson, all, { | ||
93 | spaces: 2, | ||
94 | EOL: '\n', | ||
95 | }); | ||
96 | |||
97 | // Move readme.txt back into recipe_src | ||
98 | await fs.move('./readme.txt', path.join(recipeSrc, 'readme.txt')); | ||
99 | |||
100 | console.log(`Successfully packaged and added new package ${config.id}`); | ||
101 | })(); \ No newline at end of file | ||