mirror of
https://github.com/webinstall/webi-installers.git
synced 2026-08-19 03:56:16 +00:00
chore: expose helper function & update test
This commit is contained in:
@@ -9,11 +9,28 @@ var Releases = require('./releases.js');
|
||||
// handlers caching and transformation, probably should be broken down
|
||||
var getReleases = require('./transform-releases.js');
|
||||
|
||||
var installersDir = path.join(__dirname, '..');
|
||||
|
||||
serveInstaller.serveInstaller = serveInstaller;
|
||||
serveInstaller.INSTALLERS_DIR = path.join(__dirname, '..');
|
||||
module.exports = serveInstaller;
|
||||
async function serveInstaller(baseurl, ua, pkg, tag, ext, formats, libc) {
|
||||
let [rel, opts] = await serveInstaller.helper({
|
||||
ua,
|
||||
pkg,
|
||||
tag,
|
||||
formats,
|
||||
libc,
|
||||
});
|
||||
Object.assign(opts, {
|
||||
baseurl,
|
||||
});
|
||||
|
||||
var pkgdir = path.join(serveInstaller.INSTALLERS_DIR, pkg);
|
||||
if ('ps1' === ext) {
|
||||
return Releases.renderPowerShell(pkgdir, rel, opts);
|
||||
}
|
||||
return Releases.renderBash(pkgdir, rel, opts);
|
||||
}
|
||||
serveInstaller.helper = async function ({ ua, pkg, tag, formats, libc }) {
|
||||
// TODO put some of this in a middleware? or common function?
|
||||
|
||||
// TODO maybe move package/version/lts/channel detection into getReleases
|
||||
@@ -76,9 +93,7 @@ async function serveInstaller(baseurl, ua, pkg, tag, ext, formats, libc) {
|
||||
let rels = await getReleases(releaseQuery);
|
||||
|
||||
var rel = rels.releases[0];
|
||||
var pkgdir = path.join(installersDir, pkg);
|
||||
var opts = {
|
||||
baseurl,
|
||||
pkg: cfg.alias || pkg,
|
||||
ver,
|
||||
tag,
|
||||
@@ -100,8 +115,5 @@ async function serveInstaller(baseurl, ua, pkg, tag, ext, formats, libc) {
|
||||
rel,
|
||||
);
|
||||
|
||||
if ('ps1' === ext) {
|
||||
return Releases.renderPowerShell(pkgdir, rel, opts);
|
||||
}
|
||||
return Releases.renderBash(pkgdir, rel, opts);
|
||||
}
|
||||
return [rel, opts];
|
||||
};
|
||||
|
||||
+40
-82
@@ -33,7 +33,8 @@ var os = require('os');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var Releases = require('./releases.js');
|
||||
var uaDetect = require('./ua-detect.js');
|
||||
var ServeInstaller = require('./serve-installer.js');
|
||||
|
||||
var pkg = process.argv[2].split('@');
|
||||
var pkgdir = pkg[0];
|
||||
var pkgtag = pkg[1] || '';
|
||||
@@ -42,6 +43,7 @@ var nodes = fs.readdirSync(pkgdir);
|
||||
nodes.forEach(function (node) {
|
||||
nodesMap[node] = true;
|
||||
});
|
||||
var baseurl = 'https://webinstall.dev';
|
||||
|
||||
var maxLen = 0;
|
||||
console.info('');
|
||||
@@ -62,104 +64,60 @@ console.info('Has the necessary files?');
|
||||
});
|
||||
|
||||
console.info('');
|
||||
Releases.get(path.join(process.cwd(), pkgdir)).then(function (all) {
|
||||
Releases.get(path.join(process.cwd(), pkgdir)).then(async function (all) {
|
||||
var pkgname = path.basename(pkgdir.replace(/\/$/, ''));
|
||||
var osrel = os.platform() + '-' + os.release();
|
||||
var arch = os.arch();
|
||||
var libc = 'libc';
|
||||
var nodeOs = os.platform();
|
||||
var nodeOsRelease = os.release();
|
||||
var nodeArch = os.arch();
|
||||
var nodeLibc = 'libc';
|
||||
if (process.platform === 'linux') {
|
||||
nodeLibc = 'gnu';
|
||||
let isUnofficial =
|
||||
process.config.variables.node_release_urlbase.includes('unofficial');
|
||||
if (isUnofficial) {
|
||||
nodeLibc = 'musl';
|
||||
}
|
||||
}
|
||||
var formats = ['exe', 'xz', 'tar', 'zip', 'git'];
|
||||
|
||||
var rel;
|
||||
//var releases = [];
|
||||
for (let _rel of all.releases) {
|
||||
for (let ext of formats) {
|
||||
let isSupported = _rel.ext.match(ext);
|
||||
if (!isSupported) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (_rel.channel !== 'stable') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (_rel.os !== '*') {
|
||||
let curOs = uaDetect.os(osrel);
|
||||
if (_rel.os !== curOs) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (_rel.libc !== 'none') {
|
||||
let curLibc = uaDetect.libc(libc);
|
||||
if (_rel.libc !== curLibc) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (_rel.arch !== '*') {
|
||||
let curArch = uaDetect.arch(arch);
|
||||
if (_rel.arch !== curArch) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (pkgtag) {
|
||||
if (_rel.tag !== pkgtag) {
|
||||
let pkgtagRe = new RegExp('^' + pkgtag);
|
||||
let matchesVersion = pkgtagRe.test(_rel.version);
|
||||
if (!matchesVersion) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rel = _rel;
|
||||
break;
|
||||
}
|
||||
let [rel, opts] = await ServeInstaller.helper({
|
||||
ua: `${nodeOs}/${nodeOsRelease} ${nodeArch}/unknown ${nodeLibc}`,
|
||||
pkg: pkgname,
|
||||
tag: pkgtag || '',
|
||||
formats: formats,
|
||||
libc: nodeLibc,
|
||||
});
|
||||
Object.assign(
|
||||
{
|
||||
ver: '',
|
||||
lts: null,
|
||||
channel: '',
|
||||
os: '',
|
||||
arch: '',
|
||||
limit: 0,
|
||||
},
|
||||
opts,
|
||||
{
|
||||
baseurl,
|
||||
},
|
||||
);
|
||||
|
||||
if (!rel) {
|
||||
console.error(
|
||||
`Error: ❌ no release found for os=${osrel}, arch=${arch}, and tag=${pkgtag}`,
|
||||
`Error: ❌ no release found for @${pkgtag}?os=${nodeOs}&arch=${nodeArch}&libc=${nodeLibc}&formats=${formats}`,
|
||||
);
|
||||
process.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
rel = Object.assign(
|
||||
{
|
||||
oses: all.oses,
|
||||
arches: all.arches,
|
||||
libcs: all.libcs,
|
||||
formats: all.formats,
|
||||
},
|
||||
rel,
|
||||
);
|
||||
|
||||
console.info('');
|
||||
console.info('Found release matching current os, arch, and tag:');
|
||||
console.info(rel);
|
||||
console.info('');
|
||||
|
||||
return Promise.all([
|
||||
Releases.renderBash(pkgdir, rel, {
|
||||
baseurl: 'https://webinstall.dev',
|
||||
pkg: pkgname,
|
||||
tag: pkgtag || '',
|
||||
ver: '',
|
||||
os: osrel,
|
||||
arch,
|
||||
formats: formats,
|
||||
}).catch(function () {}),
|
||||
Releases.renderPowerShell(pkgdir, rel, {
|
||||
baseurl: 'https://webinstall.dev',
|
||||
pkg: pkgname,
|
||||
tag: pkgtag || '',
|
||||
ver: '',
|
||||
os: osrel,
|
||||
arch,
|
||||
formats: formats,
|
||||
}).catch(function () {}),
|
||||
Releases.renderBash(pkgdir, rel, opts).catch(function () {}),
|
||||
Releases.renderPowerShell(pkgdir, rel, opts).catch(function () {}),
|
||||
]).then(function (scripts) {
|
||||
var bashTxt = scripts[0];
|
||||
var ps1Txt = scripts[1];
|
||||
|
||||
Reference in New Issue
Block a user