diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cd495b..2ff3848 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/backend/location.ts b/backend/location.ts index a431140..a851fa0 100644 --- a/backend/location.ts +++ b/backend/location.ts @@ -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]; diff --git a/backend/program.ts b/backend/program.ts index 10e07ee..a019f21 100644 --- a/backend/program.ts +++ b/backend/program.ts @@ -22,10 +22,11 @@ export function createProgram(inject: Inject): Command { .version(getToolVersion()); program - .command("run") + .command("run", { isDefault: true }) .argument( - "", + "[location]", "URL with dev server, path to .xdc file, or path to webxdc dist directory", + ".", // current directory ) .option( "-p, --port ",