gkz / LiveScript

LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
http://livescript.net
MIT License
2.32k stars 156 forks source link

CLI --watch option should kill previous process to be useful for long running scripts #1110

Open ceremcem opened 4 years ago

ceremcem commented 4 years ago

The --watch switch in CLI is intended for progressive script compilation and works well for that case. This feature is very useful while developing a long-running server side script as well.

However, it behaves unexpected when executing a long-running script, like with lsc -w myscript.ls. That starts just another instance before killing previous instance.

Expected

lsc -w should kill previous instance (if exists) and then start the new process.

Test case

sleep = (ms, f) -> set-timeout f, ms

i = 0 
console.log "script started."
<~ :lo(op) ~> 
    console.log "Hello #{i++}"
    <~ sleep 1000ms
    if i > 10
        return op!
    lo(op)
console.log "script ended."

Output is:

$ lsc -w test.ls 
script started.
Hello 0
Hello 1
Hello 2
script started. <--- Here the file is saved again
Hello 0
Hello 3
Hello 1
Hello 4
Hello 2
Hello 5
Hello 3
Hello 6
Hello 4
Hello 7
Hello 5
Hello 8
Hello 6
Hello 9
Hello 7
Hello 10
Hello 8
script ended.
Hello 9
Hello 10
script ended.
rhendric commented 4 years ago

FYI, there's no child process involved. lsc evaluates your code in its own process. If you want this behavior, you can modify your own script to cancel any scheduled work whenever the top level runs again, as long as you store the necessary IDs or callbacks on the global object and not just in a variable.

ceremcem commented 4 years ago

If you want this behavior,

FYI: Currently I'm starting LiveScript files via the following Bash script:

#!/bin/bash

watch=
if [[ "$1" = "-w" ]]; then
    echo "Watch mode enabled."
    watch=true
    shift
fi

lsFile="$1"
shift

if [[ -z $watch ]]; then
    lsc $lsFile "$@"
else
    LTIME=
    CMD_PID=

    cleanup(){
        [[ -n $CMD_PID ]] && kill $CMD_PID
    }
    trap cleanup EXIT

    while :; do
       ATIME=`stat -c %Z "$lsFile"`
       if [[ "$ATIME" != "$LTIME" ]]; then
            if [[ -n $CMD_PID ]]; then
                echo "---------------------------------------"
                echo "|             Restarting...           |"
                echo "---------------------------------------"
                kill $CMD_PID
            fi
            lsc $lsFile "$@" &
            CMD_PID=$!
            LTIME=$ATIME
       fi
       sleep 2
    done
fi

Which:

sourcevault commented 3 years ago

@ceremcem watch processes are quite complicated, unfortunately.

My humble advice to use https://github.com/remy/nodemon.

When source script is changed, kills the previous process and starts another one.

Even nodemon struggles here, so something like livescript would definitely not be expected to handle the issue.

The only lib that fixes many of the cross-platform problem here is https://github.com/paulmillr/chokidar.

but you still have to manually tweak the options for different OS and hardware.

Good Luck !