refactor: finish moving ssh-* scripts to own installers
[webi-installers/.git] / _webi / packages.js
1 'use strict';
2
3 var frontmarker = require('./frontmarker.js');
4 var shmatter = require('shmatter');
5 var fs = require('fs');
6 var path = require('path');
7
8 var pkgs = module.exports;
9 pkgs.create = function (Pkgs, basepath) {
10   if (!Pkgs) {
11     Pkgs = {};
12   }
13   if (!basepath) {
14     basepath = path.join(__dirname, '../');
15   }
16
17   Pkgs.all = function () {
18     return fs.promises.readdir(basepath).then(function (nodes) {
19       var items = [];
20       return nodes
21         .reduce(function (p, node) {
22           return p.then(function () {
23             return pkgs.get(node).then(function (meta) {
24               if (meta && '_' !== node[0]) {
25                 meta.name = node;
26                 items.push(meta);
27               }
28             });
29           });
30         }, Promise.resolve())
31         .then(function () {
32           return items;
33         });
34     });
35   };
36
37   Pkgs.get = function (node) {
38     return fs.promises.access(path.join(basepath, node)).then(function () {
39       return Pkgs._get(node);
40     });
41   };
42   Pkgs._get = function (node) {
43     var yash = path.join(basepath, node, 'package.yash');
44     var curlbash = path.join(basepath, node, 'install.sh');
45     var readme = path.join(basepath, node, 'README.md');
46     var winstall = path.join(basepath, node, 'install.ps1');
47     return Promise.all([
48       fs.promises
49         .readFile(readme, 'utf-8')
50         .then(function (txt) {
51           // TODO
52           return frontmarker.parse(txt);
53         })
54         .catch(function (e) {
55           if ('ENOENT' !== e.code && 'ENOTDIR' !== e.code) {
56             console.error("failed to read '" + node + "/README.md'");
57             console.error(e);
58           }
59         }),
60       fs.promises
61         .readFile(yash, 'utf-8')
62         .then(function (txt) {
63           return shmatter.parse(txt);
64         })
65         .catch(function (e) {
66           // no yash package description
67           yash = '';
68           if ('ENOENT' !== e.code && 'ENOTDIR' !== e.code) {
69             console.error("failed to parse '" + node + "/package.yash'");
70             console.error(e);
71           }
72           return fs.promises.readFile(curlbash, 'utf-8').then(function (txt) {
73             return shmatter.parse(txt);
74           });
75         })
76         .catch(function (e) {
77           // no *nix installer
78           curlbash = '';
79           if ('ENOENT' !== e.code && 'ENOTDIR' !== e.code) {
80             console.error("failed to parse '" + node + "/install.sh'");
81             console.error(e);
82           }
83         }),
84       fs.promises.access(winstall).catch(function (e) {
85         // no winstaller
86         winstall = '';
87         if ('ENOENT' !== e.code && 'ENOTDIR' !== e.code) {
88           console.error("failed to read '" + node + "/install.ps1'");
89           console.error(e);
90         }
91       })
92     ]).then(function (items) {
93       var meta = items[0] || items[1];
94       if (!meta) {
95         // doesn't exist
96         return;
97       }
98       meta.windows = !!winstall;
99       meta.bash = !!curlbash;
100
101       return meta;
102     });
103   };
104
105   return Pkgs;
106 };
107 pkgs.create(pkgs);
108
109 if (module === require.main) {
110   pkgs.all().then(function (data) {
111     console.info('package info:');
112     console.info(data);
113   });
114 }