I have a NPM_RESERVED_SCOPE
that contains a comma-separated list of forbidden scope.
I have a NPM_ALLOWED_DEV_DEPENDENCIES_PACKAGES
that contains a comma-separated list of exception.
JavaScript
x
NPM_RESERVED_SCOPE=@privatescope,@secondary
NPM_ALLOWED_DEV_DEPENDENCIES_PACKAGES="@privatescope/private-project-scripts,@privatescope/private-project-ci-build,@privatescope/private-project-ci-test,@privatescope/private-project-ci-release,@privatescope/private-project-ci-deploy,@privatescope/private-project-ci-release-transitive"
for i in $(echo ${NPM_RESERVED_SCOPES} | sed "s/,/ /g")
do
for j in $(echo ${NPM_ALLOWED_DEV_DEPENDENCIES_PACKAGES} | sed "s/,/ /g")
do
if [[ $(cat package.json | jq .devDependencies | grep "$i/" | grep -v "$j") ]]; then
echo "[Error] GitHub release abort. Reason: devDependencies within scope $i are forbidden."
exit 1
fi
done
done
I want this script to passe if the devDependencies
does not contains any of the NPM_RESERVED_SCOPES
, except the list of exception.
This is the file package.json
JavaScript
{
"devDependencies": {
"@privatescope/private-project-ci-build": "^1.0.5",
"@privatescope/private-project-ci-deploy": "^1.0.7",
"@privatescope/private-project-ci-release": "^1.0.5",
"@privatescope/private-project-release-transitive": "^1.0.6",
"@privatescope/private-project-test": "^1.0.4",
"@privatescope/private-project": "^0.15.22"
}
}
How can I do?
Advertisement
Answer
Convert the lists into extended patterns by replacing ,
with |
and wrapping the list in @(...)
.
JavaScript
reserved="@(${NPM_RESERVED_SCOPE//,/|})"
allowed="@(${NPM_ALLOWED_DEV_DEPENDENCIES_PACKAGES//,/|})"
Now pattern matching can replace the nested for loops.a = @(x|y|z)
if a
matches any one of the patterns x
, y
, or z
.
JavaScript
while IFS= read -r dep; do
if [[ $dep = $reserved && $dep != $allowed ]]; then
echo "[Error] GitHub release abort. Reason: $dep within scope $i is forbidden." >&2
exit 1
fi
done < <( jq .devDependencies '.devDependencies' )