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

Option to enable corepack #651

Open
wants to merge 6 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
Next Next commit
feat: add logic to enable corepack on passing optional input
  • Loading branch information
SayakMukhopadhyay committed Dec 26, 2022
commit 53234c232f116b171616ec07048ae0d8a16176b3
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -25,6 +25,9 @@ inputs:
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
cache-dependency-path:
description: 'Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc. Supports wildcards or a list of file names for caching multiple dependencies.'
corepack:
description: 'Used to specify whether to enable Corepack. Set to true to enable all package managers or set it to one or more package manager names (separate package manager names by a space. Supported package manager names: npm, yarn, pnpm.'
default: 'false'
# TODO: add input to control forcing to pull from cloud or dist.
# escape valve for someone having issues or needing the absolute latest which isn't cached yet
outputs:
19 changes: 19 additions & 0 deletions src/installer.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import * as core from '@actions/core';
import * as hc from '@actions/http-client';
import * as io from '@actions/io';
import * as tc from '@actions/tool-cache';
import * as exec from '@actions/exec';
import * as path from 'path';
import * as semver from 'semver';
import fs from 'fs';
@@ -604,3 +605,21 @@ export function parseNodeVersionFile(contents: string): string {
function isLatestSyntax(versionSpec): boolean {
return ['current', 'latest', 'node'].includes(versionSpec);
}

export async function enableCorepack(input: string): Promise<void> {
let corepackArgs = ['enable'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unnecessary to initialize this variable before the if condition.

if (input.length > 0 && input !== 'false') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is enough to check if (input.length && input !== 'false')

if (input !== 'true') {
const packageManagers = input.split(' ');
if (!packageManagers.every(pm => ['npm', 'yarn', 'pnpm'].includes(pm))) {
throw new Error(
`One or more of the specified package managers [ ${input} ] are not supported by corepack`
);
}
corepackArgs.push(...packageManagers);
}
await exec.getExecOutput('corepack', corepackArgs, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, use getCommandOutput or getCommandOutputNotEmpty instead of directly calling exec.getExecOutput

ignoreReturnCode: true
});
}
}
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -49,6 +49,9 @@ export async function run() {
auth.configAuthentication(registryUrl, alwaysAuth);
}

const corepack = core.getInput('corepack') || 'false';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order the input to be recognized it must be defined in action.yml

await installer.enableCorepack(corepack);

if (cache && isCacheFeatureAvailable()) {
const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(cache, cacheDependencyPath);