Skip to content

Commit 49b11fd

Browse files
committed
Fix the local development environment script
1 parent 9f15e3a commit 49b11fd

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ To get started:
3636
* Clone this repo.
3737
* `npm install`
3838
* To build & run the electron app locally:
39-
* `npm start` - runs the desktop app, downloading the latest server & using the UI from `app.httptoolkit.tech`.
40-
* `npm run start:dev` - runs the desktop app, but using the UI from `localhost:8080` (i.e. assuming you're running your own UI).
39+
* `npm start` - runs the desktop app, downloading the latest live server & using the live UI from `app.httptoolkit.tech`.
40+
* This is useful if you're working on just the desktop app, and want to see your changes with the real live environment.
41+
* `npm run start:dev` - runs the desktop app, with no built-in server using the UI from `localhost:8080`
42+
* This effectively assumes you're bringing your own working UI & server, and is useful for working on this.
43+
* You can start both from the [UI project](https://github.com/httptoolkit/httptoolkit-ui) with just `npm start`, to work on the UI within the desktop app.
44+
* Alternatively, you can run `npm start` in the [server project](https://github.com/httptoolkit/httptoolkit-server), and `npm run start:web` in the UI project, to work on the server or both.
4145
* To build distributable packages:
4246
* `npm run make` - this will attempt to build & package the desktop app for your current platform
4347

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
"description": "HTTP(S) debugging proxy, analyzer and client",
77
"main": "src/index.ts",
88
"scripts": {
9-
"prestart": "node setup-dev.js",
9+
"prestart": "node setup-local.js",
1010
"start": "electron-forge start",
11+
"prestart:dev": "node setup-dev.js",
1112
"start:dev": "APP_URL='http://local.httptoolkit.tech:8080' electron-forge start",
1213
"package": "electron-forge package",
1314
"make": "electron-forge make",

setup-dev.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
require('ts-node/register');
22

33
const { promisify } = require('util');
4+
const path = require('path');
45
const os = require('os');
56
const fs = require('fs');
67
const rimraf = require('rimraf');
7-
const insertServer = promisify(require('./src/after-copy-insert-server'));
88

99
const canAccess = (file) => promisify(fs.access)(file).then(() => true).catch(() => false);
1010
const deleteDir = promisify(rimraf);
11+
const mkdir = promisify(fs.mkdir);
12+
const writeFile = promisify(fs.writeFile);
13+
const chmod = promisify(fs.chmod);
1114

12-
const packageJsonLock = require('./package-lock.json');
13-
const requiredServerVersion = packageJsonLock.dependencies['httptoolkit-server'].version;
15+
const sleepForeverScript = `#!/usr/bin/env node
16+
setInterval(() => {}, 999999999);
17+
`;
1418

15-
// Manually trigger the after-copy hook, to give us an env like the real package
19+
// For a full local dev environment, we want to use a standalone UI & server running externally.
20+
// This lets us edit both and the desktop together. We do this by creating a fake server,
21+
// which doesn't exit, but otherwise does nothing.
1622
async function setUpDevEnv() {
17-
const serverExists = await canAccess('./httptoolkit-server');
18-
const serverVersion = serverExists ? require('./httptoolkit-server/package.json').version : null;
23+
const serverFolder = path.join(__dirname, 'httptoolkit-server');
24+
const serverExists = await canAccess(serverFolder);
25+
if (serverExists) {
26+
await deleteDir(serverFolder);
1927

20-
if (serverVersion !== requiredServerVersion) {
21-
if (serverExists) await deleteDir('./httptoolkit-server');
22-
await insertServer(__dirname, '', os.platform(), os.arch());
23-
console.log('Dev setup completed.');
24-
} else {
25-
console.log('Correct server already downloaded, nothing to do.');
28+
const binFolder = path.join(serverFolder, 'bin');
29+
await mkdir(binFolder, { recursive: true });
30+
31+
const bins = ['httptoolkit-server', 'httptoolkit-server.cmd'].map((bin) => path.join(binFolder, bin));
32+
await Promise.all(bins.map(async (bin) => {
33+
await writeFile(bin, sleepForeverScript);
34+
await chmod(bin, 0o755);
35+
}));
2636
}
2737
}
2838

29-
setUpDevEnv();
39+
setUpDevEnv().catch(e => {
40+
console.error(e);
41+
process.exit(1);
42+
});

setup-local.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require('ts-node/register');
2+
3+
const { promisify } = require('util');
4+
const os = require('os');
5+
const fs = require('fs');
6+
const rimraf = require('rimraf');
7+
const insertServer = promisify(require('./src/after-copy-insert-server'));
8+
9+
const canAccess = (file) => promisify(fs.access)(file).then(() => true).catch(() => false);
10+
const deleteDir = promisify(rimraf);
11+
12+
const packageJsonLock = require('./package-lock.json');
13+
const requiredServerVersion = packageJsonLock.dependencies['httptoolkit-server'].version;
14+
15+
// For local testing of the desktop app, we need to pull the latest server and unpack it.
16+
// This real prod server will then be used with the real prod web UI, but this local desktop app.
17+
// Manually trigger the after-copy hook, to give us an env like the real package
18+
async function setUpLocalEnv() {
19+
const serverExists = await canAccess('./httptoolkit-server');
20+
const serverVersion = serverExists ? require('./httptoolkit-server/package.json').version : null;
21+
22+
if (serverVersion !== requiredServerVersion) {
23+
if (serverExists) await deleteDir('./httptoolkit-server');
24+
await insertServer(__dirname, '', os.platform(), os.arch());
25+
console.log('Local setup completed.');
26+
} else {
27+
console.log('Correct server already downloaded, nothing to do.');
28+
}
29+
}
30+
31+
setUpLocalEnv().catch(e => {
32+
console.error(e);
33+
process.exit(1);
34+
});

0 commit comments

Comments
 (0)