Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds hostname to Snap config params for serve and watch #2922

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/snaps-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@ be served by the development server.
The port to run the development server on. If set to `0`, a random port will
be used.

##### `server.host`

- Type: `string`
- Default: `localhost`

The hostname to run the development server on.

#### `environment`

- Type: `Record<string, unknown>`
Expand Down
6 changes: 5 additions & 1 deletion packages/snaps-cli/src/commands/serve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ const command = {
command: ['serve', 's'],
desc: 'Locally serve Snap file(s) for testing',
builder: (yarg: yargs.Argv) => {
yarg.option('root', builders.root).option('port', builders.port);
yarg
.option('root', builders.root)
.option('port', builders.port)
.option('host', builders.host);
},
handler: async (argv: YargsArgs) =>
serveHandler(argv.context.config, {
port: argv.port ?? argv.context.config.server.port,
host: argv.host ?? argv.context.config.server.host,
}),
};

Expand Down
5 changes: 3 additions & 2 deletions packages/snaps-cli/src/commands/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type ServeOptions = {
* The port to listen on.
*/
port: number;
host: string;
};

/**
Expand All @@ -24,7 +25,7 @@ export async function serveHandler(

// If the `configPort` is `0`, the OS will choose a random port for us, so we
// need to get the port from the server after it starts.
const { port } = await server.listen(options.port);
const { port, host } = await server.listen(options.port, options.host);

info(`The server is listening on http://localhost:${port}.`);
info(`The server is listening on http://${host}:${port}.`);
}
2 changes: 2 additions & 0 deletions packages/snaps-cli/src/commands/watch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ const command = {
.option('serve', builders.serve)
.option('root', builders.root)
.option('port', builders.port)
.option('host', builders.host)
.implies('writeManifest', 'manifest')
.implies('depsToTranspile', 'transpilationMode');
},
handler: async (argv: YargsArgs) =>
watchHandler(argv.context.config, {
port: argv.port,
host: argv.host,
}),
};

Expand Down
12 changes: 10 additions & 2 deletions packages/snaps-cli/src/commands/watch/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ type WatchOptions = {
* The port to listen on.
*/
port?: number;

/**
* The host to listen on.
*/
host?: string;
};

type WatchContext = {
Expand All @@ -37,9 +42,12 @@ const steps: Steps<WatchContext> = [
condition: ({ config }) => config.server.enabled,
task: async ({ config, options, spinner }) => {
const server = getServer(config);
const { port } = await server.listen(options.port ?? config.server.port);
const { port, host } = await server.listen(
options.port ?? config.server.port,
options.host ?? config.server.host,
);

info(`The server is listening on http://localhost:${port}.`, spinner);
info(`The server is listening on http://${host}:${port}.`, spinner);
},
},
{
Expand Down
17 changes: 17 additions & 0 deletions packages/snaps-cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ export type SnapBrowserifyConfig = {
*/
port?: number;

/**
* The host to listen the server on.
*
* @default localhost
*/
host?: number;
darioAnongba marked this conversation as resolved.
Show resolved Hide resolved

/**
* The root directory to serve the snap from.
*
Expand Down Expand Up @@ -312,6 +319,13 @@ export type SnapWebpackConfig = {
* @default 8081
*/
port?: number;

/**
* The host to listen the server on.
*
* @default localhost
*/
host?: string;
};

/**
Expand Down Expand Up @@ -541,6 +555,7 @@ export const SnapsBrowserifyConfigStruct = object({
eval: defaulted(boolean(), true),
manifest: defaulted(boolean(), true),
port: defaulted(number(), 8081),
host: defaulted(string(), 'localhost'),
outfileName: defaulted(string(), 'bundle.js'),
root: defaulted(file(), process.cwd()),
sourceMaps: defaulted(boolean(), false),
Expand Down Expand Up @@ -603,6 +618,7 @@ export const SnapsWebpackConfigStruct = object({
enabled: defaulted(boolean(), true),
root: defaulted(file(), process.cwd()),
port: defaulted(number(), 8081),
host: defaulted(string(), 'localhost'),
}),
{},
),
Expand Down Expand Up @@ -969,6 +985,7 @@ export function getWebpackConfig(
enabled: legacyConfig.cliOptions.serve,
port: legacyConfig.cliOptions.port,
root: legacyConfig.cliOptions.root,
host: legacyConfig.cliOptions.host,
},
stats: {
verbose: false,
Expand Down
1 change: 1 addition & 0 deletions packages/snaps-cli/src/types/yargs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type YargsArgs = {
stripComments: boolean;
transformHtmlComments: boolean;
port: number;
host: string;
dist: string;
src: string;
eval: boolean;
Expand Down
16 changes: 13 additions & 3 deletions packages/snaps-cli/src/webpack/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,24 @@ export function getServer(config: ProcessedConfig) {
* Start the server on the port specified in the config.
*
* @param port - The port to listen on.
* @param host - The host to listen on.
* @returns A promise that resolves when the server is listening. The promise
* resolves to an object with the port and the server instance. Note that if
* the `config.server.port` is `0`, the OS will choose a random port for us,
* so we need to get the port from the server after it starts.
*/
const listen = async (port = config.server.port) => {
const listen = async (
port = config.server.port,
host = config.server.host,
) => {
return new Promise<{
port: number;
host: string;
server: Server;
close: () => Promise<void>;
}>((resolve, reject) => {
try {
server.listen(port, () => {
server.listen(port, host, () => {
const close = async () => {
await new Promise<void>((resolveClose, rejectClose) => {
server.close((closeError) => {
Expand All @@ -188,7 +193,12 @@ export function getServer(config: ProcessedConfig) {
};

const address = server.address() as AddressInfo;
resolve({ port: address.port, server, close });
resolve({
port: address.port,
host: address.address,
server,
close,
});
});
} catch (listenError) {
reject(listenError);
Expand Down
Loading