Skip to content

Commit 27015cf

Browse files
committed
refactor: Extract package manager utility
1 parent 946a292 commit 27015cf

File tree

3 files changed

+45
-41
lines changed

3 files changed

+45
-41
lines changed

jsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "ESNext",
5+
"moduleResolution": "NodeNext",
6+
"allowJs": true,
7+
"checkJs": true,
8+
"resolveJsonModule": true,
9+
"noEmit": true
10+
}
11+
}

src/index.js

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import path from 'path';
21
import { getInput, setFailed, startGroup, endGroup, debug } from '@actions/core';
32
import { context, getOctokit } from '@actions/github';
43
import { exec } from '@actions/exec';
54
import SizePlugin from 'size-plugin-core';
6-
import { fileExists, diffTable, toBool, stripHash } from './utils.js';
5+
import { getPackageManagerAndInstallScript, diffTable, toBool, stripHash } from './utils.js';
76

87
/**
98
* @typedef {ReturnType<typeof import("@actions/github").getOctokit>} Octokit
@@ -50,26 +49,7 @@ async function run(octokit, context, token) {
5049
const buildScript = getInput('build-script') || 'build';
5150
const cwd = process.cwd();
5251

53-
let yarnLock = await fileExists(path.resolve(cwd, 'yarn.lock'));
54-
let pnpmLock = await fileExists(path.resolve(cwd, 'pnpm-lock.yaml'));
55-
let bunLock = await fileExists(path.resolve(cwd, 'bun.lockb'));
56-
let packageLock = await fileExists(path.resolve(cwd, 'package-lock.json'));
57-
58-
let packageManager = 'npm';
59-
let installScript = 'npm install';
60-
if (yarnLock) {
61-
installScript = 'yarn --frozen-lockfile';
62-
packageManager = 'yarn';
63-
} else if (pnpmLock) {
64-
installScript = 'pnpm install --frozen-lockfile';
65-
packageManager = 'pnpm';
66-
} else if (bunLock) {
67-
installScript = 'bun install --frozen-lockfile';
68-
packageManager = 'bun';
69-
} else if (packageLock) {
70-
installScript = 'npm ci';
71-
}
72-
52+
let { packageManager, installScript } = await getPackageManagerAndInstallScript(cwd);
7353
if (getInput('install-script')) {
7454
installScript = getInput('install-script');
7555
}
@@ -127,26 +107,8 @@ async function run(octokit, context, token) {
127107

128108
startGroup(`[base] Install Dependencies`);
129109

130-
yarnLock = await fileExists(path.resolve(cwd, 'yarn.lock'));
131-
pnpmLock = await fileExists(path.resolve(cwd, 'pnpm-lock.yaml'));
132-
bunLock = await fileExists(path.resolve(cwd, 'bun.lockb'));
133-
packageLock = await fileExists(path.resolve(cwd, 'package-lock.json'));
134-
135-
packageManager = 'npm';
136-
installScript = 'npm install';
137-
if (yarnLock) {
138-
installScript = `yarn --frozen-lockfile`;
139-
packageManager = `yarn`;
140-
} else if (pnpmLock) {
141-
installScript = `pnpm install --frozen-lockfile`;
142-
packageManager = `pnpm`;
143-
} else if (bunLock) {
144-
installScript = `bun install --frozen-lockfile`;
145-
packageManager = `bun`;
146-
} else if (packageLock) {
147-
installScript = `npm ci`;
148-
}
149110

111+
({ packageManager, installScript } = await getPackageManagerAndInstallScript(cwd));
150112
if (getInput('install-script')) {
151113
installScript = getInput('install-script');
152114
}

src/utils.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
import fs from 'fs';
2+
import path from 'path';
23
import prettyBytes from 'pretty-bytes';
34

5+
/**
6+
* @param {string} cwd
7+
* @returns {Promise<{ packageManager: string, installScript: string }>}
8+
*/
9+
export async function getPackageManagerAndInstallScript(cwd) {
10+
const [yarnLockExists, pnpmLockExists, bunLockExists, packageLockExists] = await Promise.all([
11+
fileExists(path.resolve(cwd, 'yarn.lock')),
12+
fileExists(path.resolve(cwd, 'pnpm-lock.yaml')),
13+
fileExists(path.resolve(cwd, 'bun.lockb')),
14+
fileExists(path.resolve(cwd, 'package-lock.json')),
15+
]);
16+
17+
let packageManager = 'npm';
18+
let installScript = 'npm install';
19+
if (yarnLockExists) {
20+
installScript = 'yarn --frozen-lockfile';
21+
packageManager = 'yarn';
22+
} else if (pnpmLockExists) {
23+
installScript = 'pnpm install --frozen-lockfile';
24+
packageManager = 'pnpm';
25+
} else if (bunLockExists) {
26+
installScript = 'bun install --frozen-lockfile';
27+
packageManager = 'bun';
28+
} else if (packageLockExists) {
29+
installScript = 'npm ci';
30+
}
31+
32+
return { packageManager, installScript };
33+
}
34+
435
/**
536
* Check if a given file exists and can be accessed.
637
* @param {string} filename

0 commit comments

Comments
 (0)