From c3812bd36ab5c76d03533d7885105283d127e663 Mon Sep 17 00:00:00 2001 From: Taylor McCarthy Date: Sun, 1 Nov 2020 17:04:22 -0500 Subject: [PATCH] Implemented support for repository defined node version files such as '.nvmrc' --- README.md | 15 ++++++++++++ __tests__/installer.test.ts | 46 +++++++++++++++++++++++++++++++++++++ action.yml | 2 ++ dist/index.js | 9 ++++++++ src/main.ts | 11 +++++++++ 5 files changed, 83 insertions(+) diff --git a/README.md b/README.md index 0bd60db7..db6397ce 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,21 @@ steps: - run: npm test ``` +Node version file: + +The `node-version-file` input allows you to use a file within your repository which contains the version of node your project uses for example `.nvmrc`. If both the `node-version` and the `node-version-file` inputs are provided the `node-version` input is used. +> The node version file is read from the project root + +```yaml +steps: +- uses: actions/checkout@v2 +- uses: actions/setup-node@v2 + with: + node-version-file: '.nvmrc' +- run: npm install +- run: npm test +``` + Matrix Testing: ```yaml jobs: diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index dbbc4b44..524b514e 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -36,6 +36,7 @@ describe('setup-node', () => { let dbgSpy: jest.SpyInstance; let whichSpy: jest.SpyInstance; let existsSpy: jest.SpyInstance; + let readFileSyncSpy: jest.SpyInstance; let mkdirpSpy: jest.SpyInstance; let execSpy: jest.SpyInstance; let authSpy: jest.SpyInstance; @@ -67,6 +68,7 @@ describe('setup-node', () => { // io whichSpy = jest.spyOn(io, 'which'); existsSpy = jest.spyOn(fs, 'existsSync'); + readFileSyncSpy = jest.spyOn(fs, 'readFileSync'); mkdirpSpy = jest.spyOn(io, 'mkdirP'); // disable authentication portion for installer tests @@ -490,4 +492,48 @@ describe('setup-node', () => { expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`); }); }); + + describe('node-version-file flag', () => { + it('Not used if node-version is provided', async () => { + // Arrange + inputs['node-version'] = '12'; + + // Act + await main.run(); + + // Assert + expect(readFileSyncSpy).toHaveBeenCalledTimes(0); + }); + + it('Not used if node-version-file not provided', async () => { + // Act + await main.run(); + + // Assert + expect(readFileSyncSpy).toHaveBeenCalledTimes(0); + }); + + it('Reads node-version-file if provided', async () => { + // Arrange + const versionSpec = 'v12'; + const versionFile = '.nvmrc'; + + inputs['node-version-file'] = versionFile; + + readFileSyncSpy.mockImplementation(() => versionSpec); + + // Act + await main.run(); + + // Assert + expect(readFileSyncSpy).toHaveBeenCalledTimes(1); + expect(readFileSyncSpy).toHaveBeenCalledWith( + path.join(__dirname, '..', versionFile), + 'utf8' + ); + expect(logSpy).toHaveBeenCalledWith( + `Resolved ${versionFile} as ${versionSpec}` + ); + }); + }); }); diff --git a/action.yml b/action.yml index e5b901fc..490efc33 100644 --- a/action.yml +++ b/action.yml @@ -7,6 +7,8 @@ inputs: default: 'false' 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' check-latest: description: 'Set this option if you want the action to check for the latest available version that satisfies the version spec' default: false diff --git a/dist/index.js b/dist/index.js index e0e73c55..4e24b3d5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4692,6 +4692,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(__webpack_require__(470)); const installer = __importStar(__webpack_require__(749)); const auth = __importStar(__webpack_require__(202)); +const fs = __webpack_require__(747); const path = __importStar(__webpack_require__(622)); const url_1 = __webpack_require__(835); function run() { @@ -4705,6 +4706,14 @@ function run() { if (!version) { version = core.getInput('version'); } + if (!version) { + const versionFile = core.getInput('node-version-file'); + if (!!versionFile) { + const versionFilePath = path.join(__dirname, '..', versionFile); + version = fs.readFileSync(versionFilePath, 'utf8'); + core.info(`Resolved ${versionFile} as ${version}`); + } + } if (version) { let token = core.getInput('token'); let auth = !token || isGhes() ? undefined : `token ${token}`; diff --git a/src/main.ts b/src/main.ts index 328db0d2..0bea67ad 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import * as core from '@actions/core'; import * as installer from './installer'; import * as auth from './authutil'; +import fs = require('fs'); import * as path from 'path'; import {URL} from 'url'; @@ -15,6 +16,16 @@ export async function run() { version = core.getInput('version'); } + if (!version) { + const versionFile = core.getInput('node-version-file'); + + if (!!versionFile) { + const versionFilePath = path.join(__dirname, '..', versionFile); + version = fs.readFileSync(versionFilePath, 'utf8'); + core.info(`Resolved ${versionFile} as ${version}`); + } + } + if (version) { let token = core.getInput('token'); let auth = !token || isGhes() ? undefined : `token ${token}`;