Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default the scope from package.json#name #129

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor and test
  • Loading branch information
jasonkarns committed May 29, 2024
commit 55b7d827be98b5204ff20342ea96be54a2119d37
12 changes: 12 additions & 0 deletions __tests__/authutil.test.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import * as auth from '../src/authutil';
import * as cacheUtils from '../src/cache-utils';

let rcFile: string;
let pkgJson: string;

describe('authutil tests', () => {
const _runnerDir = path.join(__dirname, 'runner');
@@ -25,10 +26,12 @@ describe('authutil tests', () => {
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
process.env['RUNNER_TEMP'] = tempDir;
rcFile = path.join(tempDir, '.npmrc');
pkgJson = path.join(tempDir, 'package.json');
}, 100000);

beforeEach(async () => {
await io.rmRF(rcFile);
await io.rmRF(pkgJson);
// if (fs.existsSync(rcFile)) {
// fs.unlinkSync(rcFile);
// }
@@ -113,6 +116,15 @@ describe('authutil tests', () => {
expect(rc['always-auth']).toBe('false');
});

it('Automatically configures npm scope from package.json', async () => {
process.env['INPUT_SCOPE'] = '';
fs.writeFileSync(pkgJson, '{"name":"@myscope/mypackage"}');
await auth.configAuthentication('https://registry.npmjs.org', '');

const rc = readRcFile(rcFile);
expect(rc['@myscope:registry']).toBe('https://registry.npmjs.org/');
});

it('Sets up npmrc for always-auth true', async () => {
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
expect(fs.statSync(rcFile)).toBeDefined();
15 changes: 13 additions & 2 deletions src/authutil.ts
Original file line number Diff line number Diff line change
@@ -26,9 +26,9 @@ function writeRegistryToFile(
scope = github.context.repo.owner;
}
if (!scope) {
let namePrefix = require('./package').name.match('@[^/]+');
const namePrefix = packageJson('name')?.match(/^(@[^/]+)\//);
if (namePrefix) {
scope = namePrefix[0];
scope = namePrefix[1];
}
}
if (scope && scope[0] != '@') {
@@ -63,3 +63,14 @@ function writeRegistryToFile(
process.env.NODE_AUTH_TOKEN || 'XXXXX-XXXXX-XXXXX-XXXXX'
);
}

function packageJson(prop: string){
const pkgPath: string = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), 'package.json');
try {
const json = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));

return prop ? json[prop] : json;
} catch(e) {
core.debug(`Unable to read from package.json`);
}
}