generalize, a lot
[webi-installers/.git] / _webi / test.js
1 'use strict';
2
3 //
4 // Print help if there's no pkgdir argument
5 //
6 var usage = [
7   'Usage: node _webi/test.js <path-to-package>',
8   'Example: node _webi/test.js ./node/'
9 ].join('\n');
10
11 if (3 !== process.argv.length) {
12   console.error(usage);
13   process.exit(1);
14 }
15
16 if (/\b-?-h(elp)?\b/.test(process.argv.join(' '))) {
17   console.info(usage);
18   process.exit(0);
19 }
20
21 //
22 // Check for stuff
23 //
24 var os = require('os');
25 var fs = require('fs');
26 var path = require('path');
27 var Releases = require('./releases.js');
28 var uaDetect = require('./ua-detect.js');
29 var pkg = process.argv[2].split('@');
30 var pkgdir = pkg[0];
31 var pkgtag = pkg[1] || '';
32 var nodesMap = {};
33 var nodes = fs.readdirSync(pkgdir);
34 nodes.forEach(function (node) {
35   nodesMap[node] = true;
36 });
37
38 var maxLen = 0;
39 console.info('');
40 console.info('Has the necessary files?');
41 ['package.yash', 'releases.js', 'install.sh', 'install.bat']
42   .map(function (node) {
43     maxLen = Math.max(maxLen, node.length);
44     return node;
45   })
46   .forEach(function (node) {
47     var label = node.padStart(maxLen, ' ');
48     var found = nodesMap[node];
49     if (found) {
50       console.info('\t' + label + ': ✅ found');
51     } else {
52       console.info('\t' + label + ': ❌ not found');
53     }
54   });
55
56 console.info('');
57 Releases.get(path.join(process.cwd(), pkgdir)).then(function (all) {
58   var pkgname = path.basename(pkgdir.replace(/\/$/, ''));
59   var osrel = os.platform() + '-' + os.release();
60   var arch = os.arch();
61   var formats = ['xz', 'tar', 'zip'];
62
63   var rel = all.releases.filter(function (rel) {
64     return (
65       formats.filter(function (ext) {
66         return rel.ext.match(ext);
67       })[0] &&
68       'stable' === rel.channel &&
69       rel.os === uaDetect.os(osrel) &&
70       rel.arch === uaDetect.arch(arch) &&
71       (!pkgtag ||
72         rel.tag === pkgtag ||
73         new RegExp('^' + pkgtag).test(rel.version))
74     );
75   })[0];
76
77   if (!rel) {
78     console.error('Error: ❌ no release found for current os, arch, and tag');
79     process.exit(1);
80     return;
81   }
82
83   console.info('');
84   console.info('Found release matching current os, arch, and tag:');
85   console.info(rel);
86   console.info('');
87
88   return Releases.renderBash(pkgdir, rel, {
89     baseurl: 'https://webinstall.dev',
90     pkg: pkgname,
91     tag: pkgtag || '',
92     ver: '',
93     os: osrel,
94     arch,
95     formats: formats
96   }).then(function (bashTxt) {
97     var bashFile = 'install-' + pkgname + '.sh';
98     var batFile = 'install-' + pkgname + '.bat';
99
100     bashTxt = bashTxt.replace(/#set -x/g, 'set -x');
101     fs.writeFileSync(bashFile, bashTxt, 'utf-8');
102     console.info('Has the necessary files?');
103     console.info('\tNEEDS MANUAL TEST: %s', bashFile);
104     console.info('\t(todo: ' + batFile + ')');
105     console.info('');
106   });
107 });