From b232ee3a84c75302d9987705ed64d84c52ecf234 Mon Sep 17 00:00:00 2001 From: backroot Date: Sun, 4 Oct 2020 12:56:04 +0900 Subject: [PATCH] Support for Vercel and GitHub Private Repo (#62 #39) --- .gitignore | 3 +++ .nowignore => .vercelignore | 0 lib/index.js | 1 + lib/platform.js | 4 ++-- lib/routes.js | 20 ++++++++++++++++++-- lib/server.js | 2 +- now.json | 16 ---------------- package.json | 8 ++++++-- vercel.json | 21 +++++++++++++++++++++ 9 files changed, 52 insertions(+), 23 deletions(-) rename .nowignore => .vercelignore (100%) delete mode 100644 now.json create mode 100644 vercel.json diff --git a/.gitignore b/.gitignore index be81578..2e03c68 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ coverage # dependencies node_modules + +# vercel +.vercel diff --git a/.nowignore b/.vercelignore similarity index 100% rename from .nowignore rename to .vercelignore diff --git a/lib/index.js b/lib/index.js index 631abb7..119bc81 100644 --- a/lib/index.js +++ b/lib/index.js @@ -35,6 +35,7 @@ module.exports = config => { router.get('/download', routes.download) router.get('/download/:platform', routes.downloadPlatform) router.get('/update/:platform/:version', routes.update) + router.get('/update/:platform/:version/:targetFile', routes.update) router.get('/update/win32/:version/RELEASES', routes.releases) return (req, res) => { diff --git a/lib/platform.js b/lib/platform.js index 764d38f..8f4a1ec 100644 --- a/lib/platform.js +++ b/lib/platform.js @@ -6,11 +6,11 @@ module.exports = fileName => { if ( (fileName.includes('mac') || fileName.includes('darwin')) && - extension === 'zip' + (extension === 'yml') ) { return 'darwin' } - const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage'] + const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage', 'zip'] return directCache.find(ext => ext === extension) || false } diff --git a/lib/routes.js b/lib/routes.js index 7334462..2dcb1a3 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -1,5 +1,5 @@ // Native -const urlHelpers = require('url'); +const urlHelpers = require('url') // Packages const { send } = require('micro') @@ -81,6 +81,10 @@ module.exports = ({ cache, config }) => { platform = 'dmg' } + if (platform === 'darwin') { + platform = 'dmg' + } + // Get the latest version from the cache const latest = await loadCache() @@ -110,7 +114,7 @@ module.exports = ({ cache, config }) => { } exports.update = async (req, res) => { - const { platform: platformName, version } = req.params + const { platform: platformName, version, targetFile } = req.params if (!valid(version)) { send(res, 500, { @@ -142,6 +146,18 @@ module.exports = ({ cache, config }) => { return } + if (targetFile) { + if (shouldProxyPrivateDownload) { + for (const platform in latest.platforms) { + if (latest.platforms[platform].name === targetFile) { + proxyPrivateDownload(latest.platforms[platform], req, res) + break + } + } + return + } + } + // Previously, we were checking if the latest version is // greater than the one on the client. However, we // only need to compare if they're different (even if diff --git a/lib/server.js b/lib/server.js index f7869ea..df6db3e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,4 +1,4 @@ -const hazel = require('./index') +const hazel = require('.') const { INTERVAL: interval, diff --git a/now.json b/now.json deleted file mode 100644 index 5d79df6..0000000 --- a/now.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "version": 2, - "name": "hazel", - "builds": [ - { - "src": "lib/server.js", - "use": "@now/node" - } - ], - "routes": [ - { - "src": ".*", - "dest": "/lib/server.js" - } - ] -} diff --git a/package.json b/package.json index cb03f66..8d8f943 100644 --- a/package.json +++ b/package.json @@ -23,18 +23,20 @@ }, "lint-staged": { "*.js": [ - "yarn test && :", + "yarn test --passWithNoTests && :", "prettier --single-quote --no-semi --write --no-editorconfig", "git add" ] }, "dependencies": { + "@vercel/node": "^1.8.2", "async-retry": "1.2.3", "date-fns": "1.29.0", "express-useragent": "1.0.12", "fetch": "1.1.0", "finalhandler": "1.1.0", "handlebars": "4.0.11", + "install": "^0.13.0", "jest": "24.0.0", "micro": "9.3.3", "ms": "2.1.1", @@ -42,7 +44,9 @@ "router": "1.3.2", "semver": "5.5.0", "stream-to-string": "1.1.0", - "test-listen": "1.1.0" + "test-listen": "1.1.0", + "vercel": "^20.1.0", + "yarn": "^1.22.10" }, "devDependencies": { "eslint-config-prettier": "2.9.0", diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..10c151f --- /dev/null +++ b/vercel.json @@ -0,0 +1,21 @@ +{ + "version": 2, + "env": { + "ACCOUNT": "Your GitHub Account", + "REPOSITORY": "Your GirHub Repository", + "TOKEN": "Your GitHub Token", + "URL": "http://localhost:3000" + }, + "builds": [ + { + "src": "lib/*.js", + "use": "@vercel/node" + } + ], + "routes": [ + { + "src": ".*", + "dest": "lib/server.js" + } + ] +}