Skip to content

Commit 354dfd0

Browse files
committed
Include the bundle without unnecessary deps in the packaged server
1 parent 89ab9f1 commit 354dfd0

File tree

6 files changed

+134
-6
lines changed

6 files changed

+134
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ __pycache__
5656
*.pyc
5757

5858
bundle/
59+
build/

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
export DEPLOY_STEP=1
3636
echo 'About to deploy new tag'
3737
else
38-
# About to deploy latest-manifest
38+
# Change TRAVIS_TAG in the 2nd deploy step
3939
export DEPLOY_STEP=2
4040
echo 'About to deploy manifest update'
4141
export TRAVIS_TAG=latest-manifest
@@ -48,7 +48,7 @@ jobs:
4848
secure: BS5tc2PhyIKsw048EG8TxT5HB/EymS8javixTzdpo18+f0BngDfHaTGD9DrXOzepv+4amTdzy0j7K9TdepejMuRDbg+Z3+8IkD0BefqvnnmZZv4ZQFiIdv5VXsAfS9vj0QOOAAwA/oz9rVdA9BEIN/8MNAXClSUT66ciaHLcBq6sr1N9ZEdqaDSYs8UiBEwA7Qnk6Q2/9EuGGozMNrOH5hlyiP0eKtjn4q/PemdBzemGx5KNWFr5zVVYy8kaMB/ELboOUB0AufaOyexsGWJdXSJwOdWUTYWqKsGGXXzdjcW/AT+8tGr968WNmGvg/d2SI2mIbJKrHVk8lc3tbyVfxngUbpZnHMmqtf3JSgTKeIeBMUlrVsh0gUiOU4yYkK2jITqsliREtrZ/ik9QEkw2VXh90A+iUBzeDhHACvPpA8JlPwwtFWu34zdHOyXk/3kVUDINQkVorQ2txwa2yc68a4nbptprHKUqafG0qw8TIViY16T8cZLm0QYJ9hLI1grBR8D9YpqJysSZcJrhUi/GWppzOZm5X0nxd0zvhG1KbmtIc9UgfoUA8rA2srmR9LEhptmWHCVzOeb1WEovm6cV6iKKUnq9jmbYSJPUuiDQPrUeSSGZneRb9rwHXh6Hg2rTo7R3l97/DPPTNF+q0k+qQrXqPvWZk837VmIVaSiMc/8=
4949
file_glob: true
5050
tag_name: $TRAVIS_TAG
51-
file: dist/v*/*
51+
file: build/dist/v*/*
5252
skip_cleanup: true
5353
on:
5454
repo: httptoolkit/httptoolkit-server
@@ -59,7 +59,7 @@ jobs:
5959
file_glob: true
6060
tag_name: latest-manifest # Fixed release, updated with the latest manifests each time
6161
overwrite: true
62-
file: dist/latest-manifest/*
62+
file: build/dist/latest-manifest/*
6363
skip_cleanup: true
6464
on:
6565
repo: httptoolkit/httptoolkit-server

build-release.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ case "$TARGET_PLATFORM" in
6363
linux)
6464
EXPECTED_BIN_STRING="ELF"
6565
# Builds raw on non-Windows, but never used
66-
PACKAGE_WHITELIST="registry-js"
66+
PACKAGE_WHITELIST="registry.node"
6767
;;
6868
win32)
6969
EXPECTED_BIN_STRING="MS Windows"
@@ -72,7 +72,7 @@ case "$TARGET_PLATFORM" in
7272
darwin)
7373
EXPECTED_BIN_STRING="Mach-O"
7474
# Builds raw on non-Windows, but never used
75-
PACKAGE_WHITELIST="registry-js"
75+
PACKAGE_WHITELIST="registry.node"
7676
;;
7777
*)
7878
echo "Unknown platform $TARGET_PLATFORM"

pack.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import * as path from 'path';
2+
import * as fs from 'fs-extra';
3+
import * as _ from 'lodash';
4+
import { spawn as spawnAsync, SpawnOptions } from 'child_process';
5+
6+
const OUTPUT_DIR = path.join(__dirname, 'build');
7+
8+
const pjson = require(path.join(__dirname, './package.json'));
9+
10+
const spawn = (command: string, args: string[] = [], options?: SpawnOptions) => {
11+
return new Promise((resolve, reject) => {
12+
const proc = spawnAsync(command, args, options);
13+
proc.on('exit', (code) => {
14+
if (code === 0) resolve();
15+
else reject(new Error(
16+
`Spawn ${command} ${args.join(' ')} exited with ${code}`
17+
));
18+
});
19+
});
20+
}
21+
22+
const updateRegistryJs = async (platform: string) => {
23+
// Install registry JS for a given target platform, and copy its native
24+
// module into our bundle.
25+
const registryJsDir = path.join(OUTPUT_DIR, 'node_modules', 'registry-js');
26+
await fs.rmdir(registryJsDir).catch(() => {});
27+
await spawn('npm', [
28+
'install',
29+
'registry-js',
30+
'--no-save'
31+
], {
32+
cwd: OUTPUT_DIR,
33+
stdio: 'inherit',
34+
env: Object.assign({}, process.env, { 'npm_config_platform': platform })
35+
});
36+
await fs.copyFile(
37+
path.join(registryJsDir, 'build', 'Release', 'registry.node'),
38+
path.join(OUTPUT_DIR, 'bundle', 'registry.node'),
39+
);
40+
};
41+
42+
const packageApp = async () => {
43+
console.log('Preparing packaging directory');
44+
await fs.emptyDir(OUTPUT_DIR);
45+
46+
// Copy all normally deployable files:
47+
const filesToCopy = pjson.files;
48+
await Promise.all(filesToCopy.map((file: string) =>
49+
fs.copy(
50+
path.join(__dirname, file),
51+
path.join('build', file)
52+
)
53+
));
54+
55+
await Promise.all([
56+
// Include the packaging & build scripts:
57+
'build-release.sh',
58+
// Include package-lock.json, to keep dependencies locked:
59+
'package-lock.json',
60+
// Add the fully bundled source (not normally packaged by npm):
61+
'bundle'
62+
].map((extraFile) =>
63+
fs.copy(path.join(__dirname, extraFile), path.join(OUTPUT_DIR, extraFile))
64+
));
65+
66+
// Edit the package to replace deps with the bundle:
67+
pjson.files.push('/bundle');
68+
pjson.dependencies = _.pick(pjson.dependencies, pjson.oclif.dependenciesToPackage);
69+
delete pjson.scripts.prepack; // We don't want to rebuild
70+
await fs.writeJson(path.join(OUTPUT_DIR, 'package.json'), pjson);
71+
72+
const buildScript = path.join(OUTPUT_DIR, 'build-release.sh');
73+
74+
// Run build-release in this folder, for each platform:
75+
console.log('Building for Linux');
76+
await updateRegistryJs('linux');
77+
await spawn(buildScript, ['linux'], { cwd: OUTPUT_DIR, stdio: 'inherit' });
78+
79+
console.log('Building for Darwin');
80+
await updateRegistryJs('darwin');
81+
await spawn(buildScript, ['darwin'], { cwd: OUTPUT_DIR, stdio: 'inherit' });
82+
83+
console.log('Building for Win32');
84+
await updateRegistryJs('win32');
85+
await spawn(buildScript, ['win32'], { cwd: OUTPUT_DIR, stdio: 'inherit' });
86+
}
87+
88+
packageApp().catch((error: any) => {
89+
console.error(error.message);
90+
});

package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"start": "node-dev ./bin/run start",
1818
"build": "npm run build:src && npm run build:release",
1919
"build:src": "rm -rf lib && tsc -b",
20-
"build:release": "oclif-dev manifest && ./build-release.sh linux && ./build-release.sh darwin && ./build-release.sh win32",
20+
"build:release": "oclif-dev manifest && webpack && ts-node ./pack.ts",
2121
"prepack": "npm run build:src && oclif-dev manifest",
2222
"test": "TS_NODE_FILES=true mocha -r ts-node/register 'test/**/*.spec.ts'"
2323
},
@@ -52,6 +52,7 @@
5252
"@types/chai": "^4.1.6",
5353
"@types/env-paths": "^1.0.2",
5454
"@types/express": "^4.16.1",
55+
"@types/fs-extra": "^8.0.0",
5556
"@types/lodash": "^4.14.117",
5657
"@types/mocha": "^5.2.5",
5758
"@types/node": "^10.12.0",
@@ -63,6 +64,7 @@
6364
"bent": "^1.5.13",
6465
"chai": "^4.2.0",
6566
"copy-webpack-plugin": "^5.0.4",
67+
"fs-extra": "^8.1.0",
6668
"got": "^9.6.0",
6769
"graphql.js": "^0.6.1",
6870
"mocha": "^5.2.0",
@@ -106,6 +108,13 @@
106108
"plugins": [
107109
"@oclif/plugin-update"
108110
],
111+
"dependenciesToPackage": [
112+
"@oclif/command",
113+
"@oclif/config",
114+
"@oclif/plugin-help",
115+
"@oclif/plugin-update",
116+
"@sentry/node"
117+
],
109118
"update": {
110119
"s3": {
111120
"host": "https://github.com/httptoolkit/httptoolkit-server/releases/download/",

0 commit comments

Comments
 (0)