-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.sh
75 lines (66 loc) · 2.05 KB
/
watch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
#
# @(#) v1.0.0 2024-03-09T16:32:05+09:00
# @(#) Copyright (C) 2024 hituzi-no-sippo
# @(#) LICENSE: MIT-0 (https://choosealicense.com/licenses/mit-0/)
# Suppress Lefthook outputs
# References
# - https://github.com/evilmartians/lefthook/blob/49da7f3a9324466270af7c85c3ab16e5b0e8b7ae/docs/configuration.md#skip_output
# - https://github.com/evilmartians/lefthook/blob/49da7f3a9324466270af7c85c3ab16e5b0e8b7ae/docs/usage.md#lefthook_quiet
# v1.6.5 Commit on 2024-03-04
declare -x -r LEFTHOOK_QUIET='meta,skips,empty_summary,success,execution'
declare -r COMMAND_TO_CONVERT='convert-djot-to-html'
watch() {
# Reasons for not running watchexec in Lefthook.
# - You can not stop a watchexec process
# - You can not see results of Lefthook
#
# A Lefthook configuration when tested if watchexec work within Lefthook.
# ``` YAML
# watch-djot:
# parallel: true
# commands:
# devserver:
# run: devserver
# watch-djot:
# run: |
# watchexec --exts djot \
# -- \
# lefthook run convert-djot-to-html
# ```
#
# watchexec options
# - `emit-events-to=stdin`
# Outputs a path of modified file to STDIN.
#
# Do not enclose '--file=$(cut --delimiter=":" --fields=2)'
# with using double quotionmarks (`"`).
# This is because
# if enclose with using double quotionmarks,
# `$(cut --delimiter=":" --fields=2)` do not extract a path of modified file.
watchexec \
--exts=dj \
--poll=1s \
--on-busy-update=do-nothing \
--emit-events-to=stdin \
--postpone \
-- \
"lefthook run $COMMAND_TO_CONVERT" \
'--file="$(cut --delimiter=":" --fields=2)"'
}
main() {
devserver --reload &
declare -r server_PID="$!"
# NOTE
# At this point,
# this script does not convert Djot file that are not managed by Git to HTML.
if lefthook run "$COMMAND_TO_CONVERT"; then
if [ "$1" != '' ]; then
xdg-open "http://localhost:8080/${1%.*}"
fi
watch
lefthook run remove-html-converted-from-djot
fi
kill -s TERM "$server_PID"
}
main "$1"