Skip to content

Instantly share code, notes, and snippets.

@maciejcieslar
Last active November 7, 2018 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maciejcieslar/4dc0f58c47f8e951ca35f6cc1342e268 to your computer and use it in GitHub Desktop.
Save maciejcieslar/4dc0f58c47f8e951ca35f6cc1342e268 to your computer and use it in GitHub Desktop.
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