refactor: finish moving ssh-* scripts to own installers
[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 --debug <path-to-package>',
8   'Example: node _webi/test.js --debug ./node/'
9 ].join('\n');
10
11 var count = 3;
12 var debug = false;
13
14 if (/\b-?-debug?\b/.test(process.argv.join(' '))) {
15   count += 1;
16   debug = true;
17 }
18
19 if (3 !== process.argv.length) {
20   console.error(usage);
21   process.exit(1);
22 }
23
24 if (/\b-?-h(elp)?\b/.test(process.argv.join(' '))) {
25   console.info(usage);
26   process.exit(0);
27 }
28
29 //
30 // Check for stuff
31 //
32 var os = require('os');
33 var fs = require('fs');
34 var path = require('path');
35 var Releases = require('./releases.js');
36 var uaDetect = require('./ua-detect.js');
37 var pkg = process.argv[2].split('@');
38 var pkgdir = pkg[0];
39 var pkgtag = pkg[1] || '';
40 var nodesMap = {};
41 var nodes = fs.readdirSync(pkgdir);
42 nodes.forEach(function (node) {
43   nodesMap[node] = true;
44 });
45
46 var maxLen = 0;
47 console.info('');
48 console.info('Has the necessary files?');
49 ['README.md', 'releases.js', 'install.sh', 'install.ps1']
50   .map(function (node) {
51     maxLen = Math.max(maxLen, node.length);
52     return node;
53   })
54   .forEach(function (node) {
55     var label = node.padStart(maxLen, ' ');
56     var found = nodesMap[node];
57     if (found) {
58       console.info('\t' + label + ': ✅ found');
59     } else {
60       console.info('\t' + label + ': ❌ not found');
61     }
62   });
63
64 console.info('');
65 Releases.get(path.join(process.cwd(), pkgdir)).then(function (all) {
66   var pkgname = path.basename(pkgdir.replace(/\/$/, ''));
67   var osrel = os.platform() + '-' + os.release();
68   var arch = os.arch();
69   var formats = ['exe', 'xz', 'tar', 'zip'];
70
71   var rel = all.releases.filter(function (rel) {
72     return (
73       formats.filter(function (ext) {
74         return rel.ext.match(ext);
75       })[0] &&
76       'stable' === rel.channel &&
77       rel.os === uaDetect.os(osrel) &&
78       rel.arch === uaDetect.arch(arch) &&
79       (!pkgtag ||
80         rel.tag === pkgtag ||
81         new RegExp('^' + pkgtag).test(rel.version))
82     );
83   })[0];
84   rel.oses = all.oses;
85   rel.arches = all.arches;
86   rel.formats = all.formats;
87
88   if (!rel) {
89     console.error('Error: ❌ no release found for current os, arch, and tag');
90     process.exit(1);
91     return;
92   }
93
94   console.info('');
95   console.info('Found release matching current os, arch, and tag:');
96   console.info(rel);
97   console.info('');
98
99   return Promise.all([
100     Releases.renderBash(pkgdir, rel, {
101       baseurl: 'https://webinstall.dev',
102       pkg: pkgname,
103       tag: pkgtag || '',
104       ver: '',
105       os: osrel,
106       arch,
107       formats: formats
108     }).catch(function () {}),
109     Releases.renderPowerShell(pkgdir, rel, {
110       baseurl: 'https://webinstall.dev',
111       pkg: pkgname,
112       tag: pkgtag || '',
113       ver: '',
114       os: osrel,
115       arch,
116       formats: formats
117     }).catch(function () {})
118   ]).then(function (scripts) {
119     var bashTxt = scripts[0];
120     var ps1Txt = scripts[1];
121     var bashFile = 'install-' + pkgname + '.sh';
122     var ps1File = 'install-' + pkgname + '.ps1';
123
124     if (debug) {
125       bashTxt = (bashTxt || 'echo ERROR').replace(/#set -x/g, 'set -x');
126       ps1Txt = (ps1Txt || 'echo ERROR').replace(
127         /REM REM todo debug/g,
128         'REM todo debug'
129       );
130     }
131     console.info('Do the scripts actually work?');
132     if (bashFile && bashTxt) {
133       fs.writeFileSync(bashFile, bashTxt, 'utf-8');
134       console.info('\tNEEDS MANUAL TEST: bash %s', bashFile);
135     }
136     if (ps1File && ps1Txt) {
137       fs.writeFileSync(ps1File, ps1Txt, 'utf-8');
138       console.info('\tNEEDS MANUAL TEST: powershell.exe %s', ps1File);
139     }
140     console.info('');
141   });
142 });