1

I am trying to start a live-server on demand in any folder temporarily with automator, and kill it when I'm done.

Live server is a node package. I've installed globally. In terminal, if I go to any folder and run; live-server --port=XXXX starts the live server for that folder. But I couldn't manage to run with automator.

I've tried various things;

Automator > Quickaction

Shell Script This one doesn't return any error, but nothing seems to executed.

#!/bin/bash
~/.zprofile live-server --port=5010 --open=$1
osascript -e 'set alertResult to display alert "Live Server running on \n'$1'\n PID: '$$'" buttons {"STOP"} as warning
if button returned of result = "STOP" then
        do shell script "kill -9 '$$'
end if'

AppleScript, If I run it with out watch parameter it works, but it's watching ~/ folder, if I add watch= doesn't work at all (folder not found)

on run {input, parameters}
    set folderPath to (the POSIX path of input)
    tell application "Terminal"
        do script ("live-server --port=5010 --open=" & folderPath as string) & " --watch=" & folderPath as string
        activate
    end tell
    
    return input
end run

Shell Script So I've decided to make a bash script. Automator content;

source ~/.zprofile
~/.nvm/versions/node/v16.19.0/bin/launcher.sh $1

Here is the bash script content. This is working but this time live-server returns 404 (I believe it's about permissions this time)

#!/bin/zsh

source ~/.zprofile
cd ~/.nvm/versions/node/v16.19.0/lib/node_modules/live-server
~/.nvm/versions/node/v16.19.0/bin/node live-server.js --port=5010 --open=${1}/

Can someone explain what is going wrong?

1 Answer 1

0

I've solved the issue. It was a parameter issue. In order to launch a WebServer with Services context menu;

Via Shell Script;

cd ~/.nvm/versions/node/v16.19.0/lib/node_modules/live-server
~/.nvm/versions/node/v16.19.0/bin/node live-server.js --port=5010 ${1}

Via AppleScript

on run {input, parameters}
    set folderPath to (the POSIX path of input)
    tell application "Terminal"
        do script ("live-server --port=5010 " & folderPath as string) & " --watch=" & folderPath as string
        activate
    end tell
    
    return input
end run

Some Information; Although these 2 both valid codes they are working differently. First one, works in the background creates a cog icon in menu bar while running, but If I click on close (cancel) icon it's still running. In order to stop I need to open a terminal window and execute something like kill -9 $(lsof -ti:5010) (not a good experience.)

Second one, opens a terminal window, which isn't pleasant, but If I kill it server stops.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .