From 9f993b97197ee7f2905f9b08fb77d7b236223022 Mon Sep 17 00:00:00 2001 From: Hideki Igarashi Date: Thu, 2 Dec 2021 00:44:59 +0900 Subject: [PATCH] Add support for asdf format as Node.js version file --- .github/workflows/versions.yml | 9 +++++---- __tests__/data/.tool-versions | 1 + __tests__/installer.test.ts | 20 +++++++++++--------- action.yml | 8 ++++---- docs/advanced-usage.md | 2 +- src/installer.ts | 12 ++++++++---- 6 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 __tests__/data/.tool-versions diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index 459f33cdc..a1596669f 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -3,14 +3,14 @@ name: versions on: pull_request: paths-ignore: - - '**.md' - push: + - '**.md' + push: branches: - main - releases/* paths-ignore: - '**.md' - + jobs: local-cache: runs-on: ${{ matrix.os }} @@ -84,12 +84,13 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] + node-version-file: [.nvmrc, .tool-versions] steps: - uses: actions/checkout@v2 - name: Setup node from node version file uses: ./ with: - node-version-file: '__tests__/data/.nvmrc' + node-version-file: __tests__/data/${{ matrix.node-version-file }} - name: Verify node run: __tests__/verify-node.sh 14 diff --git a/__tests__/data/.tool-versions b/__tests__/data/.tool-versions new file mode 100644 index 000000000..317343f30 --- /dev/null +++ b/__tests__/data/.tool-versions @@ -0,0 +1 @@ +nodejs 14.0.0 diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 01f604a3c..12077d2e8 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -867,15 +867,17 @@ describe('setup-node', () => { describe('helper methods', () => { describe('parseNodeVersionFile', () => { each` - contents | expected - ${'12'} | ${'12'} - ${'12.3'} | ${'12.3'} - ${'12.3.4'} | ${'12.3.4'} - ${'v12.3.4'} | ${'12.3.4'} - ${'lts/erbium'} | ${'lts/erbium'} - ${'lts/*'} | ${'lts/*'} - ${''} | ${''} - ${'unknown format'} | ${'unknown format'} + contents | expected + ${'12'} | ${'12'} + ${'12.3'} | ${'12.3'} + ${'12.3.4'} | ${'12.3.4'} + ${'v12.3.4'} | ${'12.3.4'} + ${'lts/erbium'} | ${'lts/erbium'} + ${'lts/*'} | ${'lts/*'} + ${'nodejs 12.3.4'} | ${'12.3.4'} + ${'ruby 2.3.4\nnodejs 12.3.4\npython 3.4.5'} | ${'12.3.4'} + ${''} | ${''} + ${'unknown format'} | ${'unknown format'} `.it('parses "$contents"', ({contents, expected}) => { expect(im.parseNodeVersionFile(contents)).toBe(expected); }); diff --git a/action.yml b/action.yml index 879ac00b5..d7509c035 100644 --- a/action.yml +++ b/action.yml @@ -8,7 +8,7 @@ inputs: node-version: description: 'Version Spec of the version to use. Examples: 12.x, 10.15.1, >=10.15.0' node-version-file: - description: 'File containing the version Spec of the version to use. Examples: .nvmrc, .node-version' + description: 'File containing the version Spec of the version to use. Examples: .nvmrc, .node-version, .tool-versions' architecture: description: 'Target architecture for Node to use. Examples: x86, x64. Will use system architecture by default.' check-latest: @@ -25,17 +25,17 @@ inputs: description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm' cache-dependency-path: 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.' -# TODO: add input to control forcing to pull from cloud or dist. +# TODO: add input to control forcing to pull from cloud or dist. # escape valve for someone having issues or needing the absolute latest which isn't cached yet # Deprecated option, do not use. Will not be supported after October 1, 2019 version: description: 'Deprecated. Use node-version instead. Will not be supported after October 1, 2019' deprecationMessage: 'The version property will not be supported after October 1, 2019. Use node-version instead' outputs: - cache-hit: + cache-hit: description: 'A boolean value to indicate if a cache was hit' runs: using: 'node12' main: 'dist/setup/index.js' post: 'dist/cache-save/index.js' - post-if: success() \ No newline at end of file + post-if: success() diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index 960772f5c..49c8a1526 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -21,7 +21,7 @@ steps: ## 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` or `.node-version`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used. +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 action will search for the node version file relative to the repository root. diff --git a/src/installer.ts b/src/installer.ts index a9baae0a1..2a22e77c4 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -466,10 +466,14 @@ function translateArchToDistUrl(arch: string): string { } export function parseNodeVersionFile(contents: string): string { - let nodeVersion = contents.trim(); + const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); + const nodeVersion = found?.groups?.version; - if (/^v\d/.test(nodeVersion)) { - nodeVersion = nodeVersion.substring(1); + if (nodeVersion) { + return nodeVersion; } - return nodeVersion; + + // In the case of an unknown format, + // return as is and evaluate the version separately. + return contents.trim(); }