|
- import { Argv } from 'yargs';
- import { resolve } from 'path';
-
- const DEFAULT_PORT = 3000 as const;
-
- const buildApp = async () => {
- process.stdout.write('Building app...\n');
-
- const cwd = resolve(__dirname, '..', '..', '..');
- const nextBinPath = resolve(__dirname, '..', '..', '..', 'node_modules', '.bin', 'next');
- const { execa } = await import('execa');
-
- await execa(nextBinPath, ['build'], {
- stdout: 'inherit',
- stderr: 'inherit',
- cwd,
- });
-
- process.stdout.write('done\n');
- };
-
- const serveApp = async (port: number) => {
- process.stdout.write(`Using port: ${port}...\n`);
- process.stdout.write('Serving app...\n');
-
- const cwd = resolve(__dirname, '..', '..', '..');
- const nextBinPath = resolve(__dirname, '..', '..', '..', 'node_modules', '.bin', 'next');
- const { execa } = await import('execa');
-
- await execa(nextBinPath, ['start', '-p', port.toString()], {
- stdout: 'inherit',
- stderr: 'inherit',
- cwd,
- });
- };
-
- export const description = 'Start a development server' as const;
-
- export enum ServeReturnCode {
- SUCCESS = 0,
- }
-
- export interface ServeArgs {
- port?: number;
- subcommands?: string[];
- }
-
- export const builder = (yargs: Argv) => yargs
- .option('port', {
- type: 'number',
- default: DEFAULT_PORT,
- alias: 'p',
- });
-
- const serve = async (args: ServeArgs) => {
- const {
- port = DEFAULT_PORT,
- } = args;
-
- await buildApp();
- await serveApp(port);
- return ServeReturnCode.SUCCESS;
- };
-
- export default serve;
|