aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/add_github.js
blob: 4a5cfce011f676257c385bf052d65b2bf1121840 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
 * Add GitHub repository as recipe
 */
const fetch = require('node-fetch');
const targz = require('targz');
const fs = require('fs-extra');
const path = require('path');
const GitUrlParse = require("git-url-parse");
const packageRecipe = require('./api/package');

// Helper: Download file to filesystem
const downloadFile = (async (url, path) => {
  const res = await fetch(url);
  const fileStream = fs.createWriteStream(path);
  await new Promise((resolve, reject) => {
      res.body.pipe(fileStream);
      res.body.on("error", (err) => {
          reject(err);
      });
      fileStream.on("finish", function () {
          resolve();
      });
  });
});

// Helper: Decompress .tar.gz file
const decompress = (src, dest) => {
  return new Promise(resolve => {
      targz.decompress({
          src,
          dest
      }, function (err) {
          if (err) {
              console.log('⚠️ Could not add your recipe: There was an error while decompressing your GitHub repository file: ', err);
          }
          resolve();
      });
  })
}

const repo = process.argv[2];

if (!repo || !/https:\/\/github\.com\/[^\/]+\/[^\/]+\/?/gi.test(repo)) {
  console.log(`⚠️ Could not add your recipe: The GitHub URL you provided doesn't seem to be valid.
You should use this command like "yarn github https://github.com/user/repo".
Please make sure you provide a URL in the format "https://github.com/user/repo"
For more information about this script visit https://github.com/getferdi/recipes/blob/master/docs/integration.md#publishing
If you want to package a local recipe, please use "yarn package" instead.`);
  return;
}

const repoInfo = GitUrlParse(repo);
const tempDir = path.join(__dirname, 'tmp');

const recipeSrc = path.join(__dirname, 'recipe_src');
const recipeSrcTmp = path.join(__dirname, 'recipe_src_tmp');

const compressed = path.join(__dirname, 'tmp.tar.gz');

// Let us work in an async environment
(async () => {
  console.log("[Info] Creating temporary directory");

  await fs.ensureDir(tempDir);
  await fs.ensureDir(recipeSrc);
  await fs.ensureDir(recipeSrcTmp);

  console.log("[Info] Downloading " + repo);
  
  await downloadFile(
    `https://github.com/${repoInfo.owner}/${repoInfo.name}/archive/master.tar.gz`,
    compressed
  );

  console.log("[Info] Decompressing repository");

  await decompress(compressed, tempDir);

  console.log("[Info] Moving 'recipe_src' to 'recipe_src_tmp'");

  await fs.move(recipeSrc, recipeSrcTmp, {overwrite: true});
  await fs.move(
    path.join(tempDir, `${repoInfo.name}-master`),
    recipeSrc,
    {overwrite: true}
  );

  console.log("[Info] Packaging your recipe");
  try {
    await packageRecipe();
  } catch(e) {
    return;
  }

  console.log("[Info] Deleting temporarydownloaded repository");

  await fs.remove(compressed);
  await fs.remove(recipeSrc);

  console.log("[Info] Moving back 'recipe_src_tmp' to 'recipe_src'");

  await fs.move(recipeSrcTmp, recipeSrc);

  console.log(`✅ Successfully packaged the recipe from your GitHub repository`);
})();