-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsourcemapper.sh
More file actions
92 lines (75 loc) · 2.69 KB
/
sourcemapper.sh
File metadata and controls
92 lines (75 loc) · 2.69 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# check if a url has been supplied
if [ -z "$1" ]; then
echo "Please supply a sourcemap URL"
exit 1
fi
# store url with sourcemap filename
URL=$1
# store url without sourcemap filename
URLNOMAP=$(echo $URL | rev | cut -d '/' -f2- | rev)
# check if url is sourcemap or js
if [ $(echo $URL | rev | awk -F'.' '{print $1}' | rev) != 'map' ]; then
# If not .map check for a sourcemap reference
MAPFILE=$(curl -s $URL | grep sourceMapping | sed -e 's/.*sourceMappingURL=\([[:alnum:][:punct:]]*\)/\1/')
if [ -z $MAPFILE ]; then
echo "No sourcemap referenced in $URL"
exit 1
fi
URL="$URLNOMAP/$MAPFILE"
echo "Found reference to $URL"
fi
# disable wildcard expansion
set -f
# check if file exists at all
RESP=$(curl -s -o /dev/null -w "%{http_code}" $URL)
if [ "$RESP" != '200' ]; then
echo "Map not found. (HTTP Response Code: $RESP)"
exit 1
fi
# pull contents of the file into $MAP
MAP=$(curl -s "$URL")
# is it even valid json?
echo "$MAP" | jq > /dev/null 2>&1
if [ $? != 0 -a $? != 2 ]; then
echo "Map contains invalid JSON."
exit 1
fi
# Version?
VER=$(echo $MAP | jq '.version' 2> /dev/null)
if [ "$VER" != '3' ]; then
echo "This tool has only been tested with version 3 of the sourcemap spec."
echo "the requested sourcemap returned a version of: $VER. Trying anyway."
fi
echo "Map loaded: read $(echo $MAP | wc -c) bytes from $(echo $URL | rev | awk -F'/' '{print $1}' | rev)."
# get the number of files, the directory structure, and the file contents
LENGTH=$(echo $MAP | jq '.sources[]' 2> /dev/null | wc -l)
CONTENTS=$(echo $MAP | jq '.sourcesContent' 2> /dev/null )
SOURCES=$(echo $MAP | jq '.sources' 2> /dev/null )
echo "$LENGTH files to be written."
COUNTER=$LENGTH
for ((i=0;i<=LENGTH;i++)); do
printf "$COUNTER files remaining."
# for each file: get the path without the filename, remove ../'s, remove quotes
P=$(echo $SOURCES | jq .[$i] | rev | cut -d '/' -f2- | rev | sed 's/\"//g')
# get the filename without the path
F=$(echo $SOURCES | jq .[$i] | rev | awk -F'/' '{print $1}' | rev | sed 's/\"//g')
# check for source in sourcesContent, otherwise get directly from the URL.
DATA=$(echo $CONTENTS | jq .[$i])
if [ "$DATA" == 'null' ]; then
DATA=$(curl -s "$URLNOMAP/$P/$F")
fi
# eliminate ../'s
P=$(echo $P | sed 's/\.\.\///g')
BASE='./sourcemaps'
DATA=$(echo $DATA | sed 's/%/%%/g' | cut --complement -c 1 | rev | cut --complement -c 1,2,3 | rev | sed 's/\"/"/g')
if [ "$P" == "$F" ]; then # these files go in the base directory
echo -ne $DATA > "$BASE/$F"
echo -ne "\rWriting: $BASE/$F\n"
else
mkdir -p "$BASE/$P"
echo -ne $DATA > "$BASE/$P/$F"
echo -ne "\rWriting: $BASE/$P/$F\n"
fi
((COUNTER=COUNTER-1))
done