aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/package.js
blob: 2be3a6919b90e4e4654c15d67442582eb9a91291 (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
/**
 * Package recipe into tar.gz file
 */
const targz = require('targz');
const fs = require('fs-extra');
const path = require('path');

console.log('Ferdi Recipe Packager v1.0.0');

// Publicly availible link to this repository's uncompressed folder
// Used for generating public icon URLs
const repo = 'https://cdn.jsdelivr.net/gh/getferdi/recipes/uncompressed/';

// Helper: Compress src folder into dest file
const compress = (src, dest) => new Promise((resolve, reject) => {
  targz.compress({
    src,
    dest,
  }, (err) => {
    if (err) {
      reject(err);
    } else {
      resolve(dest);
    }
  });
});

// Create paths to important files
const recipeSrc = path.join(__dirname, 'recipe_src');
const packageJson = path.join(recipeSrc, 'package.json');
const svgIcon = path.join(recipeSrc, 'icon.svg');
const pngIcon = path.join(recipeSrc, 'icon.png');
const allJson = path.join('../', 'all.json');

// Let us work in an async environment
(async () => {
  // Check that package.json exists
  if (!await fs.pathExists(packageJson)) {
    console.log(`Error: Please add your recipe to ${recipeSrc}. (package.json not found)`);
    return;
  }
  // Check that icons exist
  if (!await fs.pathExists(svgIcon) || !await fs.pathExists(pngIcon)) {
    console.log(`Error: Please make sure your recipe contains an icon.png and an icon.svg file.`);
    return;
  }

  // Read package.json
  const config = await fs.readJson(packageJson)

  // Make sure it contains all required fields
  if (!config || !config.id || !config.name || !config.config) {
    console.log(`Error: Your package.json does not contain all required fields. 
Please make sure it contains: id, name, config`);
    return;
  }

  // Package to .tar.gz
  console.log(`Packaging ${config.id}...`);
  compress(recipeSrc, path.join('../', `${config.id}.tar.gz`));

  // Copy recipe src folder to /uncompressed/:id folder
  console.log('Copying to uncompressed recipes');
  await fs.copy('recipe_src', path.join('../', 'uncompressed', `${config.id}`));

  // Add recipe to all.json
  console.log('Adding to all.json');
  const packageInfo = {
    "author": config.author || '',
    "featured": false,
    "id": config.id,
    "name": config.name,
    "version": config.version || '1.0.0',
    "icons": {
      "png": `${repo}${config.id}/icon.png`,
      "svg": `${repo}${config.id}/icon.svg`,
    },
  };
  let all = await fs.readJson(allJson);
  // Check if package ID already exists
  const packageIndex = all.findIndex(e => e.id === config.id)
  if (packageIndex !== -1) {
    console.log('Package with ID already exists - overwriting');
    all[packageIndex] = packageInfo;
  } else {
    console.log('No package with ID found - creating new.');
    all.push(packageInfo);
  }
  await fs.writeJson(allJson, all, {
    spaces: 2,
    EOL: '\n',
  });

  console.log(`Successfully packaged and added new package ${config.id}`);
})();