forked from httptoolkit/httptoolkit-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We're no longer publishing the server to npm, so this has become very inconvenient and outdated. Although it's often easier to run the server independently, it can still be helpful to have a single script here to get everything going in one go.
- Loading branch information
Showing
6 changed files
with
14,914 additions
and
21,405 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ dist/ | |
.env | ||
meta/ | ||
*.tsbuildinfo | ||
.httptoolkit-server/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import * as path from 'path'; | ||
import * as os from 'os'; | ||
import * as fs from 'fs'; | ||
import { promisify } from 'util'; | ||
|
||
import * as _ from 'lodash'; | ||
import * as semver from 'semver'; | ||
import fetch from 'node-fetch'; | ||
import * as targz from 'targz'; | ||
|
||
const extractTarGz = promisify(targz.decompress); | ||
const deleteFile = promisify(fs.unlink); | ||
|
||
const canAccess = (path: string) => promisify(fs.access)(path).then(() => true).catch(() => false); | ||
|
||
const SERVER_PATH = path.join(__dirname, '..', '.httptoolkit-server'); | ||
|
||
async function setUpLocalEnv() { | ||
const existingServerPackageJsonPath = path.join(SERVER_PATH, 'httptoolkit-server', 'package.json'); | ||
const serverExists = await canAccess(existingServerPackageJsonPath); | ||
const currentServerVersion = serverExists | ||
? require(existingServerPackageJsonPath).version | ||
: null; | ||
|
||
const latestServerDetails = await getLatestServerDetails().catch((e) => { | ||
if (serverExists) { | ||
// If we can ignore this, do - let's just use what we've got | ||
console.log(`Failed to fetch latest server due to '${e.message}' - using existing for now`); | ||
process.exit(0); | ||
} else { | ||
throw e; | ||
} | ||
}); | ||
const latestServerVersion = latestServerDetails.tag_name; | ||
|
||
if (!serverExists || semver.gt(latestServerVersion, currentServerVersion)) { | ||
await downloadServer(latestServerDetails); | ||
console.log('Server setup completed.'); | ||
} else { | ||
console.log('Downloaded server already up to date.'); | ||
} | ||
} | ||
|
||
async function getLatestServerDetails() { | ||
const headers: { Authorization: string } | {} = process.env.GITHUB_TOKEN | ||
? { Authorization: `token ${process.env.GITHUB_TOKEN}` } | ||
: {} | ||
|
||
const response = await fetch( | ||
'https://api.github.com/repos/httptoolkit/httptoolkit-server/releases/latest', | ||
{ headers } | ||
); | ||
if (!response.ok) { | ||
console.log(`${response.status} response, body: `, await response.text(), '\n'); | ||
throw new Error(`Server releases request rejected with ${response.status}`); | ||
} | ||
|
||
return response.json(); | ||
} | ||
|
||
async function downloadServer( | ||
latestServerDetails: any | ||
) { | ||
console.log(`Downloading latest httptoolkit-server...`); | ||
|
||
const platform = os.platform(); | ||
const arch = os.arch(); | ||
const assetRegex = new RegExp(`httptoolkit-server-v[\\d\\.]+-${platform}-${arch}\\.tar\\.gz`); | ||
|
||
if (!latestServerDetails.assets) { | ||
console.error(JSON.stringify(latestServerDetails, null, 2)); | ||
throw new Error('Could not retrieve latest server assets'); | ||
} | ||
|
||
const asset = latestServerDetails.assets | ||
.filter((asset: { name: string }) => asset.name.match(assetRegex))[0]; | ||
|
||
if (!asset) { | ||
throw new Error(`No server download available matching ${assetRegex.toString()}`); | ||
} | ||
|
||
console.log(`Downloading server from ${asset.browser_download_url}...`); | ||
|
||
const downloadPath = path.join(__dirname, 'httptoolkit-server.tar.gz'); | ||
|
||
const assetDownload = await fetch(asset.browser_download_url); | ||
const assetWrite = assetDownload.body.pipe(fs.createWriteStream(downloadPath)); | ||
|
||
await new Promise((resolve, reject) => { | ||
assetWrite.on('finish', resolve); | ||
assetWrite.on('error', reject); | ||
}); | ||
|
||
console.log(`Extracting server to ${SERVER_PATH}`); | ||
await extractTarGz({ src: downloadPath, dest: SERVER_PATH }); | ||
await deleteFile(downloadPath); | ||
|
||
console.log('Server download completed'); | ||
} | ||
|
||
setUpLocalEnv().catch(e => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |
Oops, something went wrong.