From c290ba1b6cd5f9aa823d8c8e9f8671ba75d7c4cc Mon Sep 17 00:00:00 2001 From: Neil Campbell Date: Mon, 9 Nov 2020 15:41:32 +1100 Subject: [PATCH 1/2] Adding support for specifying an output path for the npmrc file --- README.md | 5 +++++ hooks/pre-command | 5 ++++- plugin.yml | 2 ++ tests/pre-command.bats | 12 ++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2800662..abf6006 100644 --- a/README.md +++ b/README.md @@ -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)) diff --git a/hooks/pre-command b/hooks/pre-command index b8542f5..ca35d83 100755 --- a/hooks/pre-command +++ b/hooks/pre-command @@ -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}" ]]; } \ @@ -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 diff --git a/plugin.yml b/plugin.yml index a24dc7f..36798f2 100644 --- a/plugin.yml +++ b/plugin.yml @@ -14,4 +14,6 @@ configuration: type: string file: type: string + output-path: + type: string diff --git a/tests/pre-command.bats b/tests/pre-command.bats index 6b02078..57c6460 100644 --- a/tests/pre-command.bats +++ b/tests/pre-command.bats @@ -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 @@ -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 From 5647fffba3e10e242358415ab4073109635bda4d Mon Sep 17 00:00:00 2001 From: Neil Campbell Date: Tue, 10 Nov 2020 13:22:54 +1100 Subject: [PATCH 2/2] Update version number in readme --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index abf6006..ca7a722 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ To read the value from an environment variable named `MY_TOKEN` when the plugin steps: - command: yarn install plugins: - - seek-oss/private-npm#v1.1.2: + - seek-oss/private-npm#v1.2.0: env: "MY_TOKEN" ``` @@ -27,7 +27,7 @@ To read the value from a file named `my_token_file`, use the `file` field. steps: - command: yarn install plugins: - - seek-oss/private-npm#v1.1.2: + - seek-oss/private-npm#v1.2.0: file: "my_token_file" ``` @@ -39,7 +39,7 @@ approach is discoraged in favour of using with the `env` or `file` fields. This steps: - command: yarn install plugins: - - seek-oss/private-npm#v1.1.2: + - seek-oss/private-npm#v1.2.0: token: ${MY_TOKEN} ``` @@ -50,7 +50,7 @@ You can also specify a custom npm registry if you are using your own mirror. steps: - command: yarn install plugins: - - seek-oss/private-npm#v1.1.2: + - seek-oss/private-npm#v1.2.0: env: "MY_TOKEN" registry: //myprivatenpm.com/ ```