1

I am using AWS Codeartifact within my project as a private NPM registry (and proxy of course) and i have some issues getting the perfect workflow. Right now i have a .sh script which generates me the Auth token for AWS and generates a project local .npmrc file. It pretty much looks like this:

#!/bin/sh
export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain xxxxx \
 --domain-owner XXXXXX --query authorizationToken --output text --profile XXXXX`
export REPOSITORY_ENDPOINT=`aws codeartifact get-repository-endpoint  --domain xxxxx \
 --repository xxxx --format npm --query repositoryEndpoint --output text --profile xxxx`
cat << EOF > .npmrc
registry=$REPOSITORY_ENDPOINT
${REPOSITORY_ENDPOINT#https:}:always-auth=true
${REPOSITORY_ENDPOINT#https:}:_authToken=\${CODEARTIFACT_AUTH_TOKEN}
EOF

Now i dont want to run this script manually of course but it should be part of my NPM build process, so i started with things like this in package.json

  "scripts": {
    "build": "tsc",
    "prepublish": "./scriptabove.sh"
  }

When running "npm publish" (for example) the .npmrc is created nicely but i assume since NPM is already running, any changes to npmrc wont get picked up. When i run "npm publish" the second time, it works of course.

My question: Is there any way to hook into the build process to apply the token? I dont want to say to my users "please call the scriptabove.sh first before doing any NPM commands. And i dont like "scriptabove.sh && npm publish" either.

Logemann
  • 2,767
  • 33
  • 53

1 Answers1

0

You could create a script like this

publish-package command can be called whatever you want

 "scripts": {
    "build": "tsc",
    "prepublish": "./scriptabove.sh",
    "publish-package": "npm run prepublish && npm publish" 
  }

Explanation:

Use & (single ampersand) for parallel execution.
Use && (double ampersand) for sequential execution.

publish-package will then run the prepublish command first then after run npm publish. This method is a great way to chain npm commands that need to run in sequential order.

For more information on this here's a StackOverflow post about it. Running NPM scripts sequentially

Nilesh Kesar
  • 387
  • 2
  • 15