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