Skip to content

Commit

Permalink
Read version from .nvmrc or .tool-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Oakley committed Jan 1, 2021
1 parent c46424e commit 7fea721
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
29 changes: 25 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4695,17 +4695,15 @@ 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 {
//
// 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.
Expand Down Expand Up @@ -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

/***/ }),
Expand Down
33 changes: 28 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ 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 {
//
// 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
Expand Down Expand Up @@ -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;
}

0 comments on commit 7fea721

Please sign in to comment.