Skip to content

Instantly share code, notes, and snippets.

@cablehead
Last active July 29, 2025 14:13
Show Gist options
  • Select an option

  • Save cablehead/def5993df09717291e4f33c7dc7b789d to your computer and use it in GitHub Desktop.

Select an option

Save cablehead/def5993df09717291e4f33c7dc7b789d to your computer and use it in GitHub Desktop.

Revisions

  1. cablehead revised this gist Jul 29, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion spawn-xs-test.nu
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Spawn xs serve in a temporary directory, run a closure, then cleanup
    export def main [closure: closure] {
    def .tmp-spawn [closure: closure] {
    # Create a temporary directory
    let tmp_dir = (mktemp -d)
    print $"Created temp directory: ($tmp_dir)"
  2. cablehead created this gist Jul 29, 2025.
    50 changes: 50 additions & 0 deletions spawn-xs-test.nu
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    # Spawn xs serve in a temporary directory, run a closure, then cleanup
    export def main [closure: closure] {
    # Create a temporary directory
    let tmp_dir = (mktemp -d)
    print $"Created temp directory: ($tmp_dir)"

    let store_path = ($tmp_dir | path join "store")

    try {
    # Create store directory
    mkdir $store_path

    # Spawn xs serve in the background
    let job_id = job spawn --tag "xs-test-server" {
    xs serve $store_path
    }
    print $"Started xs serve with job ID: ($job_id)"

    $env.XS_ADDR = $store_path
    $env.XS_CONTEXT = null

    # Give the server a moment to start up
    sleep 50ms

    try {
    # Run the provided closure
    do $closure | print $in
    } catch { |err|
    error make {msg: $"Error in closure: ($err.msg)"}
    }

    # Kill the background job
    job kill $job_id
    print $"Killed xs serve job ($job_id)"

    # Give a moment for the job to shut down
    sleep 50ms

    } catch { |err|
    print $"Error during setup: ($err.msg)"
    }

    # Clean up the temporary directory
    try {
    rm -rf $tmp_dir
    print $"Cleaned up temp directory: ($tmp_dir)"
    } catch { |err|
    print $"Warning: Could not clean up temp directory: ($err.msg)"
    }
    }