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

feat: simplify CLI usage #48

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ release date when you use `npm version` (see `README.md`).

## [Unreleased]

### Added

- Simpler ways to use CLI:

- make the only useful `run` comand the default one
- default `location` argument to the current directory
- treat `localhost:8080` as `http://localhost:8080`

Example: `webxdc-dev localhost:8080`

### Changed

- use `@webxdc/types` instead of own types declaration
Expand Down
24 changes: 24 additions & 0 deletions backend/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ export function getLocation(location: string): Location {
if (location.startsWith("http://") || location.startsWith("https://")) {
return { type: "url", url: location, dispose: () => {} };
}

// Let's go easy on users and treat `localhost:8080` as
// `http://localhost:8080`
const localhostHostnames = ["localhost", "127.0.0.1", "[::1]"];
const startsWithSomeLocalhost = localhostHostnames.some((host) =>
location.startsWith(host),
);
if (startsWithSomeLocalhost) {
// Let's make sure that it's actually a localhost URL and it's not something
// like `localhost-activities/my-webxdc-app` or `localhost-enjoyers.com`.

let urlObj: URL | undefined;
try {
urlObj = new URL("http://" + location);
} catch (e) {}

const urlHostnameIsLocalhost = localhostHostnames.some(
(localhostString) => urlObj?.hostname === localhostString,
);
if (urlObj && urlHostnameIsLocalhost) {
return { type: "url", url: urlObj.href, dispose: () => {} };
}
}

const parts = location.split("/").filter((part) => part !== "");
const lastPart = parts[parts.length - 1];

Expand Down
5 changes: 3 additions & 2 deletions backend/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ export function createProgram(inject: Inject): Command {
.version(getToolVersion());

program
.command("run")
.command("run", { isDefault: true })
.argument(
"<location>",
"[location]",
"URL with dev server, path to .xdc file, or path to webxdc dist directory",
".", // current directory
)
.option(
"-p, --port <port>",
Expand Down
Loading