Skip to content

Commit 2936fe8

Browse files
JP250552styfleSayakMukhopadhyayjrparish
authored
Added option to enable corepack (#1)
Co-authored-by: Steven <steven@ceriously.com> Co-authored-by: Sayak Mukhopadhyay <mukhopadhyaysayak@gmail.com> Co-authored-by: Jacob Parish <jacob.parish.1@gmail.com>
1 parent 5ef044f commit 2936fe8

8 files changed

Lines changed: 132 additions & 15 deletions

File tree

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ See [action.yml](action.yml)
2626
node-version: ''
2727

2828
# File containing the version Spec of the version to use. Examples: .nvmrc, .node-version, .tool-versions.
29-
# If node-version and node-version-file are both provided the action will use version from node-version.
29+
# If node-version and node-version-file are both provided the action will use version from node-version.
3030
node-version-file: ''
3131

32-
# Set this option if you want the action to check for the latest available version
32+
# Set this option if you want the action to check for the latest available version
3333
# that satisfies the version spec.
34-
# It will only get affect for lts Nodejs versions (12.x, >=10.15.0, lts/Hydrogen).
34+
# It will only get affect for lts Nodejs versions (12.x, >=10.15.0, lts/Hydrogen).
3535
# Default: false
3636
check-latest: false
3737

3838
# Target architecture for Node to use. Examples: x86, x64. Will use system architecture by default.
39-
# Default: ''. The action use system architecture by default
39+
# Default: ''. The action use system architecture by default
4040
architecture: ''
4141

42-
# Used to pull node distributions from https://github.com/actions/node-versions.
43-
# Since there's a default, this is typically not supplied by the user.
44-
# When running this action on github.com, the default value is sufficient.
42+
# Used to pull node distributions from https://github.com/actions/node-versions.
43+
# Since there's a default, this is typically not supplied by the user.
44+
# When running this action on github.com, the default value is sufficient.
4545
# When running on GHES, you can pass a personal access token for github.com if you are experiencing rate limiting.
4646
#
4747
# We recommend using a service account with the least permissions necessary. Also
@@ -57,18 +57,18 @@ See [action.yml](action.yml)
5757
# Default: ''
5858
cache: ''
5959

60-
# Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc.
61-
# It will generate hash from the target file for primary key. It works only If cache is specified.
60+
# Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc.
61+
# It will generate hash from the target file for primary key. It works only If cache is specified.
6262
# Supports wildcards or a list of file names for caching multiple dependencies.
6363
# Default: ''
6464
cache-dependency-path: ''
6565

66-
# Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file,
66+
# Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file,
6767
# and set up auth to read in from env.NODE_AUTH_TOKEN.
6868
# Default: ''
6969
registry-url: ''
7070

71-
# Optional scope for authenticating against scoped registries.
71+
# Optional scope for authenticating against scoped registries.
7272
# Will fall back to the repository owner when using the GitHub Packages registry (https://npm.pkg.github.com/).
7373
# Default: ''
7474
scope: ''
@@ -203,6 +203,7 @@ If the runner is not able to access github.com, any Nodejs versions requested du
203203
- [Publishing to npmjs and GPR with npm](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-npm)
204204
- [Publishing to npmjs and GPR with yarn](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-yarn)
205205
- [Using private packages](docs/advanced-usage.md#use-private-packages)
206+
- [Enabling Corepack](docs/advanced-usage.md#enabling-corepack)
206207

207208
## License
208209

__tests__/official-installer.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,4 +825,36 @@ describe('setup-node', () => {
825825
}
826826
);
827827
});
828+
829+
describe('corepack flag', () => {
830+
it('use corepack if specified', async () => {
831+
inputs['corepack'] = 'true';
832+
await main.run();
833+
expect(getExecOutputSpy).toHaveBeenCalledWith(
834+
'corepack',
835+
['enable'],
836+
expect.anything()
837+
);
838+
});
839+
840+
it('use corepack with given package manager', async () => {
841+
inputs['corepack'] = 'npm';
842+
await main.run();
843+
expect(getExecOutputSpy).toHaveBeenCalledWith(
844+
'corepack',
845+
['enable', 'npm'],
846+
expect.anything()
847+
);
848+
});
849+
850+
it('use corepack with multiple package managers', async () => {
851+
inputs['corepack'] = 'npm yarn';
852+
await main.run();
853+
expect(getExecOutputSpy).toHaveBeenCalledWith(
854+
'corepack',
855+
['enable', 'npm', 'yarn'],
856+
expect.anything()
857+
);
858+
});
859+
});
828860
});

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ inputs:
2525
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
2626
cache-dependency-path:
2727
description: 'Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc. Supports wildcards or a list of file names for caching multiple dependencies.'
28+
corepack:
29+
description: 'Used to specify whether to enable Corepack. Set to true to enable all package managers or set it to one or more package manager names separated by a space. Supported package manager names: npm, yarn, pnpm.'
30+
default: 'false'
2831
# TODO: add input to control forcing to pull from cloud or dist.
2932
# escape valve for someone having issues or needing the absolute latest which isn't cached yet
3033
outputs:
31-
cache-hit:
34+
cache-hit:
3235
description: 'A boolean value to indicate if a cache was hit.'
3336
node-version:
3437
description: 'The installed node version.'

dist/cache-save/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83321,7 +83321,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8332183321
});
8332283322
};
8332383323
Object.defineProperty(exports, "__esModule", ({ value: true }));
83324-
exports.unique = exports.printEnvDetailsAndSetOutput = exports.parseNodeVersionFile = void 0;
83324+
exports.enableCorepack = exports.unique = exports.printEnvDetailsAndSetOutput = exports.parseNodeVersionFile = void 0;
8332583325
const core = __importStar(__nccwpck_require__(2186));
8332683326
const exec = __importStar(__nccwpck_require__(1514));
8332783327
function parseNodeVersionFile(contents) {
@@ -83393,6 +83393,21 @@ const unique = () => {
8339383393
};
8339483394
};
8339583395
exports.unique = unique;
83396+
function enableCorepack(input) {
83397+
return __awaiter(this, void 0, void 0, function* () {
83398+
const corepackArgs = ['enable'];
83399+
if (input.length > 0 && input !== 'false') {
83400+
if (input !== 'true') {
83401+
const packageManagers = input.split(' ');
83402+
corepackArgs.push(...packageManagers);
83403+
}
83404+
yield exec.getExecOutput('corepack', corepackArgs, {
83405+
ignoreReturnCode: true
83406+
});
83407+
}
83408+
});
83409+
}
83410+
exports.enableCorepack = enableCorepack;
8339683411

8339783412

8339883413
/***/ }),

dist/setup/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93698,6 +93698,8 @@ function run() {
9369893698
if (registryUrl) {
9369993699
auth.configAuthentication(registryUrl, alwaysAuth);
9370093700
}
93701+
const corepack = core.getInput('corepack') || 'false';
93702+
yield (0, util_1.enableCorepack)(corepack);
9370193703
if (cache && (0, cache_utils_1.isCacheFeatureAvailable)()) {
9370293704
core.saveState(constants_1.State.CachePackageManager, cache);
9370393705
const cacheDependencyPath = core.getInput('cache-dependency-path');
@@ -93775,7 +93777,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9377593777
});
9377693778
};
9377793779
Object.defineProperty(exports, "__esModule", ({ value: true }));
93778-
exports.unique = exports.printEnvDetailsAndSetOutput = exports.parseNodeVersionFile = void 0;
93780+
exports.enableCorepack = exports.unique = exports.printEnvDetailsAndSetOutput = exports.parseNodeVersionFile = void 0;
9377993781
const core = __importStar(__nccwpck_require__(2186));
9378093782
const exec = __importStar(__nccwpck_require__(1514));
9378193783
function parseNodeVersionFile(contents) {
@@ -93847,6 +93849,21 @@ const unique = () => {
9384793849
};
9384893850
};
9384993851
exports.unique = unique;
93852+
function enableCorepack(input) {
93853+
return __awaiter(this, void 0, void 0, function* () {
93854+
const corepackArgs = ['enable'];
93855+
if (input.length > 0 && input !== 'false') {
93856+
if (input !== 'true') {
93857+
const packageManagers = input.split(' ');
93858+
corepackArgs.push(...packageManagers);
93859+
}
93860+
yield exec.getExecOutput('corepack', corepackArgs, {
93861+
ignoreReturnCode: true
93862+
});
93863+
}
93864+
});
93865+
}
93866+
exports.enableCorepack = enableCorepack;
9385093867

9385193868

9385293869
/***/ }),

docs/advanced-usage.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,32 @@ Please refer to the [Ensuring workflow access to your package - Configuring a pa
416416

417417
### always-auth input
418418
The always-auth input sets `always-auth=true` in .npmrc file. With this option set [npm](https://docs.npmjs.com/cli/v6/using-npm/config#always-auth)/yarn sends the authentication credentials when making a request to the registries.
419+
420+
## Enabling Corepack
421+
You can enable [Corepack](https://github.com/nodejs/corepack) by using the `corepack` input. You can then use `pnpm` and `yarn` commands in your project.
422+
423+
```yaml
424+
steps:
425+
- uses: actions/checkout@v3
426+
- uses: actions/setup-node@v3
427+
with:
428+
node-version: '16.x'
429+
corepack: true
430+
- name: Install dependencies
431+
run: yarn install --immutable
432+
```
433+
434+
You can also pass package manager names separated by a space to enable corepack for specific package managers only.
435+
436+
```yaml
437+
steps:
438+
- uses: actions/checkout@v3
439+
- uses: actions/setup-node@v3
440+
with:
441+
node-version: '16.x'
442+
corepack: yarn pnpm
443+
- name: Install dependencies
444+
run: yarn install --immutable
445+
```
446+
447+
This option by default is `false` as Corepack is still in experimental phase.

src/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import * as path from 'path';
88
import {restoreCache} from './cache-restore';
99
import {isCacheFeatureAvailable} from './cache-utils';
1010
import {getNodejsDistribution} from './distributions/installer-factory';
11-
import {parseNodeVersionFile, printEnvDetailsAndSetOutput} from './util';
11+
import {
12+
parseNodeVersionFile,
13+
printEnvDetailsAndSetOutput,
14+
enableCorepack
15+
} from './util';
1216
import {State} from './constants';
1317

1418
export async function run() {
@@ -60,6 +64,9 @@ export async function run() {
6064
auth.configAuthentication(registryUrl, alwaysAuth);
6165
}
6266

67+
const corepack = core.getInput('corepack') || 'false';
68+
await enableCorepack(corepack);
69+
6370
if (cache && isCacheFeatureAvailable()) {
6471
core.saveState(State.CachePackageManager, cache);
6572
const cacheDependencyPath = core.getInput('cache-dependency-path');

src/util.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,16 @@ export const unique = () => {
7070
return true;
7171
};
7272
};
73+
74+
export async function enableCorepack(input: string): Promise<void> {
75+
const corepackArgs = ['enable'];
76+
if (input.length > 0 && input !== 'false') {
77+
if (input !== 'true') {
78+
const packageManagers = input.split(' ');
79+
corepackArgs.push(...packageManagers);
80+
}
81+
await exec.getExecOutput('corepack', corepackArgs, {
82+
ignoreReturnCode: true
83+
});
84+
}
85+
}

0 commit comments

Comments
 (0)