Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support .nvmrc #338

Merged
merged 67 commits into from Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
c3812bd
Implemented support for repository defined node version files such as…
TJMcCarthy95 Nov 1, 2020
a0d376d
Expand node version file support to include lts based codenames
TJMcCarthy95 Nov 19, 2020
75c8584
fixed missing brackets and merged main
hkaur008 Sep 30, 2021
820128a
test commit ignore
Xlient Sep 30, 2021
ac10981
Revert "test commit ignore"
hkaur008 Sep 30, 2021
bca2818
fixed merge conflicts
Xlient Oct 1, 2021
82441b3
Refactored code and removed redundant logic
Xlient Oct 1, 2021
052bc8b
created test workflow & nvmrc file
Xlient Oct 1, 2021
7f7335c
updated nvmrc & added ignore paths
Xlient Oct 4, 2021
1f26500
fixed error in workflow file
Xlient Oct 4, 2021
510ffc9
Update version-file-workflow.yml
Xlient Oct 4, 2021
2579409
fixed merge conflicts
Xlient Oct 4, 2021
1650600
Merge branch 'pull209' of https://github.com/hkaur008/setup-node into…
Xlient Oct 4, 2021
2fb803b
refactored installer.test.ts
hkaur008 Oct 4, 2021
42c8cd4
deleted node-version-file
hkaur008 Oct 4, 2021
e471f47
code syntax matched
hkaur008 Oct 4, 2021
b1ed0c3
moved node version section in README to advanced usage
Xlient Oct 5, 2021
c178bbb
removed test logic in ParseNodeVersionFile for lts and partial matching
Xlient Oct 5, 2021
eb7c472
removed extra tabs (versions.yml)
hkaur008 Oct 5, 2021
7fb46ef
removed extra tabs (versions.yml)
hkaur008 Oct 5, 2021
ec979d0
removed unnessary test
Xlient Oct 6, 2021
fe30936
Delete index.js
Xlient Oct 6, 2021
7d40f6c
added correct path to version file
Xlient Oct 6, 2021
6e5214f
build successful
hkaur008 Oct 6, 2021
5b7f417
build successful
hkaur008 Oct 6, 2021
ab0d6af
refactored test to seperate lts alias describe()
Xlient Oct 7, 2021
fa3de08
removed await from line 25 main.ts
hkaur008 Oct 7, 2021
b8f2a97
changed path of versionFilePath line 24 main.ts
hkaur008 Oct 7, 2021
a03cbe2
changed import from syntax instead require line 3 and line 8
hkaur008 Oct 7, 2021
b94f87b
parseNodeVersionFile changed from async function
hkaur008 Oct 7, 2021
10f97d5
removed extra line 15 installer.js
hkaur008 Oct 7, 2021
04a1a57
node-version-file: '../.nvmrc' changed to .nvmrc
hkaur008 Oct 7, 2021
aa06bd5
import * as hc from '@actions/http-client';
hkaur008 Oct 7, 2021
3f779ed
built done for changes
hkaur008 Oct 7, 2021
6f69e28
defined GITHUB_WORKSPACE env variable for node-version-file test
Xlient Oct 8, 2021
01342a8
- name: Setup node from node version file
hkaur008 Oct 25, 2021
fa4605f
.nvmrc
hkaur008 Oct 25, 2021
acc44a6
paragraph added in version.yml btw jobs
hkaur008 Oct 25, 2021
f606613
diff explanation for advanced-usage.md
hkaur008 Oct 25, 2021
9cdff02
link to supported section added
hkaur008 Oct 25, 2021
7ba3c8c
added paragraph in installer.test.ts line 11
hkaur008 Oct 25, 2021
96fa061
it blocks in lowercase
hkaur008 Oct 25, 2021
f927de6
separate function for resolving version file added
hkaur008 Oct 25, 2021
1b0aeb7
v14 .nvmrc
hkaur008 Oct 26, 2021
aab770a
set added to check the version of the file
hkaur008 Oct 26, 2021
36bb47b
return version otherwise action will fail
hkaur008 Oct 26, 2021
34ece93
syncing with main
hkaur008 Oct 26, 2021
edcb32f
Merge branch 'actions:main' into pull209
hkaur008 Nov 2, 2021
6382f41
checks implemented for node version file
hkaur008 Nov 2, 2021
ff9e2a3
Merge branch 'pull209' of https://github.com/hkaur008/setup-node into…
hkaur008 Nov 2, 2021
cc29140
build syncing
hkaur008 Nov 2, 2021
36c80a7
minor changes and error fix
hkaur008 Nov 2, 2021
7ff10b1
advanced.md changes
hkaur008 Nov 2, 2021
c1c4632
build success
hkaur008 Nov 2, 2021
243f7be
fixed unit tests
Xlient Nov 2, 2021
e313959
updated path to version file
Xlient Nov 2, 2021
8dfead4
fix error in unit test
Xlient Nov 8, 2021
d465795
add fix for installer tests
dmitry-shibanov Nov 8, 2021
12a17b4
Merge pull request #2 from dmitry-shibanov/v-dmshib/fix-installer-tests
Xlient Nov 8, 2021
caf9b24
removed redundant test
Xlient Nov 11, 2021
6ef0f8d
update dist
Xlient Nov 11, 2021
cc6b809
Update docs/advanced-usage.md
Xlient Nov 15, 2021
4e4d327
adds path to error output
Xlient Nov 16, 2021
a2b4d83
import os = require('os'); moved back
hkaur008 Nov 22, 2021
e75d7c1
Merge remote-tracking branch 'refs/remotes/origin/pull209' into pull209
hkaur008 Nov 22, 2021
0f9cdab
build
hkaur008 Nov 22, 2021
a7ed1ce
removed n-node-version file
hkaur008 Nov 25, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/versions.yml
Expand Up @@ -78,6 +78,21 @@ jobs:
run: __tests__/verify-node.sh "${{ matrix.node-version }}"
shell: bash

version-file:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v2
- name: Setup node from node version file
uses: ./
with:
node-version-file: '.nvmrc'
hkaur008 marked this conversation as resolved.
Show resolved Hide resolved
- name: Verify node
- run: __tests__/verify-node.sh 14

node-dist:
runs-on: ${{ matrix.os }}
strategy:
Expand Down
15 changes: 8 additions & 7 deletions README.md
@@ -1,5 +1,4 @@
# setup-node

<p align="left">
<a href="https://github.com/actions/setup-node/actions?query=workflow%3Abuild-test"><img alt="build-test status" src="https://github.com/actions/setup-node/workflows/build-test/badge.svg"></a> <a href="https://github.com/actions/setup-node/actions?query=workflow%3Aversions"><img alt="versions status" src="https://github.com/actions/setup-node/workflows/versions/badge.svg"></a> <a href="https://github.com/actions/setup-node/actions?query=workflow%3Aproxy"><img alt="proxy status" src="https://github.com/actions/setup-node/workflows/proxy/badge.svg"></a>
</p>
Expand Down Expand Up @@ -72,6 +71,7 @@ steps:
- run: npm test
```


hkaur008 marked this conversation as resolved.
Show resolved Hide resolved
## Matrix Testing:
```yaml
jobs:
Expand All @@ -93,12 +93,13 @@ jobs:
## Advanced usage

1. [Check latest version](docs/advanced-usage.md#check-latest-version)
2. [Using different architectures](docs/advanced-usage.md#architecture)
3. [Caching packages dependencies](docs/advanced-usage.md#caching-packages-dependencies)
4. [Using multiple operating systems and architectures](docs/advanced-usage.md#multiple-operating-systems-and-architectures)
5. [Publishing to npmjs and GPR with npm](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-npm)
6. [Publishing to npmjs and GPR with yarn](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-yarn)
7. [Using private packages](docs/advanced-usage.md#use-private-packages)
2. [Using a node version file](docs/advanced-usage.md#Node-version-file)
3. [Using different architectures](docs/advanced-usage.md#architecture)
4. [Caching packages dependencies](docs/advanced-usage.md#caching-packages-dependencies)
5. [Using multiple operating systems and architectures](docs/advanced-usage.md#multiple-operating-systems-and-architectures)
6. [Publishing to npmjs and GPR with npm](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-npm)
7. [Publishing to npmjs and GPR with yarn](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-yarn)
8. [Using private packages](docs/advanced-usage.md#use-private-packages)

# License

Expand Down
1 change: 1 addition & 0 deletions __tests__/data/.nvmrc
@@ -0,0 +1 @@
v14
55 changes: 53 additions & 2 deletions __tests__/installer.test.ts
@@ -1,12 +1,12 @@
import * as core from '@actions/core';
import * as io from '@actions/io';
import * as tc from '@actions/tool-cache';
import * as im from '../src/installer';
import fs from 'fs';
import cp from 'child_process';
import osm = require('os');
import path from 'path';
import * as main from '../src/main';
import * as im from '../src/installer';
import * as auth from '../src/authutil';

let nodeTestManifest = require('./data/versions-manifest.json');
Expand All @@ -31,9 +31,11 @@ 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;
let parseNodeVersionSpy: jest.SpyInstance;

beforeEach(() => {
// @actions/core
Expand All @@ -58,10 +60,12 @@ describe('setup-node', () => {
cacheSpy = jest.spyOn(tc, 'cacheDir');
getManifestSpy = jest.spyOn(tc, 'getManifestFromRepo');
getDistSpy = jest.spyOn(im, 'getVersionsFromDist');
parseNodeVersionSpy = jest.spyOn(im, 'parseNodeVersionFile');

// 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
Expand Down Expand Up @@ -101,6 +105,7 @@ describe('setup-node', () => {

afterAll(async () => {
console.log('::stoptoken::'); // Re-enable executing of runner commands when running tests in actions
jest.restoreAllMocks();
}, 100000);

//--------------------------------------------------
Expand Down Expand Up @@ -343,7 +348,7 @@ describe('setup-node', () => {
expect(cnSpy).toHaveBeenCalledWith(`::error::${errMsg}${osm.EOL}`);
});

it('Acquires specified architecture of node', async () => {
it('acquires specified architecture of node', async () => {
for (const {arch, version, osSpec} of [
{arch: 'x86', version: '12.16.2', osSpec: 'win32'},
{arch: 'x86', version: '14.0.0', osSpec: 'win32'}
Expand Down Expand Up @@ -549,6 +554,52 @@ describe('setup-node', () => {
});
});

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';
const expectedVersionSpec = '12';
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, '..');
inputs['node-version-file'] = versionFile;

readFileSyncSpy.mockImplementation(() => versionSpec);
parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec);

// Act
await main.run();

// Assert
expect(readFileSyncSpy).toHaveBeenCalledTimes(1);
expect(readFileSyncSpy).toHaveBeenCalledWith(
path.join(process.env.GITHUB_WORKSPACE, versionFile),
'utf8'
);
expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec);
expect(logSpy).toHaveBeenCalledWith(
`Resolved ${versionFile} as ${expectedVersionSpec}`
);
});
});
describe('LTS version', () => {
beforeEach(() => {
os.platform = 'linux';
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Expand Up @@ -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'
hkaur008 marked this conversation as resolved.
Show resolved Hide resolved
architecture:
description: 'Target architecture for Node to use. Examples: x86, x64. Will use system architecture by default.'
check-latest:
Expand Down
42 changes: 32 additions & 10 deletions dist/setup/index.js
Expand Up @@ -6849,25 +6849,26 @@ var __importStar = (this && this.__importStar) || function (mod) {
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470));
const installer = __importStar(__webpack_require__(923));
const fs_1 = __importDefault(__webpack_require__(747));
const auth = __importStar(__webpack_require__(749));
const path = __importStar(__webpack_require__(622));
const cache_restore_1 = __webpack_require__(409);
const url_1 = __webpack_require__(835);
const os = __webpack_require__(87);
const os_1 = __importDefault(__webpack_require__(87));
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 = resolveVersionInput();
let arch = core.getInput('architecture');
const cache = core.getInput('cache');
// if architecture supplied but node-version is not
Expand All @@ -6876,7 +6877,7 @@ function run() {
core.warning('`architecture` is provided but `node-version` is missing. In this configuration, the version/architecture of Node will not be changed. To fix this, provide `architecture` in combination with `node-version`');
}
if (!arch) {
arch = os.arch();
arch = os_1.default.arch();
}
if (version) {
let token = core.getInput('token');
Expand All @@ -6902,8 +6903,8 @@ function run() {
core.info(`##[add-matcher]${path.join(matchersPath, 'eslint-stylish.json')}`);
core.info(`##[add-matcher]${path.join(matchersPath, 'eslint-compact.json')}`);
}
catch (error) {
core.setFailed(error.message);
catch (err) {
core.setFailed(err.message);
}
});
}
Expand All @@ -6912,6 +6913,19 @@ function isGhes() {
const ghUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
function resolveVersionInput() {
let version = core.getInput('node-version') || core.getInput('version');
if (version) {
return version;
}
const versionFileInput = core.getInput('node-version-file');
if (versionFileInput) {
const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFileInput);
version = installer.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
core.info(`Resolved ${versionFileInput} as ${version}`);
}
return version;
}


/***/ }),
Expand Down Expand Up @@ -65036,7 +65050,7 @@ exports.NOOP_TEXT_MAP_PROPAGATOR = new NoopTextMapPropagator();
/* 921 */,
/* 922 */,
/* 923 */
/***/ (function(module, exports, __webpack_require__) {
/***/ (function(__unusedmodule, exports, __webpack_require__) {

"use strict";

Expand Down Expand Up @@ -65313,7 +65327,7 @@ function queryDistForMatch(versionSpec, arch = os.arch()) {
throw new Error(`Unexpected OS '${osPlat}'`);
}
let versions = [];
let nodeVersions = yield module.exports.getVersionsFromDist();
let nodeVersions = yield getVersionsFromDist();
nodeVersions.forEach((nodeVersion) => {
// ensure this version supports your os and platform
if (nodeVersion.files.indexOf(dataFileName) >= 0) {
Expand Down Expand Up @@ -65401,6 +65415,14 @@ function translateArchToDistUrl(arch) {
return arch;
}
}
function parseNodeVersionFile(contents) {
let nodeVersion = contents.trim();
if (/^v\d/.test(nodeVersion)) {
nodeVersion = nodeVersion.substring(1);
}
return nodeVersion;
}
exports.parseNodeVersionFile = parseNodeVersionFile;


/***/ }),
Expand Down
17 changes: 17 additions & 0 deletions docs/advanced-usage.md
Expand Up @@ -19,6 +19,23 @@ steps:
- run: npm test
```

## Node version file

The action will search for the node version file relative to repository root.
The `node-version-file` input contains the version of node used by project, for example `.nvmrc`. If both the `node-version` and the `node-version-file` inputs are provided the `node-version` input is used.
Xlient marked this conversation as resolved.
Show resolved Hide resolved
You can check [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax)
> The node version file is read from the project root
hkaur008 marked this conversation as resolved.
Show resolved Hide resolved

```yaml
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version-file: '.nvmrc'
- run: npm install
- run: npm test
```

## Architecture

You can use any of the [supported operating systems](https://docs.github.com/en/actions/reference/virtual-environments-for-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
11 changes: 10 additions & 1 deletion src/installer.ts
Expand Up @@ -371,7 +371,7 @@ async function queryDistForMatch(
}

let versions: string[] = [];
let nodeVersions = await module.exports.getVersionsFromDist();
let nodeVersions = await getVersionsFromDist();

nodeVersions.forEach((nodeVersion: INodeVersion) => {
// ensure this version supports your os and platform
Expand Down Expand Up @@ -464,3 +464,12 @@ function translateArchToDistUrl(arch: string): string {
return arch;
}
}

export function parseNodeVersionFile(contents: string): string {
let nodeVersion = contents.trim();

if (/^v\d/.test(nodeVersion)) {
nodeVersion = nodeVersion.substring(1);
}
return nodeVersion;
}
33 changes: 26 additions & 7 deletions src/main.ts
@@ -1,21 +1,19 @@
import * as core from '@actions/core';
import * as installer from './installer';
import fs from 'fs';
import * as auth from './authutil';
import * as path from 'path';
import {restoreCache} from './cache-restore';
import {URL} from 'url';
import os = require('os');
hkaur008 marked this conversation as resolved.
Show resolved Hide resolved
import os from 'os';

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 = resolveVersionInput();

let arch = core.getInput('architecture');
const cache = core.getInput('cache');
Expand Down Expand Up @@ -63,8 +61,8 @@ export async function run() {
core.info(
`##[add-matcher]${path.join(matchersPath, 'eslint-compact.json')}`
);
} catch (error) {
core.setFailed(error.message);
} catch (err) {
hkaur008 marked this conversation as resolved.
Show resolved Hide resolved
core.setFailed(err.message);
}
}

Expand All @@ -74,3 +72,24 @@ function isGhes(): boolean {
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}

function resolveVersionInput(): string {
let version = core.getInput('node-version') || core.getInput('version');
if (version) {
return version;
}

const versionFileInput = core.getInput('node-version-file');
if (versionFileInput) {
hkaur008 marked this conversation as resolved.
Show resolved Hide resolved
const versionFilePath = path.join(
process.env.GITHUB_WORKSPACE!,
versionFileInput
);
version = installer.parseNodeVersionFile(
fs.readFileSync(versionFilePath, 'utf8')
);
core.info(`Resolved ${versionFileInput} as ${version}`);
}

return version;
}