diff options
author | 2020-01-14 20:47:19 +0100 | |
---|---|---|
committer | 2020-01-14 20:47:19 +0100 | |
commit | e57a73a11fb100baba065daae82d04f4f4d63610 (patch) | |
tree | 335be726e905b0379fd3faff6e779403cd7c2b0e /scripts/add_github.js | |
parent | Remove Mastodon recipe (diff) | |
download | ferdium-recipes-e57a73a11fb100baba065daae82d04f4f4d63610.tar.gz ferdium-recipes-e57a73a11fb100baba065daae82d04f4f4d63610.tar.zst ferdium-recipes-e57a73a11fb100baba065daae82d04f4f4d63610.zip |
Add github script
Diffstat (limited to 'scripts/add_github.js')
-rw-r--r-- | scripts/add_github.js | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/scripts/add_github.js b/scripts/add_github.js new file mode 100644 index 0000000..add3575 --- /dev/null +++ b/scripts/add_github.js | |||
@@ -0,0 +1,94 @@ | |||
1 | /** | ||
2 | * Add GitHub repository as recipe | ||
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 GitUrlParse = require("git-url-parse"); | ||
9 | |||
10 | // Helper: Download file to filesystem | ||
11 | const downloadFile = (async (url, path) => { | ||
12 | const res = await fetch(url); | ||
13 | const fileStream = fs.createWriteStream(path); | ||
14 | await new Promise((resolve, reject) => { | ||
15 | res.body.pipe(fileStream); | ||
16 | res.body.on("error", (err) => { | ||
17 | reject(err); | ||
18 | }); | ||
19 | fileStream.on("finish", function () { | ||
20 | resolve(); | ||
21 | }); | ||
22 | }); | ||
23 | }); | ||
24 | |||
25 | // Helper: Decompress .tar.gz file | ||
26 | const decompress = (src, dest) => { | ||
27 | return new Promise(resolve => { | ||
28 | targz.decompress({ | ||
29 | src, | ||
30 | dest | ||
31 | }, function (err) { | ||
32 | if (err) { | ||
33 | console.log('Error while decompressing recipe:', err); | ||
34 | } | ||
35 | resolve(); | ||
36 | }); | ||
37 | }) | ||
38 | } | ||
39 | |||
40 | const repo = process.argv[2]; | ||
41 | |||
42 | if (!repo || !/https:\/\/github\.com\/[^\/]+\/[^\/]+\/?/gi.test(repo)) { | ||
43 | console.log("Please provide a valid repository URL"); | ||
44 | return; | ||
45 | } | ||
46 | |||
47 | const repoInfo = GitUrlParse(repo); | ||
48 | const tempDir = path.join(__dirname, 'tmp'); | ||
49 | |||
50 | const recipeSrc = path.join(__dirname, 'recipe_src'); | ||
51 | const recipeSrcTmp = path.join(__dirname, 'recipe_src_tmp'); | ||
52 | |||
53 | const compressed = path.join(__dirname, 'tmp.tar.gz'); | ||
54 | |||
55 | // Let us work in an async environment | ||
56 | (async () => { | ||
57 | console.log("Creating temporary directory"); | ||
58 | |||
59 | await fs.ensureDir(tempDir); | ||
60 | |||
61 | console.log("Downloading " + repo); | ||
62 | |||
63 | await downloadFile( | ||
64 | `https://github.com/${repoInfo.owner}/${repoInfo.name}/archive/master.tar.gz`, | ||
65 | compressed | ||
66 | ); | ||
67 | |||
68 | console.log("Decompressing tarball"); | ||
69 | |||
70 | await decompress(compressed, tempDir); | ||
71 | |||
72 | console.log("Moving directories"); | ||
73 | |||
74 | await fs.move(recipeSrc, recipeSrcTmp); | ||
75 | await fs.move( | ||
76 | path.join(tempDir, `${repoInfo.name}-master`), | ||
77 | recipeSrc | ||
78 | ); | ||
79 | |||
80 | console.log("Adding to repository"); | ||
81 | require('./package.js'); | ||
82 | console.log("Continuing in 1.5 seconds"); | ||
83 | |||
84 | setTimeout(async () => { | ||
85 | console.log("Deleting downloaded files"); | ||
86 | |||
87 | await fs.remove(compressed); | ||
88 | await fs.remove(recipeSrc); | ||
89 | |||
90 | console.log("Moving back recipe folder"); | ||
91 | |||
92 | await fs.move(recipeSrcTmp, recipeSrc); | ||
93 | }, 1500); | ||
94 | })(); \ No newline at end of file | ||