-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpm-install-peer-dependencies
executable file
·96 lines (86 loc) · 3.34 KB
/
npm-install-peer-dependencies
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
93
94
95
96
#!/usr/bin/env bash
set -euo pipefail
NPM_INSTALL_CMD="${NPM_INSTALL_CMD:-npm install --no-save}"
NPM_SORT_DEV_CMD="${NPM_SORT_DEV_CMD:-npm remove --save-dev some-pkg-that-doesnt-exist}"
# https://stackoverflow.com/a/246128/465684
SOURCE=${BASH_SOURCE[0]}
# resolve $SOURCE until the file is no longer a symlink
while [[ -h ${SOURCE} ]]; do
DIR=$(cd -P $(dirname ${SOURCE}) && pwd)
SOURCE=$(readlink ${SOURCE})
# if $SOURCE was a relative symlink
# we need to resolve it relative to the path where the symlink file was locate
[[ ${SOURCE} != /* ]] && SOURCE=${DIR}/${SOURCE}
done
DIR=$(cd -P $(dirname ${SOURCE}) && pwd)
function exe() {
echo "$(pwd)\$ $*"
"$@"
}
{
node -e " \
peerDeps = require('${DIR}/package.json').peerDependencies; \
Object.keys(peerDeps).forEach(function(name) { \
console.log(name + '\t' + peerDeps[name]); \
}); \
"
node -e " \
peerDeps = require('${DIR}/package.json').optionalPeerDependencies; \
Object.keys(peerDeps).forEach(function(name) { \
console.log(name + '\t' + peerDeps[name] + '\t' + 'optional'); \
}); \
"
} | {
NPM_PEER_DEPS=
while read -r LINE; do
NAME=$(echo "${LINE}" | cut -d $'\t' -f 1)
VSN=$(echo "${LINE}" | cut -d $'\t' -f 2)
OPTIONAL=$(echo "${LINE}" | cut -d $'\t' -f 3)
# is the version already specified as dependency?
IN_PACKAGE_JSON=false
IN_PACKAGE_JSON=$(node -p "try { \
var pkg = require('./package.json'); \
var vsn = (pkg.dependencies || {})[\"${NAME}\"] || (pkg.devDependencies || {})[\"${NAME}\"]; \
vsn === \"${VSN}\"; \
} catch(err){false}" | \
grep -q "^false$" && \
echo "false" || echo "true")
if [[ "${IN_PACKAGE_JSON}" = "true" ]]; then
# and is it already installed (static spec), then skip
node -p "try { \
var pkg = require('./node_modules/${NAME}/package.json'); \
pkg.version === \"${VSN}\"; \
} catch(err){false}" | \
grep -q "^false$" || \
continue
# and is it already installed (range spec), then skip
node -p "try { \
var pkg = require('./node_modules/${NAME}/package.json'); \
(pkg._requested || {}).rawSpec === \"${VSN}\"; \
} catch(err){false}" | \
grep -q "^false$" || \
continue
elif [[ "${OPTIONAL}" = "optional" ]]; then
# not, if optional and no version specified as dependency, then skip
IN_PACKAGE_JSON=false
IN_PACKAGE_JSON=$(node -p "try { \
var pkg = require('./package.json'); \
var vsn = (pkg.dependencies || {})[\"${NAME}\"] || (pkg.devDependencies || {})[\"${NAME}\"]; \
vsn !== undefined; \
} catch(err){false}" | \
grep -q "^false$" && \
echo "false" || echo "true")
[[ "${IN_PACKAGE_JSON}" = "true" ]] || continue
fi
# https://github.com/stedolan/jq/issues/105#issuecomment-437363987
# cat <<< "$(jq ".devDependencies += {\"${NAME}\": \"${VSN}\"}" < package.json)" > package.json
npx json -q -I -f package.json -e "this.devDependencies = this.devDependencies || {}"
npx json -q -I -f package.json -e "this.devDependencies[\"${NAME}\"] = \"${VSN}\""
# NOTE: assumes all peerDeps have semver vers, not git urls, etc
NPM_PEER_DEPS="${NPM_PEER_DEPS} ${NAME}@${VSN}"
done
[[ -z "${NPM_PEER_DEPS}" ]] || {
${NPM_INSTALL_CMD} ${NPM_PEER_DEPS}
${NPM_SORT_DEV_CMD}
}
}