Skip to content

Commit

Permalink
chore: simplify new serve functions, fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Nov 16, 2023
1 parent 06a85f4 commit 1804a1d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 16 additions & 8 deletions src/lib.v
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ pub struct CreateOptions {
}

@[params]
pub struct ServeOptions {
pub struct ServeStaticOptions {
port u16 = 4321
}

@[params]
pub struct ServeDevOptions {
// vfmt off
pkg_manager serve.PackageManager // .node || .yarn || .pnpm
// vfmt on
script_name string = 'dev'
}

// A Hint that is passed to the Webview 'set_size' method to determine the window sizing behavior.
pub enum Hint {
// Width and height are default size.
Expand Down Expand Up @@ -138,7 +146,7 @@ pub fn (w &Webview) set_size(width int, height int, hint Hint) {
}

// navigate navigates webview to the given URL. URL may be a properly encoded data URI.
// Example: w.navigate('https://github.com/webview/webview') {
// Example: w.navigate('https://github.com/webview/webview')
// Example: w.navigate('data:text/html,%3Ch1%3EHello%3C%2Fh1%3E')
// Example: w.navigate('file://${@VMODROOT}/index.html')
pub fn (w &Webview) navigate(url string) {
Expand All @@ -155,26 +163,26 @@ pub fn (w &Webview) set_html(html string) {
// Example:
// ```
// // Runs `npm run dev` in the `ui` directory.
// w.serve_dev('ui', .npm, 'dev')!
// w.serve_dev('ui')!
// // Runs `yarn run start` in the `ui` directory (specifying an absolute path).
// w.serve_dev(os.join_path(@VMODROOT, 'ui'), pkg_manager: 'yarn', script_name: 'start')!
// ```
// vfmt off
pub fn (mut w Webview) serve_dev(ui_path string, pkg_manager serve.PackageManger, script_name string) ! {
// vfmt on
pub fn (mut w Webview) serve_dev(ui_path string, opts ServeDevOptions) ! {
if !isnil(w.proc) {
return error('a dev process is already running.
executable: `${w.proc.filename}`
arguments: `${w.proc.args.join(' ')}`
directory: `${w.proc.work_folder}`')
}
mut proc, port := serve.serve_dev(ui_path, pkg_manager, script_name) or { return err }
mut proc, port := serve.serve_dev(ui_path, opts.pkg_manager, opts.script_name) or { return err }
w.proc = proc
w.navigate('http://localhost:${port}')
}

// serve_static serves a UI that has been built into a static site on localhost and
// navigates to it address. Optionally, a port can be specified to serve the site.
// By default, the next free port from `4321` is used.
pub fn (w &Webview) serve_static(ui_build_path string, opts ServeOptions) {
pub fn (w &Webview) serve_static(ui_build_path string, opts ServeStaticOptions) {
port := serve.serve_static(ui_build_path, opts.port)
w.navigate('http://localhost:${port}')
}
Expand Down
4 changes: 2 additions & 2 deletions src/serve/dev.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ module serve
import os
import term

pub enum PackageManger {
pub enum PackageManager {
npm
yarn
pnpm // Good technology but too woke author. Usage not recommended.
}

// serve_dev uses the given package manger to run the given script name and
// navigates to the localhost address on which the application is served.
pub fn serve_dev(ui_path string, pkg_manager PackageManger, script_name string) !(&os.Process, int) {
pub fn serve_dev(ui_path string, pkg_manager PackageManager, script_name string) !(&os.Process, int) {
npm_path := os.find_abs_path_of_executable(pkg_manager.str()) or {
eprintln('failed to find ${pkg_manager}.\nMake sure is executable.')
exit(0)
Expand Down

0 comments on commit 1804a1d

Please sign in to comment.