forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint-sprig
executable file
·79 lines (69 loc) · 1.87 KB
/
lint-sprig
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
76
77
78
79
#!/usr/bin/env bash
# lint-sprig
#
# Do some sanity checks on the sprig games.
metadata=games/metadata.json
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
echo "Usage: $0 [games/<name>.js ...]" >&2;
cat <<EOF >&2
Perform sanity check on sprig games. Ensures js files in games directory are
represented in metadata file. Checks name for invalid characters. Suggests a
record to add to metadata if not present, pulling title and author from
javascript file. Checks for associated image file.
Does not read metadata to determine if it references files not present.
EOF
exit 2;
fi
if [[ $# -eq 0 ]]; then
files=$(echo games/*.js);
else
files="$@";
fi
err=0
for game in $files; do
name=$(basename "$game" .js);
if [[ ! "$name" =~ ^[a-zA-Z0-9_\-]+$ ]]; then
echo "warning: Unexpected character in file name '$game'." >&2;
err=1;
fi
if ! grep -c "$name" $metadata > /dev/null; then
echo "warning: '$name' for $game not present in $metadata." >&2;
if ! grep '@title' $game >&-; then
echo "error: no @title in $game";
title="$name";
else
title=$(grep '@title' $game | awk '{ print $2 }');
fi
if ! grep '@author' $game >&-; then
echo "error: no @author in $game";
author="N/A";
else
author=$(grep '@author' $game | awk '{ print $2 }');
fi
img="games/img/$name.png";
if [[ ! -f "$img" ]]; then
echo "warning: no '$img' image found for $game." >&2;
img="";
err=1;
fi
date=$(date +%Y-%m-%d);
echo "Suggested fix: add this record to $metadata." >&2;
cat <<EOF >&2
{
"filename": "$name.js",
"title": "$title",
"author": "$author",
"img": "$img",
"tags": [],
"addedOn": "$date"
},
EOF
err=1
fi
# img="games/img/$name.png"
# if [[ ! -f "$img" ]]; then
# echo "warning: expect image '$img' for $game." >&2;
# err=1
# fi
done
exit $err