aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/verify-all.js
blob: af0bab9c6e1f7e8610ae9edd9822112a9a756b1f (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
/**
 * Verify all archived recipes to match uncompressed recipes
 */
const targz = require('targz');
const fs = require('fs-extra');
const dircompare = require('dir-compare');
const path = require('path');

// Helper: Compress src folder into dest file
const decompress = (src, dest) => new Promise((resolve, reject) => {
  targz.decompress({
    src,
    dest,
    tar: {
      // Don't unpackage .DS_Store files
      ignore: function(name) {
          return path.basename(name) === '.DS_Store'
      }
    },
  }, (err) => {
    if (err) {
      reject(err);
    } else {
      resolve(dest);
    }
  });
});

// Let us work in an async environment
(async () => {
  // Read list of all recipes
  const allJsonPath = path.join(__dirname, '../', 'all.json');
  const all = await fs.readJSON(allJsonPath);
  
  const tempUncompressed = path.join(__dirname, `uncompressed/`);

  for (const recipeInfo of all) {
    // Get recipe infos
    const recipe = recipeInfo.id;
    const recipeNum = all.findIndex(e => e === recipeInfo);
    const compressedRecipe = path.join(__dirname, '../', `${recipe}.tar.gz`);
    const uncompressedRecipe = path.join(__dirname, '../', 'uncompressed', recipe);
  
    // Check that recipe exists
    if (!await fs.pathExists(compressedRecipe) || !await fs.pathExists(uncompressedRecipe)) {
      console.log(`Error: Recipe "${recipe}" exists in all.json but not found.`);
      process.exit(1);
    }
  
    // Clear temporary extraction folder
    if (await fs.pathExists(tempUncompressed)) {
      await fs.remove(tempUncompressed);
    }
    await fs.mkdir(tempUncompressed);
  
    // Package to uncompressed recipe to .tar.gz
    console.log(`Decompressing ${recipe} (${recipeNum + 1} of ${all.length})...`);
    await decompress(compressedRecipe, tempUncompressed);
  
    // Compare directories
    const compare = dircompare.compareSync(uncompressedRecipe, tempUncompressed, {
      compareContent: true,
      // Don't fail because of DS_Store files
      excludeFilter: '.DS_Store',
      ignoreLineEnding: true,
      ignoreWhiteSpaces: true,
    });
  
    if (compare.same) {
      console.log(`✓ ${recipe} is valid`);
    } else {
      console.log(`❌ Compressed and uncompressed files for "${recipe}" are NOT equal:`);

      // Output information about differences
      for (const file of compare.diffSet) {
        if (file.state !== 'equal') {
          console.log(`- "${file.name1 || file.name2}" is not equal (${file.type1} in uncompressed, ${file.type2} in archive)`);

          if (file.name1) {
            const filePath = path.join(file.path1, file.name1);
            console.log('File1:', await fs.readFile(filePath, 'utf-8'));
          }
          if (file.name2) {
            const filePath = path.join(file.path2, file.name2);
            console.log('File2:', await fs.readFile(filePath, 'utf-8'));
          }
        }
      }

      console.log(compare.diffSet);
      
      // TODO: REENABLE!
      //process.exit(1);
    }
  
    // Remove temporary compressed file
    await fs.remove(tempUncompressed);
  }

  console.log('All recipes are valid.');
})();