This repository has been archived by the owner on Nov 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm-install-peer-dependencies
executable file
·70 lines (62 loc) · 2.52 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
#!/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]); \
}); \
" | {
NPM_PEER_DEPS=
while read LINE; do
NAME=$(echo "${LINE}" | cut -d $'\t' -f 1)
VSN=$(echo "${LINE}" | cut -d $'\t' -f 2)
# is the version already specified in dependencies or devDependencies?
node -p "try { \
var pkg = require('./package.json'); \
var vsn = (pkg.dependencies || {})[\"${NAME}\"] || (pkg.devDependencies || {})[\"${NAME}\"]; \
vsn === \"${VSN}\"; \
} catch(err){false}" | \
grep -q "^false$" || {
# if yes, is it already installed (static spec)?
node -p "try { \
var pkg = require('./node_modules/${NAME}/package.json'); \
pkg.version === \"${VSN}\"; \
} catch(err){false}" | \
grep -q "^false$" || \
continue
# if yes, is it already installed (range spec)?
node -p "try { \
var pkg = require('./node_modules/${NAME}/package.json'); \
(pkg._requested || {}).rawSpec === \"${VSN}\"; \
} catch(err){false}" | \
grep -q "^false$" || \
continue
}
# 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}
}
}