Skip to content

Commit

Permalink
Merge pull request #540 from scarf005/refactor-split-mod
Browse files Browse the repository at this point in the history
refactor: extract `getOptionsFromCli`
  • Loading branch information
oscarotero authored Dec 24, 2023
2 parents 0650f0c + e1e8509 commit 4637cfe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
### Changed
- `decap_cms` plugin: Add a script in the homepage to redirect to /admin/
when an invite token or recovery token is detected from netlify identity.
- `getOptionsFromCli` is moved from `mod.ts` to `utils/cli_options.ts`.

### Fixed
- `sitemap` plugin: Add the `xmlns` namespace for localized urls.
Expand Down
42 changes: 42 additions & 0 deletions core/utils/cli_options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { parseArgs } from "../../deps/cli.ts";
import type { DeepPartial } from "./object.ts";
import type { SiteOptions } from "../site.ts";

export function getOptionsFromCli(): DeepPartial<SiteOptions> {
const options = parseArgs(Deno.args, {
string: ["src", "dest", "location", "port"],
boolean: ["serve", "open"],
alias: { dev: "d", serve: "s", port: "p", open: "o" },
["--"]: true,
});

const overrides: DeepPartial<SiteOptions> = {};

if (options.src) {
overrides.src = options.src;
}

if (options.dest) {
overrides.dest = options.dest;
}

if (options.location) {
overrides.location = new URL(options.location);
} else if (options.serve) {
overrides.location = new URL(`http://localhost:${options.port || 3000}/`);
}

if (options.port) {
(overrides.server ||= {}).port = parseInt(options.port);

if (overrides.location) {
overrides.location.port = options.port;
}
}

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

return overrides;
}
41 changes: 1 addition & 40 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { parseArgs } from "./deps/cli.ts";
import Site from "./core/site.ts";
import url, { Options as UrlOptions } from "./plugins/url.ts";
import json, { Options as JsonOptions } from "./plugins/json.ts";
Expand All @@ -13,6 +12,7 @@ import { merge } from "./core/utils/object.ts";

import type { DeepPartial } from "./core/utils/object.ts";
import type { SiteOptions } from "./core/site.ts";
import { getOptionsFromCli } from "./core/utils/cli_options.ts";

export interface PluginOptions {
url?: UrlOptions;
Expand Down Expand Up @@ -60,42 +60,3 @@ export default function lume(
.use(toml(pluginOptions.toml))
.use(yaml(pluginOptions.yaml));
}

function getOptionsFromCli(): DeepPartial<SiteOptions> {
const options = parseArgs(Deno.args, {
string: ["src", "dest", "location", "port"],
boolean: ["serve", "open"],
alias: { dev: "d", serve: "s", port: "p", open: "o" },
["--"]: true,
});

const overrides: DeepPartial<SiteOptions> = {};

if (options.src) {
overrides.src = options.src;
}

if (options.dest) {
overrides.dest = options.dest;
}

if (options.location) {
overrides.location = new URL(options.location);
} else if (options.serve) {
overrides.location = new URL(`http://localhost:${options.port || 3000}/`);
}

if (options.port) {
(overrides.server ||= {}).port = parseInt(options.port);

if (overrides.location) {
overrides.location.port = options.port;
}
}

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

return overrides;
}

0 comments on commit 4637cfe

Please sign in to comment.