Skip to content

Commit 6d93de9

Browse files
committed
wip
1 parent 93b04f8 commit 6d93de9

File tree

7 files changed

+417
-274
lines changed

7 files changed

+417
-274
lines changed

packages/api-server/src/cliHandlers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const apiCliOptions = {
2929
port: { default: getConfig().api?.port || 8911, type: 'number', alias: 'p' },
3030
socket: { type: 'string' },
3131
apiRootPath: {
32-
alias: ['rootPath', 'root-path'],
32+
alias: ['api-root-path', 'rootPath', 'root-path'],
3333
default: '/',
3434
type: 'string',
3535
desc: 'Root path where your api functions are served',

packages/api-server/src/index.ts

+31-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
23
import { hideBin } from 'yargs/helpers'
34
import yargs from 'yargs/yargs'
45

@@ -13,29 +14,38 @@ import {
1314

1415
export * from './types'
1516

16-
const positionalArgs = yargs(hideBin(process.argv)).parseSync()._
17-
18-
// "bin": {
19-
// "rw-api-server-watch": "./dist/watch.js",
20-
// "rw-log-formatter": "./dist/logFormatter/bin.js",
21-
// "rw-server": "./dist/index.js"
22-
// },
23-
2417
if (require.main === module) {
25-
if (positionalArgs.includes('api') && !positionalArgs.includes('web')) {
26-
apiServerHandler(
27-
yargs(hideBin(process.argv)).options(apiCliOptions).parseSync()
18+
yargs(hideBin(process.argv))
19+
.scriptName('rw-server')
20+
.usage('usage: $0 <side>')
21+
.strict()
22+
23+
.command(
24+
'$0',
25+
'Run both api and web servers',
26+
// @ts-expect-error just passing yargs though
27+
(yargs) => {
28+
yargs.options(commonOptions)
29+
},
30+
bothServerHandler
2831
)
29-
} else if (
30-
positionalArgs.includes('web') &&
31-
!positionalArgs.includes('api')
32-
) {
33-
webServerHandler(
34-
yargs(hideBin(process.argv)).options(webCliOptions).parseSync()
32+
.command(
33+
'api',
34+
'Start server for serving only the api',
35+
// @ts-expect-error just passing yargs though
36+
(yargs) => {
37+
yargs.options(apiCliOptions)
38+
},
39+
apiServerHandler
3540
)
36-
} else {
37-
bothServerHandler(
38-
yargs(hideBin(process.argv)).options(commonOptions).parseSync()
41+
.command(
42+
'web',
43+
'Start server for serving only the web side',
44+
// @ts-expect-error just passing yargs though
45+
(yargs) => {
46+
yargs.options(webCliOptions)
47+
},
48+
webServerHandler
3949
)
40-
}
50+
.parse()
4151
}

packages/cli/src/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,20 @@ async function runYargs() {
215215
await loadPlugins(yarg)
216216

217217
// Run
218-
await yarg.parse(process.argv.slice(2), {}, (_err, _argv, output) => {
218+
await yarg.parse(process.argv.slice(2), {}, (err, _argv, output) => {
219+
// Configuring yargs with `strict` makes it error on unknown args;
220+
// here we're propagating the exit code.
221+
if (err) {
222+
process.exitCode = 1
223+
}
224+
219225
// Show the output that yargs was going to if there was no callback provided
220226
if (output) {
221-
console.log(output)
227+
if (err) {
228+
console.error(output)
229+
} else {
230+
console.log(output)
231+
}
222232
}
223233
})
224234
}

packages/web-server/src/server.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,38 @@ import path from 'path'
55
import chalk from 'chalk'
66
import { config } from 'dotenv-defaults'
77
import Fastify from 'fastify'
8-
import yargsParser from 'yargs-parser'
8+
import { hideBin } from 'yargs/helpers'
9+
import yargs from 'yargs/yargs'
910

1011
import { getPaths, getConfig } from '@redwoodjs/project-config'
1112

1213
import { redwoodFastifyWeb } from './web'
1314
import { withApiProxy } from './withApiProxy'
1415

15-
interface Opts {
16-
socket?: string
17-
port?: string
18-
apiHost?: string
19-
}
20-
21-
// no help option...
22-
2316
async function serve() {
24-
// Parse server file args
25-
const args = yargsParser(process.argv.slice(2), {
26-
string: ['port', 'socket', 'apiHost'],
27-
alias: { apiHost: ['api-host'], port: ['p'] },
28-
})
29-
30-
const options: Opts = {
31-
socket: args.socket,
32-
port: args.port,
33-
apiHost: args.apiHost,
34-
}
17+
const options = yargs(hideBin(process.argv))
18+
.scriptName('rw-web-server')
19+
.usage('$0', 'Start server for serving only the web side')
20+
.strict()
21+
22+
.options({
23+
port: {
24+
default: getConfig().web?.port || 8910,
25+
type: 'number',
26+
alias: 'p',
27+
},
28+
socket: { type: 'string' },
29+
apiHost: {
30+
alias: 'api-host',
31+
type: 'string',
32+
desc: 'Forward requests from the apiUrl, defined in redwood.toml to this host',
33+
},
34+
})
35+
.parseSync()
3536

3637
const redwoodProjectPaths = getPaths()
3738
const redwoodConfig = getConfig()
3839

39-
const port = options.port ? parseInt(options.port) : redwoodConfig.web.port
4040
const apiUrl = redwoodConfig.web.apiUrl
4141

4242
const tsServer = Date.now()
@@ -84,7 +84,7 @@ async function serve() {
8484
listenOptions = { path: options.socket }
8585
} else {
8686
listenOptions = {
87-
port,
87+
port: options.port,
8888
host: process.env.NODE_ENV === 'production' ? '0.0.0.0' : '::',
8989
}
9090
}
@@ -95,7 +95,7 @@ async function serve() {
9595
if (options.socket) {
9696
console.log(`Web server started on ${options.socket}`)
9797
} else {
98-
console.log(`Web server started on http://localhost:${port}`)
98+
console.log(`Web server started on http://localhost:${options.port}`)
9999
}
100100
})
101101

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`serve web (/Users/dom/projects/redwood/redwood/packages/web-server/dist/server.js) errors out on unknown args 1`] = `
4+
"rw-web-server
5+
6+
Start server for serving only the web side
7+
8+
Options:
9+
--help Show help [boolean]
10+
--version Show version number [boolean]
11+
-p, --port [number] [default: 8910]
12+
--socket [string]
13+
--apiHost, --api-host Forward requests from the apiUrl, defined in
14+
redwood.toml to this host [string]
15+
16+
Unknown arguments: foo, bar, baz
17+
"
18+
`;
19+
20+
exports[`serve web ([
21+
'/Users/dom/projects/redwood/redwood/packages/api-server/dist/index.js',
22+
'web'
23+
]) errors out on unknown args 1`] = `
24+
"rw-server web
25+
26+
Start server for serving only the web side
27+
28+
Options:
29+
--help Show help [boolean]
30+
--version Show version number [boolean]
31+
-p, --port [number] [default: 8910]
32+
--socket [string]
33+
--apiHost, --api-host Forward requests from the apiUrl, defined in
34+
redwood.toml to this host [string]
35+
36+
Unknown arguments: foo, bar, baz
37+
"
38+
`;

tasks/server-tests/jest.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/** @type {import('jest').Config} */
22
const config = {
33
rootDir: '.',
4+
testMatch: ['<rootDir>/*.test.mjs'],
45
testTimeout: 5_000 * 2,
6+
transform: {},
57
}
68

79
module.exports = config

0 commit comments

Comments
 (0)