Skip to content

Commit

Permalink
chore: update create-npmrc action
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-ainsel committed Sep 22, 2024
1 parent c1eed1c commit 2d153ec
Show file tree
Hide file tree
Showing 6 changed files with 4,550 additions and 547 deletions.
20 changes: 9 additions & 11 deletions actions/utils/create-npmrc/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ inputs:
description: |
JSON string with registry and scope settings. See example below.
{
"registries": {
"https://registry.npmjs.org/": {
"scope1": "TOKEN_FOR_NPMJS"
},
"https://custom.npm.registry.com/": {
"scope2": "SCOPE2_TOKEN",
"scope3": "SCOPE3_TOKEN"
},
"https://another.registry.com/": {
"scope4": "SCOPE4_TOKEN"
}
"registries": {
"https://registry.npmjs.org/": {
"scopes": ["scope1", "scope2"],
"tokenVar": "NPMJS_TOKEN"
},
"https://custom.npm.registry.com/": {
"scopes": ["scope3"],
"tokenVar": "NODE_AUTH_TOKEN"
}
}
}
required: true
outputs:
Expand Down
21 changes: 10 additions & 11 deletions actions/utils/create-npmrc/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24964,31 +24964,30 @@ const path = __importStar(__nccwpck_require__(1017));
async function run() {
try {
const configInput = core.getInput('npmrcConfig');
const config = JSON.parse(configInput);
const config = JSON.parse(configInput); // Now TypeScript knows the structure of `config`
const workspacePath = process.env.GITHUB_WORKSPACE;
if (!workspacePath) {
throw new Error('GITHUB_WORKSPACE not defined');
}
const npmrcPath = path.join(workspacePath, '.npmrc');
let npmrcContent = '';
Object.entries(config.registries).forEach(([registryUrl, scopes]) => {
// Iterate over each registry in the configuration
for (const [registryUrl, { scopes, tokenVar }] of Object.entries(config.registries)) {
const registryURL = new URL(registryUrl);
// always-auth and token settings for the registry
npmrcContent += `//${registryURL.hostname}/:always-auth=true\n`;
Object.entries(scopes).forEach(([scope, tokenVar]) => {
npmrcContent += `//${registryURL.hostname}/:_authToken=\${${tokenVar}}\n`;
// Add registry setting for each scope
scopes.forEach((scope) => {
// Explicitly declare the type of 'scope'
npmrcContent += `@${scope}:registry=${registryUrl}\n`;
npmrcContent += `//${registryURL.hostname}/${scope}/:_authToken=\${${tokenVar}}\n`;
});
});
}
fs.writeFileSync(npmrcPath, npmrcContent);
core.setOutput('npmrcPath', npmrcPath);
}
catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
}
else {
core.setFailed('Unknown error occurred');
}
core.setFailed(error instanceof Error ? error.message : 'Unknown error occurred');
}
}
run();
Expand Down
31 changes: 20 additions & 11 deletions actions/utils/create-npmrc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ import * as core from '@actions/core';
import * as fs from 'fs';
import * as path from 'path';

interface NpmRegistryConfig {
scopes: string[];
tokenVar: string;
}

interface NpmConfig {
registries: Record<string, NpmRegistryConfig>;
}

async function run(): Promise<void> {
try {
const configInput = core.getInput('npmrcConfig');
const config = JSON.parse(configInput);
const config: NpmConfig = JSON.parse(configInput); // Now TypeScript knows the structure of `config`

const workspacePath = process.env.GITHUB_WORKSPACE;
if (!workspacePath) {
Expand All @@ -15,24 +24,24 @@ async function run(): Promise<void> {
const npmrcPath = path.join(workspacePath, '.npmrc');
let npmrcContent = '';

Object.entries(config.registries).forEach(([registryUrl, scopes]) => {
// Iterate over each registry in the configuration
for (const [registryUrl, { scopes, tokenVar }] of Object.entries(config.registries)) {
const registryURL = new URL(registryUrl);
// always-auth and token settings for the registry
npmrcContent += `//${registryURL.hostname}/:always-auth=true\n`;
Object.entries(scopes as Record<string, string>).forEach(([scope, tokenVar]) => {
npmrcContent += `//${registryURL.hostname}/:_authToken=\${${tokenVar}}\n`;

// Add registry setting for each scope
scopes.forEach((scope: string) => {
// Explicitly declare the type of 'scope'
npmrcContent += `@${scope}:registry=${registryUrl}\n`;
npmrcContent += `//${registryURL.hostname}/${scope}/:_authToken=\${${tokenVar}}\n`;
});
});
}

fs.writeFileSync(npmrcPath, npmrcContent);

core.setOutput('npmrcPath', npmrcPath);
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
} else {
core.setFailed('Unknown error occurred');
}
core.setFailed(error instanceof Error ? error.message : 'Unknown error occurred');
}
}

Expand Down
Loading

0 comments on commit 2d153ec

Please sign in to comment.