From 574c6daa528de8774ff0e11d2c64583560b7da93 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Sun, 29 Mar 2020 16:28:34 -0400 Subject: [PATCH 1/3] Spike to default the scope from package.json --- src/authutil.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/authutil.ts b/src/authutil.ts index 2ce94939..556cbfef 100644 --- a/src/authutil.ts +++ b/src/authutil.ts @@ -25,6 +25,9 @@ function writeRegistryToFile( if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) { scope = github.context.repo.owner; } + if (!scope && namePrefix = require('./package').name.match('@[^/]+')) { + scope = namePrefix[0]; + } if (scope && scope[0] != '@') { scope = '@' + scope; } From 473cb1b5065ff615e6e12777bd2cdeadf1d016c7 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Sun, 7 Feb 2021 10:28:55 -0500 Subject: [PATCH 2/3] Make linter happy --- src/authutil.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/authutil.ts b/src/authutil.ts index 556cbfef..07ca0851 100644 --- a/src/authutil.ts +++ b/src/authutil.ts @@ -25,8 +25,11 @@ function writeRegistryToFile( if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) { scope = github.context.repo.owner; } - if (!scope && namePrefix = require('./package').name.match('@[^/]+')) { - scope = namePrefix[0]; + if (!scope) { + let namePrefix = require('./package').name.match('@[^/]+'); + if (namePrefix) { + scope = namePrefix[0]; + } } if (scope && scope[0] != '@') { scope = '@' + scope; From 55b7d827be98b5204ff20342ea96be54a2119d37 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Tue, 28 May 2024 17:28:09 -0400 Subject: [PATCH 3/3] Refactor and test --- __tests__/authutil.test.ts | 12 ++++++++++++ src/authutil.ts | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/__tests__/authutil.test.ts b/__tests__/authutil.test.ts index 0676a850..ef95331f 100644 --- a/__tests__/authutil.test.ts +++ b/__tests__/authutil.test.ts @@ -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(); diff --git a/src/authutil.ts b/src/authutil.ts index 07ca0851..fed704db 100644 --- a/src/authutil.ts +++ b/src/authutil.ts @@ -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`); + } +}