Skip to content

Commit a847cb8

Browse files
committed
build: implemented script that checks npm access before publishing the package
1 parent 11fe23b commit a847cb8

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"lint:styles": "stylelint 'src/**/*.s(a|c)ss'",
1818
"test": "jest --config scripts/test/jest.config.js",
1919
"test:coverage": "jest --config scripts/test/jest.config.js --coverage",
20-
"prepublishOnly": "npm test && npm run lint:ci && npm run lint:styles && npm run build:prod",
20+
"check-npm-publish-access": "sh scripts/check-npm-publish-access/check-npm-publish-access.sh",
21+
"prepublishOnly": "npm run check-npm-publish-access && npm run test && npm run lint:ci && npm run lint:styles && npm run build:prod",
2122
"coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls",
2223
"release": "standard-version -t ''"
2324
},
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
3+
# Exit immediately if any command exits with a non-zero status
4+
set -e;
5+
6+
# Checking for dependencies
7+
if ! [ -x "$(command -v npm)" ]; then
8+
echo 'Error: "npm" is not installed.'
9+
exit 1
10+
fi
11+
if ! [ -x "$(command -v grep)" ]; then
12+
echo 'Error: "grep" is not installed.'
13+
exit 1
14+
fi
15+
16+
# Checking if the list of repo npm owners includes current npm user.
17+
#
18+
# `npm owner ls` prints a list of npm users, who have access to the current repo
19+
# in the following format:
20+
# > username1 <someuser@emain.com>
21+
# > username2 <auser@emain.com>
22+
# > username3 <theusername@emain.com>
23+
#
24+
# `npm whoami` prints current npm username
25+
# > username2
26+
npm_package_name=$(node -p "require('./package.json').name")
27+
npm_current_user=$(npm whoami)
28+
if npm owner ls | grep --quiet "^${npm_current_user}\s"; then
29+
echo "Access to '${npm_package_name}' repository was confirmed for '${npm_current_user}'."
30+
exit 0
31+
else
32+
echo "${npm_current_user} doesn't have an access to the repo '${npm_package_name}'."
33+
exit 1
34+
fi
35+

0 commit comments

Comments
 (0)