summaryrefslogtreecommitdiffhomepage
path: root/scripts/validate_json.js
blob: 6e71702f69c988cc68e077380f8a603636118008 (plain)
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
 /*jshint esversion: 6 */
 const path = require('path');
 const fs = require('fs-extra');

 const PUBLIC = `public`;
 const pathOfPublic = path.join(__dirname, '..', PUBLIC);


 // validating version 2
 function validate() {

     const version = '2';
     const pathOfVersion = path.join(pathOfPublic, 'v' + version);
     const pathOfApps = path.join(pathOfVersion, 'apps');

     return fs.readdir(pathOfApps)
         .then(function (items) {

             const apps = items.filter(v => v.includes('.json'));

             for (var i = 0; i < apps.length; i++) {
                 const contentString = fs.readFileSync(path.join(pathOfApps, apps[i]));
                 const content = JSON.parse(contentString);
                 const captainVersion = (content.captainVersion + '');
                 const versionString = (version + '');
                 if (versionString !== captainVersion)
                     throw new Error(`unmatched versions   ${versionString}  ${captainVersion} for ${apps[i]}`);

                 apps[i] = apps[i].replace('.json', '');

                 if (!content.displayName) {
                     content.displayName = apps[i];
                     content.displayName = content.displayName.substr(0, 1).toUpperCase() + content.displayName.substring(1, content.displayName.length);
                 }
                 if (!content.description) content.description = '';

                 const logoFileName = apps[i] + '.png';

                 const logoFullPath = path.join(pathOfVersion, 'logos', logoFileName);

                 if (!fs.existsSync(logoFullPath) ||
                     !fs.statSync(logoFullPath).isFile()) {
                     let printablePath = logoFullPath;
                     printablePath = printablePath.substr(printablePath.indexOf(`/${PUBLIC}`));
                     throw new Error(`Cannot find logo for ${apps[i]} ${printablePath}`);
                 }

                 console.log(`Validated ${apps[i]}`);

             }

         });
 }


 Promise.resolve()
     .then(function () {
         return validate();
     })
     .catch(function (err) {
         console.error(err);
         process.exit(127);
     });