From: AJ ONeal Date: Sat, 2 May 2020 19:37:47 +0000 (+0000) Subject: move ripgrep/releases.js to _common/github.js X-Git-Url: https://git.josue.xyz/?a=commitdiff_plain;h=bee07ff6a9a9fb45aa1eadf4676fa4c33a388db5;p=webi-installers%2F.git move ripgrep/releases.js to _common/github.js --- diff --git a/_common/github.js b/_common/github.js new file mode 100644 index 0000000..09eda59 --- /dev/null +++ b/_common/github.js @@ -0,0 +1,106 @@ +'use strict'; + +// this may need customizations between packages +const osMap = { + macos: /\b(mac|darwin|iPhone|iOS|iPad)/i, + linux: /\b(linux)/i, + win: /\b(win|microsoft|msft)/i, + sunos: /\b(sun)/i, + aix: /\b(aix)/i +}; + +const archMap = { + amd64: /(amd64|x64|[_\-]64)/i, + x86: /\b(x86)(?![_\-]64)/i, + ppc64le: /\b(ppc64le)/i, + ppc64: /\b(ppc64)\b/i, + i686: /\b(i686)\b/i, + arm64: /\b(arm64|arm)/i, + armv7l: /\b(armv?7l)/i, + armv6l: /\b(armv?6l)/i, + s390x: /\b(s390x)/i +}; + +const fileExtMap = { + deb: /\.deb$/i, + pkg: /\.pkg$/i, + exe: /\.exe$/i, + msi: /\.msi$/i, + zip: /\.zip$/i, + tar: /\.(tar(\.?(gz)?)|tgz)/i, + '7z': /\.7;$/i +}; + +/** + * Gets the releases for 'ripgrep'. This function could be trimmed down and made + * for use with any github release. + * + * @param request + * @param {string} owner + * @param {string} repo + * @returns {PromiseLike | Promise} + */ +function getAllReleases(request, owner = 'BurntSushi', repo = 'ripgrep') { + if (!owner) { + return Promise.reject('missing owner for repo'); + } + if (!repo) { + return Promise.reject('missing repo name'); + } + return request({ + url: `https://api.github.com/repos/${owner}/${repo}/releases`, + json: true + }).then((resp) => { + const gHubResp = resp.body; + const all = { + releases: [], + download: '' + }; + + gHubResp.forEach((release) => { + release['assets'].forEach((asset) => { + // set the primary download to the first of the releases + if (all.download === '') { + all.download = asset['browser_download_url']; + } + + const name = asset['name']; + const os = Object.keys(osMap).find(regKey => { + name.match(osMap[regKey]); + }) || 'linux'; + const arch = Object.keys(archMap) + .find(regKey => name.match(archMap[regKey])); + + let fileExt = ''; + Object.keys(fileExtMap).find(regKey => { + const match = name.match(fileExtMap[regKey]); + if (match) { + fileExt = match[0]; + return true; + } + return false; + }); + + all.releases.push({ + download: asset['browser_download_url'], + date: release['published_at'], + version: release['tag_name'], + lts: !release['prerelease'], + ext: fileExt, + arch, + os + }); + }); + }); + + return all; + }); +} + +module.exports = getAllReleases; + +if (module === require.main) { + getAllReleases(require('@root/request'), 'BurntSushi', 'ripgrep').then(function(all) { + console.log(JSON.stringify(all, null, 2)); + }); +} diff --git a/ripgrep/releases.js b/ripgrep/releases.js deleted file mode 100644 index 09eda59..0000000 --- a/ripgrep/releases.js +++ /dev/null @@ -1,106 +0,0 @@ -'use strict'; - -// this may need customizations between packages -const osMap = { - macos: /\b(mac|darwin|iPhone|iOS|iPad)/i, - linux: /\b(linux)/i, - win: /\b(win|microsoft|msft)/i, - sunos: /\b(sun)/i, - aix: /\b(aix)/i -}; - -const archMap = { - amd64: /(amd64|x64|[_\-]64)/i, - x86: /\b(x86)(?![_\-]64)/i, - ppc64le: /\b(ppc64le)/i, - ppc64: /\b(ppc64)\b/i, - i686: /\b(i686)\b/i, - arm64: /\b(arm64|arm)/i, - armv7l: /\b(armv?7l)/i, - armv6l: /\b(armv?6l)/i, - s390x: /\b(s390x)/i -}; - -const fileExtMap = { - deb: /\.deb$/i, - pkg: /\.pkg$/i, - exe: /\.exe$/i, - msi: /\.msi$/i, - zip: /\.zip$/i, - tar: /\.(tar(\.?(gz)?)|tgz)/i, - '7z': /\.7;$/i -}; - -/** - * Gets the releases for 'ripgrep'. This function could be trimmed down and made - * for use with any github release. - * - * @param request - * @param {string} owner - * @param {string} repo - * @returns {PromiseLike | Promise} - */ -function getAllReleases(request, owner = 'BurntSushi', repo = 'ripgrep') { - if (!owner) { - return Promise.reject('missing owner for repo'); - } - if (!repo) { - return Promise.reject('missing repo name'); - } - return request({ - url: `https://api.github.com/repos/${owner}/${repo}/releases`, - json: true - }).then((resp) => { - const gHubResp = resp.body; - const all = { - releases: [], - download: '' - }; - - gHubResp.forEach((release) => { - release['assets'].forEach((asset) => { - // set the primary download to the first of the releases - if (all.download === '') { - all.download = asset['browser_download_url']; - } - - const name = asset['name']; - const os = Object.keys(osMap).find(regKey => { - name.match(osMap[regKey]); - }) || 'linux'; - const arch = Object.keys(archMap) - .find(regKey => name.match(archMap[regKey])); - - let fileExt = ''; - Object.keys(fileExtMap).find(regKey => { - const match = name.match(fileExtMap[regKey]); - if (match) { - fileExt = match[0]; - return true; - } - return false; - }); - - all.releases.push({ - download: asset['browser_download_url'], - date: release['published_at'], - version: release['tag_name'], - lts: !release['prerelease'], - ext: fileExt, - arch, - os - }); - }); - }); - - return all; - }); -} - -module.exports = getAllReleases; - -if (module === require.main) { - getAllReleases(require('@root/request'), 'BurntSushi', 'ripgrep').then(function(all) { - console.log(JSON.stringify(all, null, 2)); - }); -}