Skip to content

Commit c0a6137

Browse files
committed
Add http server command to buildtools
1 parent 25fe416 commit c0a6137

File tree

6 files changed

+149
-41
lines changed

6 files changed

+149
-41
lines changed

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Modules Documentation Server
22

3+
Install only the dependencies required for this server: `yarn workspaces focus @sourceacademy/modules-docserver`
4+
35
Run `yarn dev` to run the development version of the server. If need be, run `yarn build-lib-docs` beforehand to build the
46
documentation for the modules library.

lib/buildtools/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"devDependencies": {
77
"@commander-js/extra-typings": "^13.0.0",
88
"@types/estree": "^1.0.0",
9+
"@types/http-server": "^0.12.4",
910
"@types/lodash": "^4.14.198",
1011
"@types/node": "^22.15.30",
1112
"typescript": "^5.8.2"
@@ -26,6 +27,7 @@
2627
"commander": "^13.0.0",
2728
"esbuild": "^0.25.8",
2829
"eslint": "^9.31.0",
30+
"http-server": "^14.1.1",
2931
"jsdom": "^26.1.0",
3032
"lodash": "^4.17.21",
3133
"typedoc": "^0.28.9",

lib/buildtools/src/commands/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { Command } from '@commander-js/extra-typings';
22
import { getBuildCommand, getBuildHtmlCommand, getManifestCommand } from './build.js';
33
import { getListCommand } from './list.js';
44
import { getLintCommand, getLintGlobalCommand, getPrebuildAllCommand, getTscCommand } from './prebuild.js';
5+
import getHttpServerCommand from './server.js';
56
import getTemplateCommand from './template.js';
67
import { getTestAllCommand, getTestCommand } from './testing.js';
78

89
const commands: (() => Command<any>)[] = [
910
getBuildCommand,
1011
getBuildHtmlCommand,
12+
getHttpServerCommand,
1113
getLintCommand,
1214
getLintGlobalCommand,
1315
getListCommand,
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Adapted directly from the http-server package
3+
*/
4+
5+
import { Command, InvalidOptionArgumentError } from '@commander-js/extra-typings';
6+
import { outDir } from '@sourceacademy/modules-repotools/getGitRoot';
7+
import chalk from 'chalk';
8+
import { createServer } from 'http-server';
9+
10+
export default function getHttpServerCommand() {
11+
return new Command('serve')
12+
.option(
13+
'-p, --port <port>',
14+
'Port to serve modules over. If 0, look for open port.',
15+
val => {
16+
const portVal = parseInt(val);
17+
if (portVal < 0 || portVal > 65535) {
18+
throw new InvalidOptionArgumentError(`Invalid port ${val}`);
19+
}
20+
21+
return portVal;
22+
},
23+
8022
24+
)
25+
.option('--bind <address>', 'Address to bind to', '127.0.0.1')
26+
.action(({ port, bind: hostname }) => {
27+
const server = createServer({
28+
cache: -1,
29+
cors: true,
30+
root: outDir,
31+
logFn(req, res, err: any) {
32+
const date = new Date();
33+
const ip = req.headers['x-forwarded-for'] || '' + req.socket.remoteAddress;
34+
if (err) {
35+
console.info(
36+
'[%s] %s "%s %s" Error (%s): "%s"\n',
37+
date,
38+
ip,
39+
chalk.red(req.method),
40+
chalk.red(req.url),
41+
chalk.red(err.status.toString()),
42+
chalk.red(err.message)
43+
);
44+
} else {
45+
console.info(
46+
'[%s] %s "%s %s" "%s\n"',
47+
date,
48+
ip,
49+
chalk.cyan(req.method),
50+
chalk.cyan(req.url),
51+
req.headers['user-agent']
52+
);
53+
}
54+
},
55+
});
56+
57+
const logMessage = [
58+
chalk.bold(chalk.greenBright('Modules Server')),
59+
'\n\n',
60+
chalk.bold('➜ Address: '),
61+
chalk.cyanBright(`http://${hostname}:${port}`),
62+
'\n',
63+
chalk.bold('➜ Build Directory: '),
64+
chalk.cyanBright(outDir),
65+
'\n\n',
66+
chalk.yellowBright('Use Ctrl+C to stop')
67+
];
68+
console.log(logMessage.join(''));
69+
server.listen(port, hostname);
70+
});
71+
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"lint:modules": "yarn workspaces foreach -j 5 -pW --from \"./src/{bundles,tabs}\" run lint",
3838
"prepare": "husky",
3939
"run:bundles": "yarn workspaces foreach -ptW --from \"./src/bundles/*\" run",
40-
"serve": "http-server --cors=* -c-1 -p 8022 ./build",
40+
"serve": "yarn buildtools serve",
4141
"template": "buildtools template",
4242
"test:all": "buildtools testall",
4343
"test:devserver": "yarn workspaces foreach -A --include \"@sourceacademy/modules-devserver\" run test",
@@ -69,7 +69,6 @@
6969
"eslint-plugin-react": "^7.37.4",
7070
"eslint-plugin-react-hooks": "^5.1.0",
7171
"eslint-plugin-yml": "^1.18.0",
72-
"http-server": "^0.13.0",
7372
"husky": "^9.1.7",
7473
"jsdom": "^26.1.0",
7574
"jsonc-eslint-parser": "^2.4.0",

yarn.lock

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,6 +3120,7 @@ __metadata:
31203120
"@commander-js/extra-typings": "npm:^13.0.0"
31213121
"@sourceacademy/modules-repotools": "workspace:^"
31223122
"@types/estree": "npm:^1.0.0"
3123+
"@types/http-server": "npm:^0.12.4"
31233124
"@types/lodash": "npm:^4.14.198"
31243125
"@types/node": "npm:^22.15.30"
31253126
"@vitejs/plugin-react": "npm:^4.5.1"
@@ -3131,6 +3132,7 @@ __metadata:
31313132
commander: "npm:^13.0.0"
31323133
esbuild: "npm:^0.25.8"
31333134
eslint: "npm:^9.31.0"
3135+
http-server: "npm:^14.1.1"
31343136
jsdom: "npm:^26.1.0"
31353137
lodash: "npm:^4.17.21"
31363138
typedoc: "npm:^0.28.9"
@@ -3281,7 +3283,6 @@ __metadata:
32813283
eslint-plugin-react: "npm:^7.37.4"
32823284
eslint-plugin-react-hooks: "npm:^5.1.0"
32833285
eslint-plugin-yml: "npm:^1.18.0"
3284-
http-server: "npm:^0.13.0"
32853286
husky: "npm:^9.1.7"
32863287
js-slang: "npm:^1.0.81"
32873288
jsdom: "npm:^26.1.0"
@@ -3736,6 +3737,15 @@ __metadata:
37363737
languageName: node
37373738
linkType: hard
37383739

3740+
"@types/connect@npm:*":
3741+
version: 3.4.38
3742+
resolution: "@types/connect@npm:3.4.38"
3743+
dependencies:
3744+
"@types/node": "npm:*"
3745+
checksum: 10c0/2e1cdba2c410f25649e77856505cd60223250fa12dff7a503e492208dbfdd25f62859918f28aba95315251fd1f5e1ffbfca1e25e73037189ab85dd3f8d0a148c
3746+
languageName: node
3747+
linkType: hard
3748+
37393749
"@types/d3-array@npm:*":
37403750
version: 3.2.1
37413751
resolution: "@types/d3-array@npm:3.2.1"
@@ -4070,6 +4080,15 @@ __metadata:
40704080
languageName: node
40714081
linkType: hard
40724082

4083+
"@types/http-server@npm:^0.12.4":
4084+
version: 0.12.4
4085+
resolution: "@types/http-server@npm:0.12.4"
4086+
dependencies:
4087+
"@types/connect": "npm:*"
4088+
checksum: 10c0/cb1eae5366c07b71886257f01e88e050f86e5b11af7bb40a6e27c4e63a4e15745477d9443869afa98af44564be64f1ceb0a0effce1f4c3e28f84c3b5a79ed99a
4089+
languageName: node
4090+
linkType: hard
4091+
40734092
"@types/istanbul-lib-coverage@npm:*, @types/istanbul-lib-coverage@npm:^2.0.0":
40744093
version: 2.0.6
40754094
resolution: "@types/istanbul-lib-coverage@npm:2.0.6"
@@ -5702,10 +5721,12 @@ __metadata:
57025721
languageName: node
57035722
linkType: hard
57045723

5705-
"basic-auth@npm:^1.0.3":
5706-
version: 1.1.0
5707-
resolution: "basic-auth@npm:1.1.0"
5708-
checksum: 10c0/af1d7687a1f71acb823c44f6ae8f085dcdb450ff74cb6c98a9a68a3b7a7035050ddac10049d98046771ee4be5c534d5aeeae2304c9c7434f54433260ddef96aa
5724+
"basic-auth@npm:^2.0.1":
5725+
version: 2.0.1
5726+
resolution: "basic-auth@npm:2.0.1"
5727+
dependencies:
5728+
safe-buffer: "npm:5.1.2"
5729+
checksum: 10c0/05f56db3a0fc31c89c86b605231e32ee143fb6ae38dc60616bc0970ae6a0f034172def99e69d3aed0e2c9e7cac84e2d63bc51a0b5ff6ab5fc8808cc8b29923c1
57095730
languageName: node
57105731
linkType: hard
57115732

@@ -6121,7 +6142,7 @@ __metadata:
61216142
languageName: node
61226143
linkType: hard
61236144

6124-
"chalk@npm:^4.0.0, chalk@npm:^4.1.0":
6145+
"chalk@npm:^4.0.0, chalk@npm:^4.1.0, chalk@npm:^4.1.2":
61256146
version: 4.1.2
61266147
resolution: "chalk@npm:4.1.2"
61276148
dependencies:
@@ -6296,13 +6317,6 @@ __metadata:
62966317
languageName: node
62976318
linkType: hard
62986319

6299-
"colors@npm:^1.4.0":
6300-
version: 1.4.0
6301-
resolution: "colors@npm:1.4.0"
6302-
checksum: 10c0/9af357c019da3c5a098a301cf64e3799d27549d8f185d86f79af23069e4f4303110d115da98483519331f6fb71c8568d5688fa1c6523600044fd4a54e97c4efb
6303-
languageName: node
6304-
linkType: hard
6305-
63066320
"comma-separated-tokens@npm:^2.0.0":
63076321
version: 2.0.3
63086322
resolution: "comma-separated-tokens@npm:2.0.3"
@@ -8943,7 +8957,7 @@ __metadata:
89438957
languageName: node
89448958
linkType: hard
89458959

8946-
"he@npm:^1.1.0":
8960+
"he@npm:^1.2.0":
89478961
version: 1.2.0
89488962
resolution: "he@npm:1.2.0"
89498963
bin:
@@ -8997,6 +9011,15 @@ __metadata:
89979011
languageName: node
89989012
linkType: hard
89999013

9014+
"html-encoding-sniffer@npm:^3.0.0":
9015+
version: 3.0.0
9016+
resolution: "html-encoding-sniffer@npm:3.0.0"
9017+
dependencies:
9018+
whatwg-encoding: "npm:^2.0.0"
9019+
checksum: 10c0/b17b3b0fb5d061d8eb15121c3b0b536376c3e295ecaf09ba48dd69c6b6c957839db124fe1e2b3f11329753a4ee01aa7dedf63b7677999e86da17fbbdd82c5386
9020+
languageName: node
9021+
linkType: hard
9022+
90009023
"html-encoding-sniffer@npm:^4.0.0":
90019024
version: 4.0.0
90029025
resolution: "html-encoding-sniffer@npm:4.0.0"
@@ -9037,7 +9060,7 @@ __metadata:
90379060
languageName: node
90389061
linkType: hard
90399062

9040-
"http-proxy@npm:^1.18.0":
9063+
"http-proxy@npm:^1.18.1":
90419064
version: 1.18.1
90429065
resolution: "http-proxy@npm:1.18.1"
90439066
dependencies:
@@ -9048,26 +9071,26 @@ __metadata:
90489071
languageName: node
90499072
linkType: hard
90509073

9051-
"http-server@npm:^0.13.0":
9052-
version: 0.13.0
9053-
resolution: "http-server@npm:0.13.0"
9074+
"http-server@npm:^14.1.1":
9075+
version: 14.1.1
9076+
resolution: "http-server@npm:14.1.1"
90549077
dependencies:
9055-
basic-auth: "npm:^1.0.3"
9056-
colors: "npm:^1.4.0"
9078+
basic-auth: "npm:^2.0.1"
9079+
chalk: "npm:^4.1.2"
90579080
corser: "npm:^2.0.1"
9058-
he: "npm:^1.1.0"
9059-
http-proxy: "npm:^1.18.0"
9081+
he: "npm:^1.2.0"
9082+
html-encoding-sniffer: "npm:^3.0.0"
9083+
http-proxy: "npm:^1.18.1"
90609084
mime: "npm:^1.6.0"
9061-
minimist: "npm:^1.2.5"
9085+
minimist: "npm:^1.2.6"
90629086
opener: "npm:^1.5.1"
9063-
portfinder: "npm:^1.0.25"
9087+
portfinder: "npm:^1.0.28"
90649088
secure-compare: "npm:3.0.1"
90659089
union: "npm:~0.5.0"
9066-
url-join: "npm:^2.0.5"
9090+
url-join: "npm:^4.0.1"
90679091
bin:
9068-
hs: bin/http-server
90699092
http-server: bin/http-server
9070-
checksum: 10c0/e54485a55bd29ffdca8c54228fc415e10848e85d84986570d6cf663086213be27665dd12396ce6fefec814a8e8a63d47d9a943b6ebc921afe93aa8a7e46a0c94
9093+
checksum: 10c0/c5770ddd722dd520ce0af25efee6bfb7c6300ff4e934636d4eec83fa995739e64de2e699e89e7a795b3a1894bcc37bec226617c1023600aacd7871fd8d6ffe6d
90719094
languageName: node
90729095
linkType: hard
90739096

@@ -12118,7 +12141,7 @@ __metadata:
1211812141
languageName: node
1211912142
linkType: hard
1212012143

12121-
"portfinder@npm:^1.0.25":
12144+
"portfinder@npm:^1.0.28":
1212212145
version: 1.0.37
1212312146
resolution: "portfinder@npm:1.0.37"
1212412147
dependencies:
@@ -12930,20 +12953,20 @@ __metadata:
1293012953
languageName: node
1293112954
linkType: hard
1293212955

12956+
"safe-buffer@npm:5.1.2, safe-buffer@npm:~5.1.0, safe-buffer@npm:~5.1.1":
12957+
version: 5.1.2
12958+
resolution: "safe-buffer@npm:5.1.2"
12959+
checksum: 10c0/780ba6b5d99cc9a40f7b951d47152297d0e260f0df01472a1b99d4889679a4b94a13d644f7dbc4f022572f09ae9005fa2fbb93bbbd83643316f365a3e9a45b21
12960+
languageName: node
12961+
linkType: hard
12962+
1293312963
"safe-buffer@npm:^5.0.1, safe-buffer@npm:^5.1.0, safe-buffer@npm:^5.1.1, safe-buffer@npm:^5.1.2, safe-buffer@npm:^5.2.0, safe-buffer@npm:^5.2.1, safe-buffer@npm:~5.2.0":
1293412964
version: 5.2.1
1293512965
resolution: "safe-buffer@npm:5.2.1"
1293612966
checksum: 10c0/6501914237c0a86e9675d4e51d89ca3c21ffd6a31642efeba25ad65720bce6921c9e7e974e5be91a786b25aa058b5303285d3c15dbabf983a919f5f630d349f3
1293712967
languageName: node
1293812968
linkType: hard
1293912969

12940-
"safe-buffer@npm:~5.1.0, safe-buffer@npm:~5.1.1":
12941-
version: 5.1.2
12942-
resolution: "safe-buffer@npm:5.1.2"
12943-
checksum: 10c0/780ba6b5d99cc9a40f7b951d47152297d0e260f0df01472a1b99d4889679a4b94a13d644f7dbc4f022572f09ae9005fa2fbb93bbbd83643316f365a3e9a45b21
12944-
languageName: node
12945-
linkType: hard
12946-
1294712970
"safe-push-apply@npm:^1.0.0":
1294812971
version: 1.0.0
1294912972
resolution: "safe-push-apply@npm:1.0.0"
@@ -14610,10 +14633,10 @@ __metadata:
1461014633
languageName: node
1461114634
linkType: hard
1461214635

14613-
"url-join@npm:^2.0.5":
14614-
version: 2.0.5
14615-
resolution: "url-join@npm:2.0.5"
14616-
checksum: 10c0/4551af5942417c98db5eb43d1eaae65686ddcb7b9374241e93eea2b74e9b7f069c8eb0eac405eea9db496e2a79d73a805e0b85d7ab0b6238d6d771cd926c0dde
14636+
"url-join@npm:^4.0.1":
14637+
version: 4.0.1
14638+
resolution: "url-join@npm:4.0.1"
14639+
checksum: 10c0/ac65e2c7c562d7b49b68edddcf55385d3e922bc1dd5d90419ea40b53b6de1607d1e45ceb71efb9d60da02c681d13c6cb3a1aa8b13fc0c989dfc219df97ee992d
1461714640
languageName: node
1461814641
linkType: hard
1461914642

@@ -15054,6 +15077,15 @@ __metadata:
1505415077
languageName: node
1505515078
linkType: hard
1505615079

15080+
"whatwg-encoding@npm:^2.0.0":
15081+
version: 2.0.0
15082+
resolution: "whatwg-encoding@npm:2.0.0"
15083+
dependencies:
15084+
iconv-lite: "npm:0.6.3"
15085+
checksum: 10c0/91b90a49f312dc751496fd23a7e68981e62f33afe938b97281ad766235c4872fc4e66319f925c5e9001502b3040dd25a33b02a9c693b73a4cbbfdc4ad10c3e3e
15086+
languageName: node
15087+
linkType: hard
15088+
1505715089
"whatwg-encoding@npm:^3.1.1":
1505815090
version: 3.1.1
1505915091
resolution: "whatwg-encoding@npm:3.1.1"

0 commit comments

Comments
 (0)