diff options
Diffstat (limited to 'scripts/gh_load.js')
-rw-r--r-- | scripts/gh_load.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/scripts/gh_load.js b/scripts/gh_load.js new file mode 100644 index 0000000..e72cf23 --- /dev/null +++ b/scripts/gh_load.js | |||
@@ -0,0 +1,85 @@ | |||
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 | await fs.ensureDir(recipeSrc); | ||
61 | await fs.ensureDir(recipeSrcTmp); | ||
62 | |||
63 | console.log("Downloading " + repo); | ||
64 | |||
65 | await downloadFile( | ||
66 | `https://github.com/${repoInfo.owner}/${repoInfo.name}/archive/master.tar.gz`, | ||
67 | compressed | ||
68 | ); | ||
69 | |||
70 | console.log("Decompressing tarball"); | ||
71 | |||
72 | await decompress(compressed, tempDir); | ||
73 | |||
74 | console.log("Moving directories"); | ||
75 | |||
76 | await fs.move(recipeSrc, recipeSrcTmp, {overwrite: true}); | ||
77 | await fs.move( | ||
78 | path.join(tempDir, `${repoInfo.name}-master`), | ||
79 | recipeSrc, | ||
80 | {overwrite: true} | ||
81 | ); | ||
82 | |||
83 | await fs.remove(compressed); | ||
84 | await fs.remove(recipeSrcTmp); | ||
85 | })(); \ No newline at end of file | ||