Skip to content

refactor: Extract package manager utility #117

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "NodeNext",
"allowJs": true,
"checkJs": true,
"resolveJsonModule": true,
"noEmit": true
}
}
47 changes: 3 additions & 44 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import path from 'path';
import { getInput, setFailed, startGroup, endGroup, debug } from '@actions/core';
import { context, getOctokit } from '@actions/github';
import { exec } from '@actions/exec';
import SizePlugin from 'size-plugin-core';
import { fileExists, diffTable, toBool, stripHash } from './utils.js';
import { getPackageManagerAndInstallScript, diffTable, toBool, stripHash } from './utils.js';

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

let yarnLock = await fileExists(path.resolve(cwd, 'yarn.lock'));
let pnpmLock = await fileExists(path.resolve(cwd, 'pnpm-lock.yaml'));
let bunLockb = await fileExists(path.resolve(cwd, 'bun.lockb'));
let bunLock = await fileExists(path.resolve(cwd, 'bun.lock'));
let packageLock = await fileExists(path.resolve(cwd, 'package-lock.json'));

let packageManager = 'npm';
let installScript = 'npm install';
if (yarnLock) {
installScript = 'yarn --frozen-lockfile';
packageManager = 'yarn';
} else if (pnpmLock) {
installScript = 'pnpm install --frozen-lockfile';
packageManager = 'pnpm';
} else if (bunLockb || bunLock) {
installScript = 'bun install --frozen-lockfile';
packageManager = 'bun';
} else if (packageLock) {
installScript = 'npm ci';
}

let { packageManager, installScript } = await getPackageManagerAndInstallScript(cwd);
if (getInput('install-script')) {
installScript = getInput('install-script');
}
Expand Down Expand Up @@ -128,27 +107,7 @@ async function run(octokit, context, token) {

startGroup(`[base] Install Dependencies`);

yarnLock = await fileExists(path.resolve(cwd, 'yarn.lock'));
pnpmLock = await fileExists(path.resolve(cwd, 'pnpm-lock.yaml'));
bunLockb = await fileExists(path.resolve(cwd, 'bun.lockb'));
bunLock = await fileExists(path.resolve(cwd, 'bun.lock'));
packageLock = await fileExists(path.resolve(cwd, 'package-lock.json'));

packageManager = 'npm';
installScript = 'npm install';
if (yarnLock) {
installScript = `yarn --frozen-lockfile`;
packageManager = `yarn`;
} else if (pnpmLock) {
installScript = `pnpm install --frozen-lockfile`;
packageManager = `pnpm`;
} else if (bunLockb || bunLock) {
installScript = `bun install --frozen-lockfile`;
packageManager = `bun`;
} else if (packageLock) {
installScript = `npm ci`;
}

({ packageManager, installScript } = await getPackageManagerAndInstallScript(cwd));
if (getInput('install-script')) {
installScript = getInput('install-script');
}
Expand Down
32 changes: 32 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
import fs from 'fs';
import path from 'path';
import prettyBytes from 'pretty-bytes';

/**
* @param {string} cwd
* @returns {Promise<{ packageManager: string, installScript: string }>}
*/
export async function getPackageManagerAndInstallScript(cwd) {
const [yarnLockExists, pnpmLockExists, bunLockBinaryExists, bunLockExists, packageLockExists] = await Promise.all([
fileExists(path.resolve(cwd, 'yarn.lock')),
fileExists(path.resolve(cwd, 'pnpm-lock.yaml')),
fileExists(path.resolve(cwd, 'bun.lockb')),
fileExists(path.resolve(cwd, 'bun.lock')),
fileExists(path.resolve(cwd, 'package-lock.json')),
]);

let packageManager = 'npm';
let installScript = 'npm install';
if (yarnLockExists) {
installScript = 'yarn --frozen-lockfile';
packageManager = 'yarn';
} else if (pnpmLockExists) {
installScript = 'pnpm install --frozen-lockfile';
packageManager = 'pnpm';
} else if (bunLockBinaryExists || bunLockExists) {
installScript = 'bun install --frozen-lockfile';
packageManager = 'bun';
} else if (packageLockExists) {
installScript = 'npm ci';
}

return { packageManager, installScript };
}

/**
* Check if a given file exists and can be accessed.
* @param {string} filename
Expand Down
15 changes: 14 additions & 1 deletion tests/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toBool, getDeltaText, iconForDifference, diffTable, fileExists, stripHash } from '../src/utils.js';
import path from 'path';
import { toBool, getDeltaText, iconForDifference, diffTable, getPackageManagerAndInstallScript, fileExists, stripHash } from '../src/utils.js';

test('toBool', () => {
expect(toBool('1')).toBe(true);
Expand Down Expand Up @@ -65,6 +66,18 @@ test('diffTable', () => {
expect(diffTable([files[2]], { ...defaultOptions })).toMatchSnapshot();
});

test('getPackageManagerAndInstallScript', async () => {
let cwd = process.cwd();
let { packageManager, installScript } = await getPackageManagerAndInstallScript(cwd);
expect(packageManager).toBe('npm');
expect(installScript).toBe('npm ci');

cwd = path.join(cwd, 'tests');
({ packageManager, installScript } = await getPackageManagerAndInstallScript(cwd));
expect(packageManager).toBe('npm');
expect(installScript).toBe('npm install');
});

test('fileExists', async () => {
expect(await fileExists('package.json')).toBe(true);
expect(await fileExists('file-that-does-not-exist')).toBe(false);
Expand Down