3 var fs = require('fs');
4 var path = require('path');
5 var request = require('@root/request');
6 var _normalize = require('../_webi/normalize.js');
8 var reInstallTpl = /\s*#?\s*{{ installer }}/;
10 var Releases = module.exports;
11 Releases.get = async function (pkgdir) {
14 get = require(path.join(pkgdir, 'releases.js'));
16 throw new Error('no releases.js for', pkgdir.split(/[\/\\]+/).pop());
18 return get(request).then(function (all) {
19 return _normalize(all);
23 function padScript(txt) {
24 return txt.replace(/^/g, ' ');
27 Releases.renderBash = function (
30 { baseurl, pkg, tag, ver, os, arch, formats }
32 if (!Array.isArray(formats)) {
39 .readFile(path.join(pkgdir, 'install.sh'), 'utf8')
40 .then(function (installTxt) {
41 installTxt = padScript(installTxt);
42 var vers = rel.version.split('.');
44 major: vers.shift() || '',
45 minor: vers.shift() || '',
46 patch: vers.join('.').replace(/[+\-].*/, ''),
49 .replace(/[^+\-]*/, '')
52 var pkgFile = rel.filename || rel.name;
54 .readFile(path.join(__dirname, 'template.sh'), 'utf8')
55 .then(function (tplTxt) {
60 "WEBI_PKG='" + pkg + '@' + ver + "'"
62 .replace(/^\s*#?WEBI_HOST=.*/m, "WEBI_HOST='" + baseurl + "'")
63 .replace(/^\s*#?WEBI_OS=.*/m, "WEBI_OS='" + (os || '') + "'")
65 /^\s*#?WEBI_ARCH=.*/m,
66 "WEBI_ARCH='" + (arch || '') + "'"
68 .replace(/^\s*#?WEBI_TAG=.*/m, "WEBI_TAG='" + tag + "'")
70 /^\s*#?WEBI_RELEASES=.*/m,
107 /^\s*#?WEBI_VERSION=.*/m,
108 'WEBI_VERSION=' + JSON.stringify(rel.version)
110 .replace(/^\s*#?WEBI_MAJOR=.*/m, 'WEBI_MAJOR=' + v.major)
111 .replace(/^\s*#?WEBI_MINOR=.*/m, 'WEBI_MINOR=' + v.minor)
112 .replace(/^\s*#?WEBI_PATCH=.*/m, 'WEBI_PATCH=' + v.patch)
113 .replace(/^\s*#?WEBI_BUILD=.*/m, 'WEBI_BUILD=' + v.build)
114 .replace(/^\s*#?WEBI_LTS=.*/m, 'WEBI_LTS=' + rel.lts)
115 .replace(/^\s*#?WEBI_CHANNEL=.*/m, 'WEBI_CHANNEL=' + rel.channel)
117 /^\s*#?WEBI_EXT=.*/m,
118 'WEBI_EXT=' + rel.ext.replace(/tar.*/, 'tar')
121 /^\s*#?WEBI_FORMATS=.*/m,
122 "WEBI_FORMATS='" + formats.join(',') + "'"
125 /^\s*#?WEBI_PKG_URL=.*/m,
126 "WEBI_PKG_URL='" + rel.download + "'"
129 /^\s*#?WEBI_PKG_FILE=.*/m,
130 "WEBI_PKG_FILE='" + pkgFile + "'"
133 .replace(/^\s*#?PKG_NAME=.*/m, "PKG_NAME='" + pkg + "'")
135 /^\s*#?PKG_OSES=.*/m,
136 "PKG_OSES='" + ((rel && rel.oses) || []).join(',') + "'"
139 /^\s*#?PKG_ARCHES=.*/m,
140 "PKG_ARCHES='" + ((rel && rel.arches) || []).join(',') + "'"
143 /^\s*#?PKG_FORMATS=.*/m,
144 "PKG_FORMATS='" + ((rel && rel.formats) || []).join(',') + "'"
146 .replace(reInstallTpl, '\n' + installTxt)
152 Releases.renderBatch = function (
155 { baseurl, pkg, tag, ver, os, arch, formats }
157 if (!Array.isArray(formats)) {
164 .readFile(path.join(pkgdir, 'install.bat'), 'utf8')
165 .then(function (installTxt) {
166 installTxt = padScript(installTxt);
168 var vers = rel.version.split('.');
170 major: vers.shift() || '',
171 minor: vers.shift() || '',
172 patch: vers.join('.').replace(/[+\-].*$/, ''),
175 .replace(/[^+\-]*()/, '')
180 .readFile(path.join(__dirname, 'template.bat'), 'utf8')
181 .then(function (tplTxt) {
184 /^(REM )?WEBI_PKG=.*/im,
185 "WEBI_PKG='" + pkg + '@' + ver + "'"
187 .replace(reInstallTpl, '\n' + installTxt);
192 Releases.renderPowerShell = function (
195 { baseurl, pkg, tag, ver, os, arch, formats }
197 if (!Array.isArray(formats)) {
204 .readFile(path.join(pkgdir, 'install.ps1'), 'utf8')
205 .then(function (installTxt) {
206 installTxt = padScript(installTxt);
208 var vers = rel.version.split('.');
210 major: vers.shift() || '',
211 minor: vers.shift() || '',
212 patch: vers.join('.').replace(/[+\-].*$/, ''),
215 .replace(/[^+\-]*()/, '')
220 .readFile(path.join(__dirname, 'template.ps1'), 'utf8')
221 .then(function (tplTxt) {
222 var pkgver = pkg + '@' + ver;
225 /^(#)?\$Env:WEBI_HOST\s*=.*/im,
226 "$Env:WEBI_HOST = '" + baseurl + "'"
229 /^(#)?\$Env:WEBI_PKG\s*=.*/im,
230 "$Env:WEBI_PKG = '" + pkgver + "'"
233 /^(#)?\$Env:PKG_NAME\s*=.*/im,
234 "$Env:PKG_NAME = '" + pkg + "'"
237 /^(#)?\$Env:WEBI_VERSION\s*=.*/im,
238 "$Env:WEBI_VERSION = '" + rel.version + "'"
241 /^(#)?\$Env:WEBI_PKG_URL\s*=.*/im,
242 "$Env:WEBI_PKG_URL = '" + rel.download + "'"
245 /^(#)?\$Env:WEBI_PKG_FILE\s*=.*/im,
246 "$Env:WEBI_PKG_FILE = '" + rel.name + "'"
248 .replace(reInstallTpl, '\n' + installTxt);