Skip to content

Commit

Permalink
Merge pull request #532 from jef/main
Browse files Browse the repository at this point in the history
feat: add volta as node-version-file
  • Loading branch information
marko-zivic-93 authored Sep 12, 2022
2 parents 30f0e7d + 4cffe5c commit 9f3a02b
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 33 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ jobs:
- name: Verify node
run: __tests__/verify-node.sh 14

version-file-volta:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
steps:
- uses: actions/checkout@v3
- name: Remove engines from package.json
shell: bash
run: cat <<< "$(jq 'del(.engines)' ./__tests__/data/package.json)" > ./__tests__/data/package.json
- name: Setup node from node version file
uses: ./
with:
node-version-file: '__tests__/data/package.json'
- name: Verify node
run: __tests__/verify-node.sh 14

node-dist:
runs-on: ${{ matrix.os }}
strategy:
Expand Down
3 changes: 3 additions & 0 deletions __tests__/data/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"engines": {
"node": "^14.0.0"
},
"volta": {
"node": "14.0.0"
}
}
1 change: 1 addition & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ describe('setup-node', () => {
existsSpy.mockImplementationOnce(
input => input === path.join(__dirname, 'data', versionFile)
);

// Act
await main.run();

Expand Down
28 changes: 12 additions & 16 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73530,29 +73530,25 @@ function translateArchToDistUrl(arch) {
}
}
function parseNodeVersionFile(contents) {
var _a, _b;
var _a, _b, _c;
let nodeVersion;
// Try parsing the file as an NPM `package.json`
// file.
// Try parsing the file as an NPM `package.json` file.
try {
nodeVersion = (_a = JSON.parse(contents).engines) === null || _a === void 0 ? void 0 : _a.node;
nodeVersion = (_a = JSON.parse(contents).volta) === null || _a === void 0 ? void 0 : _a.node;
if (!nodeVersion)
nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node;
}
catch (_c) {
catch (_d) {
core.warning('Node version file is not JSON file');
}
if (!nodeVersion) {
try {
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
nodeVersion = (_b = found === null || found === void 0 ? void 0 : found.groups) === null || _b === void 0 ? void 0 : _b.version;
if (!nodeVersion)
throw new Error();
}
catch (err) {
// In the case of an unknown format,
// return as is and evaluate the version separately.
nodeVersion = contents.trim();
}
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
nodeVersion = (_c = found === null || found === void 0 ? void 0 : found.groups) === null || _c === void 0 ? void 0 : _c.version;
}
// In the case of an unknown format,
// return as is and evaluate the version separately.
if (!nodeVersion)
nodeVersion = contents.trim();
return nodeVersion;
}
exports.parseNodeVersionFile = parseNodeVersionFile;
Expand Down
20 changes: 17 additions & 3 deletions docs/advanced-usage.md
Original file line number Diff line number Diff line change
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/using-github-hosted-runners/about-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
22 changes: 9 additions & 13 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,27 +497,23 @@ function translateArchToDistUrl(arch: string): string {
export function parseNodeVersionFile(contents: string): string {
let nodeVersion: string | undefined;

// Try parsing the file as an NPM `package.json`
// file.
// Try parsing the file as an NPM `package.json` file.
try {
nodeVersion = JSON.parse(contents).engines?.node;
nodeVersion = JSON.parse(contents).volta?.node;
if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node;
} catch {
core.warning('Node version file is not JSON file');
}

if (!nodeVersion) {
try {
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
nodeVersion = found?.groups?.version;

if (!nodeVersion) throw new Error();
} catch (err) {
// In the case of an unknown format,
// return as is and evaluate the version separately.
nodeVersion = contents.trim();
}
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
nodeVersion = found?.groups?.version;
}

// In the case of an unknown format,
// return as is and evaluate the version separately.
if (!nodeVersion) nodeVersion = contents.trim();

return nodeVersion as string;
}

Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 9f3a02b

Please sign in to comment.