Skip to content

Commit 61cc585

Browse files
authored
Add support for npm and yarn (#25)
1 parent c07f45a commit 61cc585

7 files changed

Lines changed: 110 additions & 2 deletions

File tree

src/artifacts-helper/NOTES.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
This installs [Azure Artifacts Credential Provider](https://github.com/microsoft/artifacts-credprovider)
2-
and optionally configures an alias for `dotnet` and `nuget` that dynamically sets an authentication token
2+
and optionally configures an alias for `dotnet`, `nuget`, `npm`, and `yarn` that dynamically sets an authentication token
33
for pulling artifacts from a feed before running the command.
44

5+
For `npm` and `yarn` this requires that your `~/.npmrc` file is configured to use the ${ARTIFACTS_ACCESSTOKEN}
6+
environment variable for the password. A helper script has been added that you can use to write your `~/.npmrc`
7+
file during your setup process, though there are many ways you could accomplish this. To use the script, run it like
8+
this:
9+
10+
```
11+
write-npm.sh pkgs.dev.azure.com/orgname/projectname/_packaging/feed1/npm
12+
write-npm.sh pkgs.dev.azure.com/orgname/projectname/_packaging/feed2/npm username
13+
write-npm.sh pkgs.dev.azure.com/orgname/projectname/_packaging/feed3/npm username email
14+
```
15+
16+
You must pass the feed name to the script, but you can optionally provide a username and email if desired. Defaults
17+
are put in place if they are not provided.
18+
519
## OS Support
620

721
This feature is tested to work on Debian/Ubuntu and Mariner CBL 2.0

src/artifacts-helper/devcontainer-feature.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Azure Artifacts Credential Helper",
33
"id": "artifacts-helper",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"description": "Configures Codespace to authenticate with Azure Artifact feeds",
66
"options": {
77
"nugetURIPrefixes": {
@@ -23,6 +23,16 @@
2323
"type": "boolean",
2424
"default": true,
2525
"description": "Create alias for nuget"
26+
},
27+
"npmAlias": {
28+
"type": "boolean",
29+
"default": true,
30+
"description": "Create alias for npm"
31+
},
32+
"yarnAlias": {
33+
"type": "boolean",
34+
"default": true,
35+
"description": "Create alias for yarn"
2636
}
2737
},
2838
"installsAfter": [

src/artifacts-helper/install.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ PREFIXES="${NUGETURIPREFIXES:-"https://pkgs.dev.azure.com/"}"
66
USENET6="${DOTNET6:-"false"}"
77
ALIAS_DOTNET="${DOTNETALIAS:-"true"}"
88
ALIAS_NUGET="${NUGETALIAS:-"true"}"
9+
ALIAS_NPM="${NPMALIAS:-"true"}"
10+
ALIAS_YARN="${YARNALIAS:-"true"}"
911

1012
# Source /etc/os-release to get OS info
1113
. /etc/os-release
@@ -51,6 +53,13 @@ sed "s|REPLACE_WITH_AZURE_DEVOPS_NUGET_FEED_URL_PREFIX|${PREFIXES}|g" ./scripts/
5153
chmod +rx /usr/local/bin/run-dotnet.sh
5254
sed "s|REPLACE_WITH_AZURE_DEVOPS_NUGET_FEED_URL_PREFIX|${PREFIXES}|g" ./scripts/run-nuget.sh > /usr/local/bin/run-nuget.sh
5355
chmod +rx /usr/local/bin/run-nuget.sh
56+
cp ./scripts/run-npm.sh /usr/local/bin/run-npm.sh
57+
chmod +rx /usr/local/bin/run-npm.sh
58+
cp ./scripts/run-yarn.sh /usr/local/bin/run-yarn.sh
59+
chmod +rx /usr/local/bin/run-yarn.sh
60+
cp ./scripts/write-npm.sh /usr/local/bin/write-npm.sh
61+
chmod +rx /usr/local/bin/write-npm.sh
62+
5463

5564
if command -v sudo >/dev/null 2>&1; then
5665
if [ "root" != "$_REMOTE_USER" ]; then
@@ -62,6 +71,14 @@ if command -v sudo >/dev/null 2>&1; then
6271
sudo -u ${_REMOTE_USER} bash -c "echo 'alias nuget=/usr/local/bin/run-nuget.sh' >> ~/.bashrc"
6372
sudo -u ${_REMOTE_USER} bash -c "echo 'alias nuget=/usr/local/bin/run-nuget.sh' >> ~/.zshrc"
6473
fi
74+
if [ "${ALIAS_NPM}" = "true" ]; then
75+
sudo -u ${_REMOTE_USER} bash -c "echo 'alias npm=/usr/local/bin/run-npm.sh' >> ~/.bashrc"
76+
sudo -u ${_REMOTE_USER} bash -c "echo 'alias npm=/usr/local/bin/run-npm.sh' >> ~/.zshrc"
77+
fi
78+
if [ "${ALIAS_YARN}" = "true" ]; then
79+
sudo -u ${_REMOTE_USER} bash -c "echo 'alias yarn=/usr/local/bin/run-yarn.sh' >> ~/.bashrc"
80+
sudo -u ${_REMOTE_USER} bash -c "echo 'alias yarn=/usr/local/bin/run-yarn.sh' >> ~/.zshrc"
81+
fi
6582
sudo -u ${_REMOTE_USER} bash -c "/tmp/install-provider.sh ${USENET6}"
6683
rm /tmp/install-provider.sh
6784
exit 0
@@ -78,6 +95,16 @@ if [ "${ALIAS_NUGET}" = "true" ]; then
7895
echo "alias nuget='/usr/local/bin/run-nuget.sh'" >> /etc/zsh/zshrc || true
7996
fi
8097

98+
if [ "${ALIAS_NPM}" = "true" ]; then
99+
sudo -u ${_REMOTE_USER} bash -c "echo 'alias npm=/usr/local/bin/run-npm.sh' >> /etc/bash.bashrc || true
100+
sudo -u ${_REMOTE_USER} bash -c "echo 'alias npm=/usr/local/bin/run-npm.sh' >> /etc/zsh/zshrc || true
101+
fi
102+
103+
if [ "${ALIAS_YARN}" = "true" ]; then
104+
sudo -u ${_REMOTE_USER} bash -c "echo 'alias yarn=/usr/local/bin/run-yarn.sh' >> /etc/bash.bashrc || true
105+
sudo -u ${_REMOTE_USER} bash -c "echo 'alias yarn=/usr/local/bin/run-yarn.sh' >> /etc/zsh/zshrc || true
106+
fi
107+
81108
rm /tmp/install-provider.sh
82109

83110
exit 0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
if [ -f "${HOME}/ado-auth-helper" ]; then
4+
export ARTIFACTS_ACCESSTOKEN=$(${HOME}/ado-auth-helper get-access-token)
5+
fi
6+
7+
# Find the npm executable so we do not run the bash alias again
8+
NPM_EXE=$(which npm)
9+
10+
${NPM_EXE} "$@"
11+
EXIT_CODE=$?
12+
unset NPM_EXE
13+
14+
if [ -f "${HOME}/ado-auth-helper" ]; then
15+
unset ARTIFACTS_ACCESSTOKEN
16+
fi
17+
18+
exit $EXIT_CODE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
if [ -f "${HOME}/ado-auth-helper" ]; then
4+
export ARTIFACTS_ACCESSTOKEN=$(${HOME}/ado-auth-helper get-access-token)
5+
fi
6+
7+
# Find the yarn executable so we do not run the bash alias again
8+
YARN_EXE=$(which yarn)
9+
10+
${YARN_EXE} "$@"
11+
EXIT_CODE=$?
12+
unset YARN_EXE
13+
14+
if [ -f "${HOME}/ado-auth-helper" ]; then
15+
unset ARTIFACTS_ACCESSTOKEN
16+
fi
17+
18+
exit $EXIT_CODE
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
ARTIFACTS_FEED=${1:-"required"}
4+
FEED_USER="${2:-"codespaces"}"
5+
FEED_EMAIL="${3:-"codespaces@github.com"}"
6+
7+
# If ARTIFACTS_FEED equals "required" then exit with error message
8+
if [ "${ARTIFACTS_FEED}" = "required" ]; then
9+
echo " Usage: write-npm.sh <ARTIFACTS_FEED> <FEED_USER>(optional) <FEED_EMAIL>(optional)"
10+
echo "example: write-npm.sh pkgs.dev.azure.com/orgname/projectname/_packaging/feedname/npm"
11+
exit 1
12+
fi
13+
14+
echo "//${ARTIFACTS_FEED}/registry/:username=${FEED_USER}" >> ${HOME}/.npmrc
15+
echo "//${ARTIFACTS_FEED}/registry/:_password=\${ARTIFACTS_ACCESSTOKEN}" >> ${HOME}/.npmrc
16+
echo "//${ARTIFACTS_FEED}/registry/:email=${FEED_EMAIL}" >> ${HOME}/.npmrc
17+
echo "//${ARTIFACTS_FEED}/:username=${FEED_USER}" >> ${HOME}/.npmrc
18+
echo "//${ARTIFACTS_FEED}/:_password=\${ARTIFACTS_ACCESSTOKEN}" >> ${HOME}/.npmrc
19+
echo "//${ARTIFACTS_FEED}/:email=${FEED_EMAIL}" >> ${HOME}/.npmrc

test/artifacts-helper/test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ source dev-container-features-test-lib
88
# Feature-specific tests
99
check "dotnet" grep "pkgs.dev.azure.com" <(cat /usr/local/bin/run-dotnet.sh)
1010
check "nuget" grep "pkgs.dev.azure.com" <(cat /usr/local/bin/run-nuget.sh)
11+
check "write-npm" /usr/local/bin/write-npm.sh pkgs.dev.azure.com && grep "pkgs.dev.azure.com" <(cat ~/.npmrc)
12+
1113

1214
# Report results
1315
# If any of the checks above exited with a non-zero exit code, the test will fail.

0 commit comments

Comments
 (0)