-
Notifications
You must be signed in to change notification settings - Fork 0
/
write-gitignore.sh
65 lines (47 loc) · 1.73 KB
/
write-gitignore.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
#!/usr/bin/env bash
# Add production dependencies and all their dependencies to .gitignore. These dependencies need to be checked in for js
# actions to work.
deps=$(npm pkg get dependencies --json)
allDependencies="[]"
fetchedDepsFor="[]"
function get-deps-recursive() {
package=$1
echo "Fetching deps for $package"
fetchedDepsFor+=($package)
dependencies=$(yarn npm info $package --json | jq '.dependencies')
# if package is scoped, add the namespace to the list of dependencies
packageNamespace=$(echo $package | cut -d'/' -f1)
allDependencies=$(echo $allDependencies | jq ". + [\"$packageNamespace\"]")
allDependencies=$(echo $allDependencies | jq ". + [\"$package\"]")
if [ "$dependencies" == "null" ]; then
return
fi
dependencyKeys=$(echo $dependencies | jq -r 'keys[]')
for dep in $dependencyKeys; do
allDependencies=$(echo $allDependencies | jq ". + [\"$dep\"]")
done
for dep in $dependencyKeys; do
if [[ " ${fetchedDepsFor[@]} " =~ " ${dep} " ]]; then
echo "Skipping $dep as it has already been fetched."
continue
fi
get-deps-recursive $dep
done
}
for dep in $(echo $deps | jq -r 'keys[]'); do
get-deps-recursive $dep
done
# remove duplicates
allDependencies=$(echo $allDependencies | jq 'unique | sort')
startMarker="# <!-- start automatically generated entries -->"
endMarker="# <!-- end automatically generated entries -->"
# delete the old markers and content
sed -i .bak "/$startMarker/,/$endMarker/d" ".gitignore"
rm -f ".gitignore.bak"
# add the new content
echo "$startMarker" >> ".gitignore"
echo "node_modules/*" >> ".gitignore"
for dep in $(echo $allDependencies | jq -r '.[]'); do
echo "!node_modules/$dep/" >> ".gitignore"
done
echo "$endMarker" >> ".gitignore"