feat: add logic to enable corepack on passing optional input

This commit is contained in:
Sayak Mukhopadhyay 2022-12-26 22:32:49 +05:30
parent b6efa7f903
commit 53234c232f
No known key found for this signature in database
GPG key ID: 89EEFF3FB23D3FFD
3 changed files with 25 additions and 0 deletions

View file

@ -25,6 +25,9 @@ inputs:
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.' description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
cache-dependency-path: 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.' 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. # 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 # escape valve for someone having issues or needing the absolute latest which isn't cached yet
outputs: outputs:

View file

@ -4,6 +4,7 @@ import * as core from '@actions/core';
import * as hc from '@actions/http-client'; import * as hc from '@actions/http-client';
import * as io from '@actions/io'; import * as io from '@actions/io';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import * as exec from '@actions/exec';
import * as path from 'path'; import * as path from 'path';
import * as semver from 'semver'; import * as semver from 'semver';
import fs from 'fs'; import fs from 'fs';
@ -604,3 +605,21 @@ export function parseNodeVersionFile(contents: string): string {
function isLatestSyntax(versionSpec): boolean { function isLatestSyntax(versionSpec): boolean {
return ['current', 'latest', 'node'].includes(versionSpec); return ['current', 'latest', 'node'].includes(versionSpec);
} }
export async function enableCorepack(input: string): Promise<void> {
let corepackArgs = ['enable'];
if (input.length > 0 && 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, {
ignoreReturnCode: true
});
}
}

View file

@ -49,6 +49,9 @@ export async function run() {
auth.configAuthentication(registryUrl, alwaysAuth); auth.configAuthentication(registryUrl, alwaysAuth);
} }
const corepack = core.getInput('corepack') || 'false';
await installer.enableCorepack(corepack);
if (cache && isCacheFeatureAvailable()) { if (cache && isCacheFeatureAvailable()) {
const cacheDependencyPath = core.getInput('cache-dependency-path'); const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(cache, cacheDependencyPath); await restoreCache(cache, cacheDependencyPath);