diff options
author | 2020-09-22 20:56:48 +0200 | |
---|---|---|
committer | 2020-09-22 19:56:48 +0100 | |
commit | 6f5e4a00588aefdda7a5a1cfe70935870e7e234a (patch) | |
tree | 9e29aa7aa0620a1a4a968ff8739b4b8ba96791a9 /scripts/add_github.js | |
parent | Updated logos for Outlook/OWA (diff) | |
download | ferdium-recipes-6f5e4a00588aefdda7a5a1cfe70935870e7e234a.tar.gz ferdium-recipes-6f5e4a00588aefdda7a5a1cfe70935870e7e234a.tar.zst ferdium-recipes-6f5e4a00588aefdda7a5a1cfe70935870e7e234a.zip |
Unpack recipes and update recipes icons (#292)
Co-authored-by: Amine Mouafik <amine@mouafik.fr>
Diffstat (limited to 'scripts/add_github.js')
-rw-r--r-- | scripts/add_github.js | 107 |
1 files changed, 0 insertions, 107 deletions
diff --git a/scripts/add_github.js b/scripts/add_github.js deleted file mode 100644 index c3a7ca4..0000000 --- a/scripts/add_github.js +++ /dev/null | |||
@@ -1,107 +0,0 @@ | |||
1 | /** | ||
2 | * Add GitHub repository as recipe | ||
3 | */ | ||
4 | require('./api/require-depts')(); | ||
5 | |||
6 | const fetch = require('node-fetch'); | ||
7 | const targz = require('targz'); | ||
8 | const fs = require('fs-extra'); | ||
9 | const path = require('path'); | ||
10 | const GitUrlParse = require("git-url-parse"); | ||
11 | const packageRecipe = require('./api/package'); | ||
12 | |||
13 | // Helper: Download file to filesystem | ||
14 | const downloadFile = (async (url, path) => { | ||
15 | const res = await fetch(url); | ||
16 | const fileStream = fs.createWriteStream(path); | ||
17 | await new Promise((resolve, reject) => { | ||
18 | res.body.pipe(fileStream); | ||
19 | res.body.on("error", (err) => { | ||
20 | reject(err); | ||
21 | }); | ||
22 | fileStream.on("finish", function () { | ||
23 | resolve(); | ||
24 | }); | ||
25 | }); | ||
26 | }); | ||
27 | |||
28 | // Helper: Decompress .tar.gz file | ||
29 | const decompress = (src, dest) => { | ||
30 | return new Promise(resolve => { | ||
31 | targz.decompress({ | ||
32 | src, | ||
33 | dest | ||
34 | }, function (err) { | ||
35 | if (err) { | ||
36 | console.log('⚠️ Could not add your recipe: There was an error while decompressing your GitHub repository file: ', err); | ||
37 | } | ||
38 | resolve(); | ||
39 | }); | ||
40 | }) | ||
41 | } | ||
42 | |||
43 | const repo = process.argv[2]; | ||
44 | |||
45 | if (!repo || !/https:\/\/github\.com\/[^\/]+\/[^\/]+\/?/gi.test(repo)) { | ||
46 | console.log(`⚠️ Could not add your recipe: The GitHub URL you provided doesn't seem to be valid. | ||
47 | You should use this command like "yarn github https://github.com/user/repo". | ||
48 | Please make sure you provide a URL in the format "https://github.com/user/repo" | ||
49 | For more information about this script visit https://github.com/getferdi/recipes/blob/master/docs/integration.md#publishing | ||
50 | If you want to package a local recipe, please use "yarn package" instead.`); | ||
51 | return; | ||
52 | } | ||
53 | |||
54 | const repoInfo = GitUrlParse(repo); | ||
55 | const tempDir = path.join(__dirname, 'tmp'); | ||
56 | |||
57 | const recipeSrc = path.join(__dirname, 'recipe_src'); | ||
58 | const recipeSrcTmp = path.join(__dirname, 'recipe_src_tmp'); | ||
59 | |||
60 | const compressed = path.join(__dirname, 'tmp.tar.gz'); | ||
61 | |||
62 | // Let us work in an async environment | ||
63 | (async () => { | ||
64 | console.log("[Info] Creating temporary directory"); | ||
65 | |||
66 | await fs.ensureDir(tempDir); | ||
67 | await fs.ensureDir(recipeSrc); | ||
68 | await fs.ensureDir(recipeSrcTmp); | ||
69 | |||
70 | console.log("[Info] Downloading " + repo); | ||
71 | |||
72 | await downloadFile( | ||
73 | `https://github.com/${repoInfo.owner}/${repoInfo.name}/archive/master.tar.gz`, | ||
74 | compressed | ||
75 | ); | ||
76 | |||
77 | console.log("[Info] Decompressing repository"); | ||
78 | |||
79 | await decompress(compressed, tempDir); | ||
80 | |||
81 | console.log("[Info] Moving 'recipe_src' to 'recipe_src_tmp'"); | ||
82 | |||
83 | await fs.move(recipeSrc, recipeSrcTmp, {overwrite: true}); | ||
84 | await fs.move( | ||
85 | path.join(tempDir, `${repoInfo.name}-master`), | ||
86 | recipeSrc, | ||
87 | {overwrite: true} | ||
88 | ); | ||
89 | |||
90 | console.log("[Info] Packaging your recipe"); | ||
91 | try { | ||
92 | await packageRecipe(); | ||
93 | } catch(e) { | ||
94 | return; | ||
95 | } | ||
96 | |||
97 | console.log("[Info] Deleting temporarydownloaded repository"); | ||
98 | |||
99 | await fs.remove(compressed); | ||
100 | await fs.remove(recipeSrc); | ||
101 | |||
102 | console.log("[Info] Moving back 'recipe_src_tmp' to 'recipe_src'"); | ||
103 | |||
104 | await fs.move(recipeSrcTmp, recipeSrc); | ||
105 | |||
106 | console.log(`✅ Successfully packaged the recipe from your GitHub repository`); | ||
107 | })(); \ No newline at end of file | ||