From 48ed97757dab49189b7fbe7f97ffdcc2b7e78962 Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Thu, 5 May 2022 11:21:29 -0600 Subject: [PATCH] add check for packageManager field --- __tests__/installer.test.ts | 8 ++++---- src/main.ts | 23 ++++++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 98cc10b3..b54bcfa2 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -89,19 +89,19 @@ describe('setup-node', () => { warningSpy = jest.spyOn(core, 'warning'); cnSpy.mockImplementation(line => { // uncomment to debug - process.stderr.write('write:' + line + '\n'); + // process.stderr.write('write:' + line + '\n'); }); logSpy.mockImplementation(line => { // uncomment to debug - process.stderr.write('log:' + line + '\n'); + // process.stderr.write('log:' + line + '\n'); }); dbgSpy.mockImplementation(msg => { // uncomment to see debug output - process.stderr.write(msg + '\n'); + // process.stderr.write(msg + '\n'); }); warningSpy.mockImplementation(msg => { // uncomment to debug - process.stderr.write('log:' + msg + '\n'); + // process.stderr.write('log:' + msg + '\n'); }); }); diff --git a/src/main.ts b/src/main.ts index 59a23a74..2fe3bb41 100644 --- a/src/main.ts +++ b/src/main.ts @@ -47,11 +47,24 @@ export async function run() { } if (cache && isCacheFeatureAvailable()) { - if (semver.gte(version, '14.19.0')) { - try { - core.info(await getCommandOutput('corepack enable')); - } catch (err) { - core.warning(`Failed to enable corepack. Error: ${err.message}`) + const pkgJsonPath = path.join(__dirname, '..', 'package.json'); + try { + const stat = await fs.promises.stat(pkgJsonPath); + if (stat.isFile()) { + const packageJson = JSON.parse(await fs.promises.readFile(pkgJsonPath, 'utf8')) + const packageManager = packageJson.packageManager; + + if (packageManager !== undefined && semver.gte(version, '14.19.0')) { + try { + core.info(await getCommandOutput('corepack enable')); + } catch (err) { + core.warning(`Failed to enable corepack. Error: ${err.message}`) + } + } + } + } catch (err) { + if (err instanceof Error && (err as NodeJS.ErrnoException).code === 'ENOENT') { + core.warning(`Cannot find package.json at path ${pkgJsonPath}`) } } const cacheDependencyPath = core.getInput('cache-dependency-path');