Skip to content

Commit

Permalink
refactor port & location configuration from cli. fixes #704
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Dec 7, 2024
1 parent a8ef3a8 commit 8315fdb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
- Added tests for middlewares.
- Types errors in Deno 2.1.
- `og_images` plugin: make satori options type partial.
- Fixed `--port` configuration [#704] on serve mode.
- Updates dependencies: `std`, `sass`, `liquidjs`, `tailwindcss`, `postcss`, `lightningcss`, `preact`, `decap-cms`, `unocss`, `magic-string`, `vento`, `satori`, `terser`, `markdown-it-attrs` and some icons.

## [2.4.2] - 2024-11-10
Expand Down Expand Up @@ -617,6 +618,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
[#685]: https://github.com/lumeland/lume/issues/685
[#686]: https://github.com/lumeland/lume/issues/686
[#689]: https://github.com/lumeland/lume/issues/689
[#704]: https://github.com/lumeland/lume/issues/704

[2.4.3]: https://github.com/lumeland/lume/compare/v2.4.2...HEAD
[2.4.2]: https://github.com/lumeland/lume/compare/v2.4.1...v2.4.2
Expand Down
60 changes: 34 additions & 26 deletions core/utils/cli_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function getOptionsFromCli(
const cli = parseArgs(Deno.args, {
string: ["src", "dest", "location", "port"],
boolean: ["serve", "open"],
alias: { dev: "d", serve: "s", port: "p", open: "o" },
alias: { serve: "s", port: "p", open: "o" },
["--"]: true,
});

Expand All @@ -22,40 +22,48 @@ export function getOptionsFromCli(

const serveMode = cli.serve || cli._[0] === "cms";

if (cli.port) {
(options.server ||= {}).port = parseInt(cli.port);
} else if (serveMode) {
(options.server ||= {}).port = 3000;
}

// Detect location and port
let port: number;
let location: URL;

if (cli.location) {
location = new URL(cli.location);
} else if (options.location && !serveMode) {
location = options.location as URL;
} else {
location = new URL("http://localhost");
}

let port: number;
if (serveMode) {
location = cli.location
? new URL(cli.location)
: new URL("http://localhost");

if (cli.port) {
port = parseInt(cli.port);
} else if (location.port) {
port = parseInt(location.port);
} else if (options.server?.port) {
port = options.server.port;
if (cli.port) {
port = parseInt(cli.port);
location.port = port.toString();
} else if (location.port) {
port = parseInt(location.port);
} else if (options.server?.port) {
port = options.server.port;
location.port = port.toString();
} else {
port = location.protocol === "https:" ? 443 : 3000;
location.port = port.toString();
}
} else {
port = cli.serve ? 3000 : location.protocol === "https:" ? 443 : 80;
location = cli.location
? new URL(cli.location)
: (options.location as URL | undefined) || new URL("http://localhost");

if (cli.port) {
port = parseInt(cli.port);
location.port = port.toString();
} else if (location.port) {
port = parseInt(location.port);
} else {
port = location.protocol === "https:" ? 443 : 80;
}
}

(options.server ||= {}).port = port;
location.port = port.toString();
options.location = location;
options.server ||= {};
options.server.port = port;

if (cli.open) {
(options.server ||= {}).open = cli.open;
options.server.open = cli.open;
}

return options;
Expand Down

0 comments on commit 8315fdb

Please sign in to comment.