|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const https = require('https') |
| 4 | +const fs = require('fs-extra') |
| 5 | +const childProcess = require('child_process') |
| 6 | +const path = require('path') |
| 7 | +const assert = require('assert') |
| 8 | + |
| 9 | +async function secureGet (url) { |
| 10 | + return new Promise((resolve, reject) => { |
| 11 | + https.get(url, response => { |
| 12 | + const statusCode = response.statusCode |
| 13 | + const location = response.headers.location |
| 14 | + |
| 15 | + // Reject if it’s not one of the status codes we are testing. |
| 16 | + if (statusCode !== 200 && statusCode !== 302) { |
| 17 | + reject({statusCode}) |
| 18 | + } |
| 19 | + |
| 20 | + let body = '' |
| 21 | + response.on('data', _ => body += _) |
| 22 | + response.on('end', () => { |
| 23 | + resolve({statusCode, location, body}) |
| 24 | + }) |
| 25 | + }) |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +async function secureStreamToFile (url, filePath) { |
| 30 | + return new Promise((resolve, reject) => { |
| 31 | + const fileStream = fs.createWriteStream(filePath) |
| 32 | + https.get(url, response => { |
| 33 | + response.pipe(fileStream) |
| 34 | + fileStream.on('finish', () => { |
| 35 | + fileStream.close() |
| 36 | + resolve() |
| 37 | + }) |
| 38 | + fileStream.on('error', error => { |
| 39 | + fs.unlinkSync(filePath) |
| 40 | + reject(error) |
| 41 | + }) |
| 42 | + }) |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +// Compare two semver strings (nn.nn.nn) and return 0 if they’re equal, |
| 47 | +// 1 if a > b and -1 if a < b. |
| 48 | +function semverCompare (a, b) { |
| 49 | + const [aMajor, aMinor, aPatch] = a.split('.').map(string => parseInt(string)) |
| 50 | + const [bMajor, bMinor, bPatch] = b.split('.').map(string => parseInt(string)) |
| 51 | + |
| 52 | + const aIsGreaterThanB = |
| 53 | + (aMajor > bMajor) |
| 54 | + || (aMajor === bMajor && aMinor > bMinor) |
| 55 | + || (aMajor === bMajor && aMinor === bMinor && aPatch > bPatch) |
| 56 | + |
| 57 | + return (a === b) ? 0 : aIsGreaterThanB ? 1 : -1 |
| 58 | +} |
| 59 | + |
| 60 | +;(async ()=> { |
| 61 | + console.log('') |
| 62 | + console.log(' Update mkcert-bin script') |
| 63 | + console.log(' ════════════════════════') |
| 64 | + console.log('') |
| 65 | + |
| 66 | + // Get the version of the current release. |
| 67 | + const mkcertBinariesDirectory = path.resolve(path.join(__dirname, '..', 'mkcert-bin')) |
| 68 | + |
| 69 | + if (!fs.existsSync(mkcertBinariesDirectory)) { |
| 70 | + console.log(' Error: No mkcert-bin folder found. Exiting.\n') |
| 71 | + process.exit(1) |
| 72 | + } |
| 73 | + |
| 74 | + const currentMkcertBinaries = fs.readdirSync(mkcertBinariesDirectory) |
| 75 | + |
| 76 | + const currentMkcertVersionMatch = currentMkcertBinaries[0].match(/^mkcert-v(\d+\.\d+\.\d+)-/) |
| 77 | + if (currentMkcertVersionMatch === null) { |
| 78 | + console.log(' Error: Unable to ascertain current mkcert version. Exiting.\n') |
| 79 | + process.exit(1) |
| 80 | + } |
| 81 | + |
| 82 | + const currentMkcertVersion = currentMkcertVersionMatch[1] |
| 83 | + |
| 84 | + console.log(` Current mkcert version: ${currentMkcertVersion}`) |
| 85 | + |
| 86 | + // Get the location of the latest release page. |
| 87 | + const latestMkcertReleasesPage = await secureGet('https://github.com/FiloSottile/mkcert/releases/latest') |
| 88 | + |
| 89 | + assert (latestMkcertReleasesPage.location !== undefined, 'Location must exist (302 redirect).') |
| 90 | + |
| 91 | + // Get the latest release page. |
| 92 | + const actualLatestReleasePage = await secureGet(latestMkcertReleasesPage.location) |
| 93 | + |
| 94 | + assert(actualLatestReleasePage.location === undefined, 'Actual page should not be a redirect.') |
| 95 | + |
| 96 | + const page = actualLatestReleasePage.body |
| 97 | + |
| 98 | + const versionMatch = page.match(/href=\"\/FiloSottile\/mkcert\/releases\/tag\/v(\d+\.\d+\.\d+)\"/) |
| 99 | + |
| 100 | + assert(versionMatch !== null, 'Version should be found on page.') |
| 101 | + |
| 102 | + const latestMkcertVersion = versionMatch[1] |
| 103 | + |
| 104 | + assert(latestMkcertVersion !== undefined, 'Version capturing group should exist.') |
| 105 | + |
| 106 | + console.log(` Latest mkcert version : ${latestMkcertVersion}\n`) |
| 107 | + |
| 108 | + switch(semverCompare(currentMkcertVersion, latestMkcertVersion)) { |
| 109 | + case 0: |
| 110 | + console.log('You already have the latest release version of mkcert included in auto-encrypt-localhost. Exiting.') |
| 111 | + process.exit() |
| 112 | + |
| 113 | + case 1: |
| 114 | + console.log('Warning: It appears you have a later version than the release version included. Exiting.') |
| 115 | + process.exit() |
| 116 | + } |
| 117 | + |
| 118 | + console.log(' Upgrading the binaries to the latest version…\n') |
| 119 | + |
| 120 | + // Delete and recreate the mkcert-bin folder. |
| 121 | + fs.removeSync(mkcertBinariesDirectory) |
| 122 | + fs.mkdirpSync(mkcertBinariesDirectory) |
| 123 | + |
| 124 | + const mkcertReleaseUrlPrefix = `https://github.com/FiloSottile/mkcert/releases/download/v${latestMkcertVersion}` |
| 125 | + |
| 126 | + const latestMkcertBinaries = [ |
| 127 | + { |
| 128 | + platform: 'Linux AMD 64-bit', |
| 129 | + binaryName: `mkcert-v${latestMkcertVersion}-linux-amd64` |
| 130 | + }, |
| 131 | + { |
| 132 | + platform: 'Linux ARM', |
| 133 | + binaryName: `mkcert-v${latestMkcertVersion}-linux-arm` |
| 134 | + }, |
| 135 | + { |
| 136 | + platform: 'Linux ARM64', |
| 137 | + binaryName: `mkcert-v${latestMkcertVersion}-linux-arm64` |
| 138 | + }, |
| 139 | + { |
| 140 | + platform: 'Darwin (masOS) AMD 64-bit', |
| 141 | + binaryName: `mkcert-v${latestMkcertVersion}-darwin-amd64` |
| 142 | + }, |
| 143 | + { |
| 144 | + platform: 'Windows AMD 64-bit', |
| 145 | + binaryName: `mkcert-v${latestMkcertVersion}-windows-amd64.exe` |
| 146 | + } |
| 147 | + ] |
| 148 | + |
| 149 | + for (mkcertBinary of latestMkcertBinaries) { |
| 150 | + const mkcertBinaryUrl = `${mkcertReleaseUrlPrefix}/${mkcertBinary.binaryName}` |
| 151 | + |
| 152 | + console.log(` ${mkcertBinary.platform}`) |
| 153 | + console.log(` ${'─'.repeat(mkcertBinary.platform.length)}`) |
| 154 | + |
| 155 | + console.log(' ├ Downloading binary…') |
| 156 | + |
| 157 | + const binaryRedirectUrl = (await secureGet(mkcertBinaryUrl)).location |
| 158 | + const binaryPath = path.join(mkcertBinariesDirectory, mkcertBinary.binaryName) |
| 159 | + await secureStreamToFile(binaryRedirectUrl, binaryPath) |
| 160 | + |
| 161 | + console.log(` ╰ Upgraded to ${mkcertBinary.binaryName}\n`) |
| 162 | + } |
| 163 | + |
| 164 | + console.log(' Done.\n') |
| 165 | +})() |
0 commit comments