Skip to content

Commit

Permalink
⚓️ Specify PORT in the CLI (#692)
Browse files Browse the repository at this point in the history
See #691
  • Loading branch information
rowanc1 authored Oct 20, 2023
1 parent 5ede905 commit 2c934d0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/fluffy-toes-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'myst-cli': patch
'mystmd': patch
---

Specify port numbers for app and server ports
2 changes: 2 additions & 0 deletions packages/myst-cli/src/build/site/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type Options = {
headless?: boolean;
checkLinks?: boolean;
yes?: boolean;
port?: number;
serverPort?: number;
writeToc?: boolean;
keepHost?: boolean;
extraLinkTransformers?: LinkTransformer[];
Expand Down
12 changes: 5 additions & 7 deletions packages/myst-cli/src/build/site/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const DEFAULT_START_COMMAND = 'npm run start';
/**
* Creates a content server and a websocket that can reload and log messages to the client.
*/
export async function startContentServer(session: ISession) {
const port = await getPort({ port: portNumbers(3100, 3200) });
export async function startContentServer(session: ISession, opts?: { serverPort?: number }) {
const port = opts?.serverPort ?? (await getPort({ port: portNumbers(3100, 3200) }));
const app = express();
app.use(cors());
app.get('/', (req, res) => {
Expand Down Expand Up @@ -111,7 +111,7 @@ export async function startServer(
const mystTemplate = await getMystTemplate(session, opts);
if (!opts.headless) await installSiteTemplate(session, mystTemplate);
await buildSite(session, opts);
const server = await startContentServer(session);
const server = await startContentServer(session, opts);
const { extraLinkTransformers, extraTransforms, defaultTemplate } = opts;
if (!opts.buildStatic) {
watchContent(session, server.reload, {
Expand All @@ -130,10 +130,8 @@ export async function startServer(
session.log.info(
`\n\n\t✨✨✨ Starting ${mystTemplate.getValidatedTemplateYml().title} ✨✨✨\n\n`,
);
const port = await getPort({ port: portNumbers(3000, 3100) });
const appServer = {
port,
} as AppServer;
const port = opts?.port ?? (await getPort({ port: portNumbers(3000, 3100) }));
const appServer = { port } as AppServer;
await new Promise<void>((resolve) => {
const start = makeExecutable(
mystTemplate.getValidatedTemplateYml().build?.start ?? DEFAULT_START_COMMAND,
Expand Down
25 changes: 24 additions & 1 deletion packages/mystmd/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Option } from 'commander';
import { InvalidArgumentError, Option } from 'commander';

function parseInt(value: any) {
const parsedValue = Number.parseInt(value, 10);
if (isNaN(parsedValue)) throw new InvalidArgumentError('Not a number.');
return parsedValue;
}

export function makePdfOption(description: string) {
return new Option('--pdf', description).default(false);
Expand Down Expand Up @@ -94,6 +100,23 @@ export function makeHeadlessOption() {
).default(false);
}

export function makePortOption() {
return new Option('--port <port>', 'Run the application server from the specified port number')
.argParser(parseInt)
.env('PORT')
.default(undefined);
}

export function makeServerPortOption() {
return new Option(
'--server-port <server-port>',
'Run the content server from the specified port number',
)
.argParser(parseInt)
.env('SERVER_PORT')
.default(undefined);
}

export function makeYesOption() {
return new Option('-y, --yes', 'Automatically respond yes to prompts').default(false);
}
Expand Down
9 changes: 8 additions & 1 deletion packages/mystmd/src/site.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Command } from 'commander';
import { Session, startServer } from 'myst-cli';
import { clirun } from './clirun.js';
import { makeKeepHostOption, makeHeadlessOption } from './options.js';
import {
makeKeepHostOption,
makeHeadlessOption,
makePortOption,
makeServerPortOption,
} from './options.js';

export function makeStartCLI(program: Command) {
const command = new Command('start')
.description('Start the current project as a website')
.addOption(makeKeepHostOption())
.addOption(makeHeadlessOption())
.addOption(makePortOption())
.addOption(makeServerPortOption())
.action(clirun(Session, startServer, program));
return command;
}

0 comments on commit 2c934d0

Please sign in to comment.