From 7fea721543de7199fda8ac291c8341fabe9d0517 Mon Sep 17 00:00:00 2001 From: Matt Oakley Date: Thu, 31 Dec 2020 21:26:46 +0000 Subject: [PATCH] Read version from .nvmrc or .tool-versions --- dist/index.js | 29 +++++++++++++++++++++++++---- src/main.ts | 33 ++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/dist/index.js b/dist/index.js index 0276299c..d199f722 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4695,6 +4695,7 @@ const auth = __importStar(__webpack_require__(202)); const path = __importStar(__webpack_require__(622)); const url_1 = __webpack_require__(835); const os = __webpack_require__(87); +const fs = __webpack_require__(747); function run() { return __awaiter(this, void 0, void 0, function* () { try { @@ -4702,10 +4703,7 @@ function run() { // Version is optional. If supplied, install / use from the tool cache // If not supplied then task is still used to setup proxy, auth, etc... // - let version = core.getInput('node-version'); - if (!version) { - version = core.getInput('version'); - } + let version = parseNodeVersion(); let arch = core.getInput('architecture'); // if architecture supplied but node-version is not // if we don't throw a warning, the already installed x64 node will be used which is not probably what user meant. @@ -4742,6 +4740,29 @@ function isGhes() { const ghUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; } +function parseNodeVersion() { + let nodeVersion = core.getInput('node-version') || core.getInput('version'); + if (!nodeVersion) { + if (fs.existsSync('.nvmrc')) { + // Read from .nvmrc + nodeVersion = fs.readFileSync('.nvmrc', 'utf8').trim(); + console.log(`Using ${nodeVersion} as input from file .nvmrc`); + } + else if (fs.existsSync('.tool-versions')) { + // Read from .tool-versions + const toolVersions = fs.readFileSync('.tool-versions', 'utf8').trim(); + const nodeLine = toolVersions + .split(/\r?\n/) + .filter(e => e.match(/^nodejs\s/))[0]; + nodeVersion = nodeLine.match(/^nodejs\s+(.+)$/)[1]; + console.log(`Using ${nodeVersion} as input from file .tool-versions`); + } + else { + console.log(`Version not specified and not found in .nvmrc or .tool-versions`); + } + } + return nodeVersion; +} //# sourceMappingURL=main.js.map /***/ }), diff --git a/src/main.ts b/src/main.ts index 34332765..92db83c3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,7 @@ import * as auth from './authutil'; import * as path from 'path'; import {URL} from 'url'; import os = require('os'); +import fs = require('fs'); export async function run() { try { @@ -11,11 +12,7 @@ export async function run() { // Version is optional. If supplied, install / use from the tool cache // If not supplied then task is still used to setup proxy, auth, etc... // - let version = core.getInput('node-version'); - if (!version) { - version = core.getInput('version'); - } - + let version = parseNodeVersion(); let arch = core.getInput('architecture'); // if architecture supplied but node-version is not @@ -64,3 +61,29 @@ function isGhes(): boolean { ); return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; } + +function parseNodeVersion(): string { + let nodeVersion = core.getInput('node-version') || core.getInput('version'); + + if (!nodeVersion) { + if (fs.existsSync('.nvmrc')) { + // Read from .nvmrc + nodeVersion = fs.readFileSync('.nvmrc', 'utf8').trim(); + console.log(`Using ${nodeVersion} as input from file .nvmrc`); + } else if (fs.existsSync('.tool-versions')) { + // Read from .tool-versions + const toolVersions = fs.readFileSync('.tool-versions', 'utf8').trim(); + const nodeLine = toolVersions + .split(/\r?\n/) + .filter(e => e.match(/^nodejs\s/))[0]; + nodeVersion = nodeLine.match(/^nodejs\s+(.+)$/)![1]; + console.log(`Using ${nodeVersion} as input from file .tool-versions`); + } else { + console.log( + `Version not specified and not found in .nvmrc or .tool-versions` + ); + } + } + + return nodeVersion; +}