diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..aee6398 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "NodeNext", + "allowJs": true, + "checkJs": true, + "resolveJsonModule": true, + "noEmit": true + } +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index d67b5f4..71b536c 100644 --- a/src/index.js +++ b/src/index.js @@ -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} Octokit @@ -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'); } @@ -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'); } diff --git a/src/utils.js b/src/utils.js index 540cd8b..da71df4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 diff --git a/tests/utils.spec.js b/tests/utils.spec.js index 5aaef4b..6a39cb8 100644 --- a/tests/utils.spec.js +++ b/tests/utils.spec.js @@ -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); @@ -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);