Skip to content
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

feat: add volta as node-version-file #532

Merged
merged 21 commits into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions __tests__/data/package.json
@@ -1,5 +1,8 @@
{
"engines": {
"node": ">=14.0.0"
},
"volta": {
"node": "14.0.0"
}
}
1 change: 1 addition & 0 deletions __tests__/installer.test.ts
Expand Up @@ -579,6 +579,7 @@ describe('setup-node', () => {
existsSpy.mockImplementationOnce(
input => input === path.join(__dirname, 'data', versionFile)
);

// Act
await main.run();

Expand Down
9 changes: 5 additions & 4 deletions dist/setup/index.js
Expand Up @@ -71768,15 +71768,16 @@ function translateArchToDistUrl(arch) {
}
}
function parseNodeVersionFile(contents) {
var _a, _b;
var _a, _b, _c;
let nodeVersion;
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version;
if (!nodeVersion) {
try {
// Try parsing the file as an NPM `package.json`
// file.
nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node;
// Try parsing the file as an NPM `package.json` file.
nodeVersion = (_b = JSON.parse(contents).volta) === null || _b === void 0 ? void 0 : _b.node;
if (!nodeVersion)
nodeVersion = (_c = JSON.parse(contents).engines) === null || _c === void 0 ? void 0 : _c.node;
if (!nodeVersion)
throw new Error();
}
Expand Down
20 changes: 17 additions & 3 deletions docs/advanced-usage.md
Expand Up @@ -48,16 +48,17 @@ steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '14'
node-version: '16'
check-latest: true
- run: npm ci
- run: npm test
```

## Node version file

The `node-version-file` input accepts a path to a file containing the version of Node.js to be used by a project, for example `.nvmrc`, `.node-version` or `.tool-versions`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used.
See [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax)
The `node-version-file` input accepts a path to a file containing the version of Node.js to be used by a project, for example `.nvmrc`, `.node-version`, `.tool-versions`, or `package.json`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used.
See [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax).

> The action will search for the node version file relative to the repository root.

```yaml
Expand All @@ -70,6 +71,19 @@ steps:
- run: npm test
```

When using the `package.json` input, the action will look for `volta.node` first. If `volta.node` isn't defined, then it will look for `engines.node`.

```json
{
"engines": {
"node": ">=16.0.0"
},
"volta": {
"node": "16.0.0"
}
}
```

## Architecture

You can use any of the [supported operating systems](https://docs.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners), and the compatible `architecture` can be selected using `architecture`. Values are `x86`, `x64`, `arm64`, `armv6l`, `armv7l`, `ppc64le`, `s390x` (not all of the architectures are available on all platforms).
Expand Down
7 changes: 3 additions & 4 deletions src/installer.ts
Expand Up @@ -502,10 +502,9 @@ export function parseNodeVersionFile(contents: string): string {

if (!nodeVersion) {
try {
// Try parsing the file as an NPM `package.json`
// file.
nodeVersion = JSON.parse(contents).engines?.node;

// Try parsing the file as an NPM `package.json` file.
nodeVersion = JSON.parse(contents).volta?.node;
if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node;
if (!nodeVersion) throw new Error();
} catch (err) {
// In the case of an unknown format,
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Expand Up @@ -95,14 +95,17 @@ function resolveVersionInput(): string {
process.env.GITHUB_WORKSPACE!,
versionFileInput
);

if (!fs.existsSync(versionFilePath)) {
throw new Error(
`The specified node version file at: ${versionFilePath} does not exist`
);
}

version = installer.parseNodeVersionFile(
fs.readFileSync(versionFilePath, 'utf8')
);

core.info(`Resolved ${versionFileInput} as ${version}`);
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -6,7 +6,7 @@
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"sourceMap": true,
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"exclude": ["__tests__", "lib", "node_modules"]
Expand Down