aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/add_github.js
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/add_github.js')
-rw-r--r--scripts/add_github.js107
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 */
4require('./api/require-depts')();
5
6const fetch = require('node-fetch');
7const targz = require('targz');
8const fs = require('fs-extra');
9const path = require('path');
10const GitUrlParse = require("git-url-parse");
11const packageRecipe = require('./api/package');
12
13// Helper: Download file to filesystem
14const 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
29const 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
43const repo = process.argv[2];
44
45if (!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.
47You should use this command like "yarn github https://github.com/user/repo".
48Please make sure you provide a URL in the format "https://github.com/user/repo"
49For more information about this script visit https://github.com/getferdi/recipes/blob/master/docs/integration.md#publishing
50If you want to package a local recipe, please use "yarn package" instead.`);
51 return;
52}
53
54const repoInfo = GitUrlParse(repo);
55const tempDir = path.join(__dirname, 'tmp');
56
57const recipeSrc = path.join(__dirname, 'recipe_src');
58const recipeSrcTmp = path.join(__dirname, 'recipe_src_tmp');
59
60const 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