From 2b9c956517ae5f9b397967037488fce2b4bc7ff7 Mon Sep 17 00:00:00 2001
From: Danny McCormick <damccorm@microsoft.com>
Date: Mon, 5 Aug 2019 11:35:39 -0400
Subject: [PATCH] Updates

---
 README.md         | 28 ++++++++++++++++++++++++++++
 action.yml        |  2 ++
 lib/authutil.js   | 33 +++++++++++++++++++++++++++++++++
 lib/setup-node.js |  5 +++++
 src/authutil.ts   | 39 +++++++++++++++++++++++++++++++++++++++
 src/setup-node.ts |  6 ++++++
 6 files changed, 113 insertions(+)
 create mode 100644 lib/authutil.js
 create mode 100644 src/authutil.ts

diff --git a/README.md b/README.md
index 1bd83f26..8f45d0a5 100644
--- a/README.md
+++ b/README.md
@@ -39,6 +39,34 @@ jobs:
       - run: npm test
 ```
 
+Set up auth with npm:
+```yaml
+steps:
+- uses: actions/checkout@master
+- uses: actions/setup-node@v1
+  with:
+    version: '10.x'
+    registry-url: <registry url>
+- run: npm install
+- run: npm publish
+  env:
+    NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
+```
+
+Set up auth with yarn:
+```yaml
+steps:
+- uses: actions/checkout@master
+- uses: actions/setup-node@v1
+  with:
+    version: '10.x'
+    registry-url: <registry url>
+- run: npm install
+- run: yarn publish
+  env:
+    YARN_TOKEN: ${{ secrets.YARN_TOKEN }}
+```
+
 # License
 
 The scripts and documentation in this project are released under the [MIT License](LICENSE)
diff --git a/action.yml b/action.yml
index 0d97307f..c951c200 100644
--- a/action.yml
+++ b/action.yml
@@ -5,6 +5,8 @@ inputs:
   version:
     description: 'Version Spec of the version to use.  Examples: 10.x, 10.15.1, >=10.15.0, lts'
     default: '10.x'
+  registry-url:
+    description: 'Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file, and set up auth to read in from env.NPM_TOKEN or env.YARN_TOKEN respectively.'
 runs:
   using: 'node12'
   main: 'lib/setup-node.js'
diff --git a/lib/authutil.js b/lib/authutil.js
new file mode 100644
index 00000000..3ce05bbf
--- /dev/null
+++ b/lib/authutil.js
@@ -0,0 +1,33 @@
+"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const fs = __importStar(require("fs"));
+const os = __importStar(require("os"));
+const path = __importStar(require("path"));
+function configAuth(registryUrl) {
+    let npmrc = path.resolve(process.cwd(), '.npmrc');
+    let yarnrc = path.resolve(process.cwd(), '.yarnrc');
+    writeRegistryToFile(registryUrl, npmrc, 'NPM_TOKEN');
+    writeRegistryToFile(registryUrl, yarnrc, 'YARN_TOKEN');
+}
+exports.configAuth = configAuth;
+function writeRegistryToFile(registryUrl, fileLocation, authTokenName) {
+    let newContents = '';
+    if (fs.existsSync(fileLocation)) {
+        const curContents = fs.readFileSync(fileLocation, 'utf8');
+        curContents.split(os.EOL).forEach((line) => {
+            // Add current contents unless they are setting the registry
+            if (!line.startsWith('registry')) {
+                newContents += line + os.EOL;
+            }
+        });
+    }
+    newContents += 'registry=' + registryUrl + os.EOL + 'always-auth=true' + os.EOL + registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${' + authTokenName + '}';
+    fs.writeFileSync(fileLocation, newContents);
+}
diff --git a/lib/setup-node.js b/lib/setup-node.js
index b59c4b3d..26d716a4 100644
--- a/lib/setup-node.js
+++ b/lib/setup-node.js
@@ -17,6 +17,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
 Object.defineProperty(exports, "__esModule", { value: true });
 const core = __importStar(require("@actions/core"));
 const installer = __importStar(require("./installer"));
+const auth = __importStar(require("./authutil"));
 const path = __importStar(require("path"));
 function run() {
     return __awaiter(this, void 0, void 0, function* () {
@@ -30,6 +31,10 @@ function run() {
                 // TODO: installer doesn't support proxy
                 yield installer.getNode(version);
             }
+            const registryUrl = core.getInput('registry-url');
+            if (registryUrl) {
+                auth.configAuth(registryUrl);
+            }
             // TODO: setup proxy from runner proxy config
             const matchersPath = path.join(__dirname, '..', '.github');
             console.log(`##[add-matcher]${path.join(matchersPath, 'tsc.json')}`);
diff --git a/src/authutil.ts b/src/authutil.ts
new file mode 100644
index 00000000..baa21c9d
--- /dev/null
+++ b/src/authutil.ts
@@ -0,0 +1,39 @@
+import * as fs from 'fs';
+import * as os from 'os';
+import * as path from 'path';
+
+export function configAuth(registryUrl: string) {
+  let npmrc: string = path.resolve(process.cwd(), '.npmrc');
+  let yarnrc: string = path.resolve(process.cwd(), '.yarnrc');
+
+  writeRegistryToFile(registryUrl, npmrc, 'NPM_TOKEN');
+  writeRegistryToFile(registryUrl, yarnrc, 'YARN_TOKEN');
+}
+
+function writeRegistryToFile(
+  registryUrl: string,
+  fileLocation: string,
+  authTokenName: string
+) {
+  let newContents = '';
+  if (fs.existsSync(fileLocation)) {
+    const curContents = fs.readFileSync(fileLocation, 'utf8');
+    curContents.split(os.EOL).forEach((line: string) => {
+      // Add current contents unless they are setting the registry
+      if (!line.startsWith('registry')) {
+        newContents += line + os.EOL;
+      }
+    });
+  }
+  newContents +=
+    'registry=' +
+    registryUrl +
+    os.EOL +
+    'always-auth=true' +
+    os.EOL +
+    registryUrl.replace(/(^\w+:|^)/, '') +
+    ':_authToken=${' +
+    authTokenName +
+    '}';
+  fs.writeFileSync(fileLocation, newContents);
+}
diff --git a/src/setup-node.ts b/src/setup-node.ts
index c3dcd37d..c0387861 100644
--- a/src/setup-node.ts
+++ b/src/setup-node.ts
@@ -1,5 +1,6 @@
 import * as core from '@actions/core';
 import * as installer from './installer';
+import * as auth from './authutil';
 import * as path from 'path';
 
 async function run() {
@@ -14,6 +15,11 @@ async function run() {
       await installer.getNode(version);
     }
 
+    const registryUrl = core.getInput('registry-url');
+    if (registryUrl) {
+      auth.configAuth(registryUrl);
+    }
+
     // TODO: setup proxy from runner proxy config
     const matchersPath = path.join(__dirname, '..', '.github');
     console.log(`##[add-matcher]${path.join(matchersPath, 'tsc.json')}`);