Skip to content

Commit

Permalink
Merge serve.js with latest on main
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Jun 25, 2023
1 parent 0268f2f commit 1aec335
Showing 1 changed file with 94 additions and 9 deletions.
103 changes: 94 additions & 9 deletions packages/cli/src/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,35 @@ export const builder = async (yargs) => {
const redwoodProjectPaths = getPaths()
const redwoodProjectConfig = getConfig()

const { apiCliOptions, webCliOptions, commonOptions, apiServerHandler } =
await import('@redwoodjs/api-server')

yargs
.usage('usage: $0 <side>')
.command({
command: '$0',
descriptions: 'Run both api and web servers',
handler: streamServerErrorHandler,
builder: (yargs) => yargs.options(commonOptions),
descriptions: 'Run both api and web servers. Uses the web port and host',
handler: (argv) => {
recordTelemetryAttributes({
command,
port: argv.port,
host: argv.host,
socket: argv.socket,
apiHost: argv.apiHost,
})

streamServerErrorHandler()
},
builder: (yargs) =>
yargs.options({
port: {
default: redwoodProjectConfig.web.port,
type: 'number',
alias: 'p',
},
host: {
default: redwoodProjectConfig.web.host,
type: 'string',
},
socket: { type: 'string' },
}),
})
.command({
command: 'both',
Expand Down Expand Up @@ -103,14 +122,80 @@ export const builder = async (yargs) => {
.command({
command: 'api',
description: 'start server for serving only the api',
handler: apiServerHandler,
builder: (yargs) => yargs.options(apiCliOptions),
builder: (yargs) =>
yargs.options({
port: {
default: redwoodProjectConfig.api.port,
type: 'number',
alias: 'p',
},
host: {
default: redwoodProjectConfig.api.host,
type: 'string',
},
socket: { type: 'string' },
apiRootPath: {
alias: ['api-root-path', 'rootPath', 'root-path'],
default: '/',
type: 'string',
desc: 'Root path where your api functions are served',
coerce: coerceRootPath,
},
}),
handler: async (argv) => {
recordTelemetryAttributes({
command,
port: argv.port,
host: argv.host,
socket: argv.socket,
apiRootPath: argv.apiRootPath,
})

// Run the experimental server file, if it exists, api side only
if (hasExperimentalServerFile()) {
console.log(
[
separator,
`🧪 ${chalk.green('Experimental Feature')} 🧪`,
separator,
'Using the experimental API server file at api/dist/server.js',
separator,
].join('\n')
)
await execa('yarn', ['node', path.join('dist', 'server.js')], {
cwd: redwoodProjectPaths.api.base,
stdio: 'inherit',
shell: true,
})
return
}

const { apiServerHandler } = await import('./serveHandler.js')
await apiServerHandler(argv)
},
})
.command({
command: 'web',
description: 'start server for serving only the web side',
builder: (yargs) =>
yargs.options({
port: {
default: redwoodProjectConfig.web.port,
type: 'number',
alias: 'p',
},
host: {
default: redwoodProjectConfig.web.host,
type: 'string',
},
socket: { type: 'string' },
apiHost: {
alias: 'api-host',
type: 'string',
desc: 'Forward requests from the apiUrl, defined in redwood.toml to this host',
},
}),
handler: streamServerErrorHandler,
builder: (yargs) => yargs.options(webCliOptions),
})
.middleware((argv) => {
// Make sure the relevant side has been built, before serving
Expand Down

0 comments on commit 1aec335

Please sign in to comment.