diff --git a/README.md b/README.md index ec690332..1ccefa7f 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ It's **always** recommended to commit the lockfile of your package manager for s The action has a built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/cache) under the hood for caching global packages data but requires less configuration settings. Supported package managers are `npm`, `yarn`, `pnpm` (v6.10+). The `cache` input is optional. -Caching is turned on by default when a `packageManager` or `devEngines.packageManager` field is detected in the `package.json` file. The `package-manager-cache` input provides control over this automatic caching behavior. By default, `package-manager-cache` is set to `true`, which enables caching when a valid package manager field is detected in the `package.json` file. To disable this automatic caching, set the `package-manager-cache` input to `false`. +Caching is turned on by default when a `packageManager` field is detected in the `package.json` file. The `package-manager-cache` input provides control over this automatic caching behavior. By default, `package-manager-cache` is set to `true`, which enables caching when a valid package manager field is detected in the `package.json` file. To disable this automatic caching, set the `package-manager-cache` input to `false`. ```yaml steps: @@ -147,7 +147,7 @@ steps: package-manager-cache: false - run: npm ci ``` -> If no `packageManager` or `devEngines.packageManager` field is detected in the `package.json` file, caching will remain disabled unless explicitly configured. +> If no valid `packageManager` field is detected in the `package.json` file, caching will remain disabled unless explicitly configured. The action defaults to search for the dependency file (`package-lock.json`, `npm-shrinkwrap.json` or `yarn.lock`) in the repository root, and uses its hash as a part of the cache key. Use `cache-dependency-path` for cases when multiple dependency files are used, or they are located in different subdirectories. diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 96a79825..5af57092 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -285,28 +285,6 @@ describe('main tests', () => { }); describe('cache feature tests', () => { - it('Should enable caching with the resolved package manager from devEngines.packageManager in package.json when the cache input is not provided', async () => { - inputs['package-manager-cache'] = 'true'; - inputs['cache'] = ''; // No cache input is provided - - inSpy.mockImplementation(name => inputs[name]); - - const readFileSpy = jest.spyOn(fs, 'readFileSync'); - readFileSpy.mockImplementation(() => - JSON.stringify({ - devEngines: { - packageManager: { - name: 'pnpm' - } - } - }) - ); - - await main.run(); - - expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'pnpm'); - }); - it('Should enable caching with the resolved package manager from packageManager field in package.json when the cache input is not provided', async () => { inputs['package-manager-cache'] = 'true'; inputs['cache'] = ''; // No cache input is provided diff --git a/dist/setup/index.js b/dist/setup/index.js index f06d22c9..c6381da6 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -99683,21 +99683,17 @@ function resolveVersionInput() { return version; } function getNameFromPackageManagerField() { - var _a, _b; - // Check devEngines.packageManager and packageManager field in package.json + // Check packageManager field in package.json const SUPPORTED_PACKAGE_MANAGERS = ['npm', 'yarn', 'pnpm']; try { const packageJson = JSON.parse(fs_1.default.readFileSync(path.join(process.env.GITHUB_WORKSPACE, 'package.json'), 'utf-8')); - return (((_b = (_a = packageJson.devEngines) === null || _a === void 0 ? void 0 : _a.packageManager) === null || _b === void 0 ? void 0 : _b.name) || - (() => { - const pm = packageJson.packageManager; - if (typeof pm === 'string') { - const regex = new RegExp(`^(?:\\^)?(${SUPPORTED_PACKAGE_MANAGERS.join('|')})@`); - const match = pm.match(regex); - return match ? match[1] : undefined; - } - return undefined; - })()); + const pm = packageJson.packageManager; + if (typeof pm === 'string') { + const regex = new RegExp(`^(?:\\^)?(${SUPPORTED_PACKAGE_MANAGERS.join('|')})@`); + const match = pm.match(regex); + return match ? match[1] : undefined; + } + return undefined; } catch (err) { return undefined; diff --git a/src/main.ts b/src/main.ts index 0b622102..f169cef0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -127,7 +127,7 @@ function resolveVersionInput(): string { } export function getNameFromPackageManagerField(): string | undefined { - // Check devEngines.packageManager and packageManager field in package.json + // Check packageManager field in package.json const SUPPORTED_PACKAGE_MANAGERS = ['npm', 'yarn', 'pnpm']; try { const packageJson = JSON.parse( @@ -136,20 +136,15 @@ export function getNameFromPackageManagerField(): string | undefined { 'utf-8' ) ); - return ( - packageJson.devEngines?.packageManager?.name || - (() => { - const pm = packageJson.packageManager; - if (typeof pm === 'string') { - const regex = new RegExp( - `^(?:\\^)?(${SUPPORTED_PACKAGE_MANAGERS.join('|')})@` - ); - const match = pm.match(regex); - return match ? match[1] : undefined; - } - return undefined; - })() - ); + const pm = packageJson.packageManager; + if (typeof pm === 'string') { + const regex = new RegExp( + `^(?:\\^)?(${SUPPORTED_PACKAGE_MANAGERS.join('|')})@` + ); + const match = pm.match(regex); + return match ? match[1] : undefined; + } + return undefined; } catch (err) { return undefined; }