summaryrefslogtreecommitdiffhomepage
path: root/scripts/build_one_click_apps.js
blob: 5bb0d5a07ca0f11e98381acda0a3c42f7d355afb (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*jshint esversion: 6 */
const path = require('path');
const fs = require('fs-extra');

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

const pathOfDist = path.join(__dirname, '..', `dist`);
//  const pathOfDistV1 = path.join(pathOfDist, 'v1');
const pathOfDistV2 = path.join(pathOfDist, 'v2');
const pathOfDistV3 = path.join(pathOfDist, 'v3');

const pathOfSourceDirectory = path.join(pathOfPublic, 'v2');
const pathOfSourceDirectoryApps = path.join(pathOfSourceDirectory, 'apps');
const pathOfSourceDirectoryLogos = path.join(pathOfSourceDirectory, 'logos');


function createAppList(appsList, pathOfApps) {
    const apps = appsList.filter(v => v.includes('.json'));
    const appDetails = [];

    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 + '');

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

        if (captainVersion + '' === '2') {
            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 = '';

            appDetails[i] = {
                name: apps[i],
                displayName: content.displayName,
                description: content.description,
                logoUrl: apps[i] + '.png'
            };
        } else {
            throw new Error('Unknown captain-version: ' + captainVersion);
        }

    }

    return {
        appList: apps,
        appDetails: appDetails
    };
}


function buildDist() {
    return fs.readdir(pathOfSourceDirectoryApps)
        .then(function (appsFileNames) { // [ app1.json app2.json .... ]

            appsFileNames.forEach(appFileName => {
                fs.copySync(path.join(pathOfSourceDirectoryApps, appFileName), path.join(pathOfDistV2, `apps`, appFileName));
                fs.copySync(path.join(pathOfSourceDirectoryApps, appFileName), path.join(pathOfDistV3, `apps`, appFileName.split('.')[0]));
            });

            fs.copySync(pathOfSourceDirectoryLogos, path.join(pathOfDistV2, `logos`));
            fs.copySync(pathOfSourceDirectoryLogos, path.join(pathOfDistV3, `logos`));

            const allAppsList = createAppList(appsFileNames, pathOfSourceDirectoryApps);
            const v3List = {
                oneClickApps: allAppsList.appDetails
            };
            fs.outputJsonSync(path.join(pathOfDistV2, 'autoGeneratedList.json'), allAppsList);
            fs.outputJsonSync(path.join(pathOfDistV2, 'list'), v3List); // TODO delete oneClickApps: 
            fs.outputJsonSync(path.join(pathOfDistV3, 'list'), v3List);
        })
        .then(function () {
            return fs.copySync(path.join(pathOfPublic, 'CNAME'), path.join(pathOfDist, 'CNAME'))
        });
}


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