Last active
November 7, 2018 19:13
-
-
Save maciejcieslar/4dc0f58c47f8e951ca35f6cc1342e268 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createScriptExec = (script: string) => { | |
let instance = null | |
return async function execute() { | |
if (instance) { | |
await kill(instance, 'SIGKILL') | |
} | |
instance = spawn(script, [], { shell: true }) | |
return merge<String, String>( | |
fromEvent(instance.stderr, 'data').pipe( | |
map((data) => message(data.toString(), 'red')), | |
), | |
fromEvent(instance.stdout, 'data').pipe( | |
map((data) => message(data.toString(), 'cyan')), | |
), | |
).pipe(takeUntil(fromEvent(instance, 'close'))) | |
} | |
} | |
// ... | |
const executeScript = createScriptExec(script) | |
// ... | |
fromEvent(chokidar.watch(process.cwd()), 'all').pipe( | |
// ... | |
switchMap(executeScript), | |
tap(() => console.log(message('Executing...', 'green'))), | |
switchMap((obsvr) => { | |
return obsvr.pipe( | |
tap(console.log), | |
reduce(() => null), | |
) | |
}), | |
tap(() => console.log(message('Finished! :fire:', 'green'))), | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment