Skip to content

Enhancement: Live reloading #3

Description

@nhrones

I have a simple dev-server called Hot. https://jsr.io/@ndh/hot
It auto builds on src change, and hot-reloads the browser.
On start, it auto-opens the browser with the url : port being served.

I really like dsbuild and think a browser-launch feature would be a great enhancement:
https://jsr.io/@ndh/hot/1.1.5/browser.ts

/**
 * Open a web page with the appropriate browser.
 * 
 * This utility is used by a few dev-servers to 
 * auto-launch the browser with the url being served.
 * @module
 */

/**
 * Get a browser open command based on the os
 * @returns the string for the command to call
 */
function getBrowserCmd(): string {
    switch (Deno.build.os) {
       case "windows":
          return "explorer.exe";
       case "darwin":
          return "open";
       case "linux":
          if (Deno.env.get("WSL_DISTRO_NAME")) {
             // is WSL
             return "explorer.exe";
          } else {
             return "xdg-open";
          }
       default:
          return "Unknown os" 
     }
 }
 
 /**
  * Opens a URL in the default browser
  * 
  * @param url  - the complete url to be opened in the browser
  * 
  * @examples 
  *    openWebsite('https://Deno.com')
  *    openWebsite('http://localhost:8080')
  */
 export function openWebsite(url: string): Deno.CommandOutput {
    return new Deno.Command(getBrowserCmd(), 
    { args: [url] }).outputSync();
 }

USAGE: bottom of my server:
https://jsr.io/@ndh/hot/1.1.5/server.ts

import {  openWebsite } from "./browser.ts"
// ....
// ....
// ....  bottom of my server
// launch the browser with our url
openWebsite(`http://localhost:${cfg.Port}`)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions