3
0
Fork 0
mirror of https://gitea.com/actions/setup-node.git synced 2024-11-24 11:09:33 +01:00
This commit is contained in:
Danny McCormick 2019-08-05 11:35:39 -04:00
parent 9bb7038a07
commit 2b9c956517
6 changed files with 113 additions and 0 deletions

View file

@ -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)

View file

@ -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'

33
lib/authutil.js Normal file
View file

@ -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);
}

View file

@ -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')}`);

39
src/authutil.ts Normal file
View file

@ -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);
}

View file

@ -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')}`);