Skip to content

Commit d9ff6ee

Browse files
committed
feat: simplify CLI usage
This shouldn't break previous valid usages
1 parent dea6ad0 commit d9ff6ee

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ release date when you use `npm version` (see `README.md`).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Simpler ways to use CLI:
13+
14+
- make the only useful `run` comand the default one
15+
- default `location` argument to the current directory
16+
- treat `localhost:8080` as `http://localhost:8080`
17+
18+
Example: `webxdc-dev localhost:8080`
19+
1020
### Changed
1121

1222
- use `@webxdc/types` instead of own types declaration

backend/location.ts

+24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ export function getLocation(location: string): Location {
2828
if (location.startsWith("http://") || location.startsWith("https://")) {
2929
return { type: "url", url: location, dispose: () => {} };
3030
}
31+
32+
// Let's go easy on users and treat `localhost:8080` as
33+
// `http://localhost:8080`
34+
const localhostHostnames = ["localhost", "127.0.0.1", "[::1]"];
35+
const startsWithSomeLocalhost = localhostHostnames.some((host) =>
36+
location.startsWith(host),
37+
);
38+
if (startsWithSomeLocalhost) {
39+
// Let's make sure that it's actually a localhost URL and it's not something
40+
// like `localhost-activities/my-webxdc-app` or `localhost-enjoyers.com`.
41+
42+
let urlObj: URL | undefined;
43+
try {
44+
urlObj = new URL("http://" + location);
45+
} catch (e) {}
46+
47+
const urlHostnameIsLocalhost = localhostHostnames.some(
48+
(localhostString) => urlObj?.hostname === localhostString,
49+
);
50+
if (urlObj && urlHostnameIsLocalhost) {
51+
return { type: "url", url: urlObj.href, dispose: () => {} };
52+
}
53+
}
54+
3155
const parts = location.split("/").filter((part) => part !== "");
3256
const lastPart = parts[parts.length - 1];
3357

backend/program.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ export function createProgram(inject: Inject): Command {
2222
.version(getToolVersion());
2323

2424
program
25-
.command("run")
25+
.command("run", { isDefault: true })
2626
.argument(
27-
"<location>",
27+
"[location]",
2828
"URL with dev server, path to .xdc file, or path to webxdc dist directory",
29+
".", // current directory
2930
)
3031
.option(
3132
"-p, --port <port>",

0 commit comments

Comments
 (0)