Skip to content

Commit

Permalink
Adding support for specifying an output path for the npmrc file
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcampbell committed Nov 9, 2020
1 parent f17306b commit c290ba1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,10 @@ The path to a private npm repository. Please ensure you supply the trailing `/`

Example: `//myprivatenpm.com/`

### `output-path` (optional)
The path to the .npmrc that will be created. Please ensure you supply the trailing `/`!

Example: `./project/path/`

## License
MIT (see [LICENSE](./LICENSE))
5 changes: 4 additions & 1 deletion hooks/pre-command
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SEEK_OSS_PRIVATE_NPM_REGISTRY=${BUILDKITE_PLUGIN_PRIVATE_NPM_REGISTRY:-'//regist
SEEK_OSS_PRIVATE_NPM_TOKEN=${BUILDKITE_PLUGIN_PRIVATE_NPM_TOKEN:-''}
SEEK_OSS_PRIVATE_NPM_FILE=${BUILDKITE_PLUGIN_PRIVATE_NPM_FILE:-''}
SEEK_OSS_PRIVATE_NPM_ENV=${BUILDKITE_PLUGIN_PRIVATE_NPM_ENV:-''}
SEEK_OSS_PRIVATE_NPM_OUTPUT_PATH=${BUILDKITE_PLUGIN_PRIVATE_NPM_OUTPUT_PATH:-'./'}

if { [[ -n "${SEEK_OSS_PRIVATE_NPM_FILE}" ]] && [[ -n "${SEEK_OSS_PRIVATE_NPM_ENV}" ]]; } \
|| { [[ -n "${SEEK_OSS_PRIVATE_NPM_FILE}" ]] && [[ -n "${SEEK_OSS_PRIVATE_NPM_TOKEN}" ]]; } \
Expand All @@ -31,7 +32,9 @@ fi

echo '--- Setting up access for :no_entry_sign: :npm: :package:'

cat > .npmrc << EOF
OUTPUT_FILE="${SEEK_OSS_PRIVATE_NPM_OUTPUT_PATH}.npmrc"

mkdir -p "${OUTPUT_FILE%/*}" && cat > $OUTPUT_FILE << EOF
${SEEK_OSS_PRIVATE_NPM_REGISTRY}:_authToken=${SEEK_OSS_PRIVATE_NPM_TOKEN}
save-exact=true
EOF
2 changes: 2 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ configuration:
type: string
file:
type: string
output-path:
type: string

12 changes: 12 additions & 0 deletions tests/pre-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
load "$BATS_PATH/load.bash"
teardown() {
rm -f .npmrc
rm -f ./tests/path/to/project/.npmrc
unset BUILDKITE_PLUGIN_PRIVATE_NPM_ENV
unset BUILDKITE_PLUGIN_PRIVATE_NPM_TOKEN
unset BUILDKITE_PLUGIN_PRIVATE_NPM_FILE
Expand Down Expand Up @@ -116,6 +117,17 @@ teardown() {
assert_equal "$(head -n1 .npmrc)" '//myprivateregistry.org/:_authToken=abc123'
}

@test "creates a npmrc file with supplied output path and token" {
export BUILDKITE_PLUGIN_PRIVATE_NPM_TOKEN='abc123'
export BUILDKITE_PLUGIN_PRIVATE_NPM_OUTPUT_PATH='./tests/path/to/project/'

run $PWD/hooks/pre-command

assert_success
assert [ -e './tests/path/to/project/.npmrc' ]
assert_equal "$(head -n1 ./tests/path/to/project/.npmrc)" '//registry.npmjs.org/:_authToken=abc123'
}

@test "the command fails if none of the fields are not set" {
run $PWD/hooks/pre-command

Expand Down

0 comments on commit c290ba1

Please sign in to comment.