mirror of
https://github.com/actions/setup-node.git
synced 2025-12-17 22:07:51 +00:00
add devEngines.packageManager detection logic for npm auto-caching
This commit is contained in:
parent
7024c214c6
commit
8f225c52ce
3 changed files with 141 additions and 27 deletions
28
src/main.ts
28
src/main.ts
|
|
@ -138,7 +138,7 @@ function resolveVersionInput(): string {
|
|||
}
|
||||
|
||||
export function getNameFromPackageManagerField(): string | undefined {
|
||||
// Enable auto-cache for npm
|
||||
const npmRegex = /^(\^)?npm(@.*)?$/; // matches "npm", "npm@...", "^npm@..."
|
||||
try {
|
||||
const packageJson = JSON.parse(
|
||||
fs.readFileSync(
|
||||
|
|
@ -146,13 +146,29 @@ export function getNameFromPackageManagerField(): string | undefined {
|
|||
'utf-8'
|
||||
)
|
||||
);
|
||||
const pm = packageJson.packageManager;
|
||||
if (typeof pm === 'string') {
|
||||
const match = pm.match(/^(?:\^)?(npm)@/);
|
||||
return match ? match[1] : undefined;
|
||||
|
||||
// Check devEngines.packageManager first (object or array)
|
||||
const devPM = packageJson?.devEngines?.packageManager;
|
||||
if (devPM) {
|
||||
if (Array.isArray(devPM)) {
|
||||
for (const obj of devPM) {
|
||||
if (typeof obj?.name === 'string' && npmRegex.test(obj.name)) {
|
||||
return 'npm';
|
||||
}
|
||||
}
|
||||
} else if (typeof devPM?.name === 'string' && npmRegex.test(devPM.name)) {
|
||||
return 'npm';
|
||||
}
|
||||
}
|
||||
|
||||
// Check top-level packageManager
|
||||
const topLevelPM = packageJson?.packageManager;
|
||||
if (typeof topLevelPM === 'string' && npmRegex.test(topLevelPM)) {
|
||||
return 'npm';
|
||||
}
|
||||
|
||||
return undefined;
|
||||
} catch (err) {
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue