From 82441b3f8251ee6ff9d9a76fb590e86ee891411f Mon Sep 17 00:00:00 2001 From: La'Kaleigh Harris <35268101+Xlient@users.noreply.github.com> Date: Fri, 1 Oct 2021 19:16:00 +0000 Subject: [PATCH 1/5] Refactored code and removed redundant logic --- src/installer.ts | 48 ++++++++++++++++++++++++++++- src/main.ts | 5 ++-- src/node-version-file.ts | 65 ---------------------------------------- src/node-version.ts | 21 ------------- 4 files changed, 50 insertions(+), 89 deletions(-) delete mode 100644 src/node-version-file.ts delete mode 100644 src/node-version.ts diff --git a/src/installer.ts b/src/installer.ts index 7f32e907..2f1a8ffb 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -2,11 +2,18 @@ import os = require('os'); import * as assert from 'assert'; import * as core from '@actions/core'; import * as io from '@actions/io'; +import * as hc from '@actions/http-client'; import * as tc from '@actions/tool-cache'; import * as path from 'path'; import * as semver from 'semver'; import fs = require('fs'); -import {INodeVersion, getVersionsFromDist} from './node-version'; + + + +interface INodeVersion { + version: string; + files: string[]; +} interface INodeVersionInfo { downloadUrl: string; @@ -376,6 +383,16 @@ async function queryDistForMatch( return version; } +async function getVersionsFromDist(): Promise { + let dataUrl = 'https://nodejs.org/dist/index.json'; + let httpClient = new hc.HttpClient('setup-node', [], { + allowRetries: true, + maxRetries: 3 + }); + let response = await httpClient.getJson(dataUrl); + return response.result || []; +} + // For non LTS versions of Node, the files we need (for Windows) are sometimes located // in a different folder than they normally are for other versions. // Normally the format is similar to: https://nodejs.org/dist/v5.10.1/node-v5.10.1-win-x64.7z @@ -445,3 +462,32 @@ function translateArchToDistUrl(arch: string): string { return arch; } } + +export async function parseNodeVersionFile(contents: string): Promise { + contents = contents.trim(); + + if (/^v\d/.test(contents)) { + contents = contents.substring(1); + } + + const nodeVersions = await getVersionsFromDist(); + + let nodeVersion: string; + + if (semver.valid(contents) || isPartialMatch(contents)) { + nodeVersion = contents; + } else { + throw new Error(`Couldn't resolve node version: '${contents}'`); + } + + return stripVPrefix(nodeVersion); +} + +function isPartialMatch(version: string): boolean { + return /^\d+(\.\d+(\.\d+)?)?$/.test(version); +} + +function stripVPrefix(version: string): string { + return /^v\d/.test(version) ? version.substring(1) : version; +} + diff --git a/src/main.ts b/src/main.ts index 76d7f5e0..9d05c6ec 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,7 +5,6 @@ import fs = require('fs'); import * as path from 'path'; import {restoreCache} from './cache-restore'; import {URL} from 'url'; -import {parseNodeVersionFile} from './node-version-file'; import os = require('os'); export async function run() { @@ -24,12 +23,13 @@ export async function run() { if (!!versionFile) { const versionFilePath = path.join(__dirname, '..', versionFile); - version = await parseNodeVersionFile( + version = await installer.parseNodeVersionFile( fs.readFileSync(versionFilePath, 'utf8') ); core.info(`Resolved ${versionFile} as ${version}`); } } + let arch = core.getInput('architecture'); const cache = core.getInput('cache'); @@ -87,3 +87,4 @@ function isGhes(): boolean { ); return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; } + diff --git a/src/node-version-file.ts b/src/node-version-file.ts deleted file mode 100644 index d2d3568b..00000000 --- a/src/node-version-file.ts +++ /dev/null @@ -1,65 +0,0 @@ -import * as semvar from 'semver'; -import {INodeVersion, getVersionsFromDist} from './node-version'; - -export async function parseNodeVersionFile(contents: string): Promise { - contents = contents.trim(); - - if (/^v\d/.test(contents)) { - contents = contents.substring(1); - } - - const nodeVersions = await getVersionsFromDist(); - - let nodeVersion: string; - - if (contents.startsWith('lts/')) { - nodeVersion = findLatestLts(nodeVersions, contents).version; - } else if (semvar.valid(contents) || isPartialMatch(contents)) { - nodeVersion = contents; - } else { - throw new Error(`Couldn't resolve node version: '${contents}'`); - } - - return stripVPrefix(nodeVersion); -} - -function findLatestLts( - nodeVersions: INodeVersion[], - codename: string -): INodeVersion { - let nodeVersion: INodeVersion | undefined; - - if (codename === 'lts/*') { - nodeVersion = nodeVersions.reduce((latest, nodeVersion) => { - if (!nodeVersion.lts) { - return latest; - } - - return semvar.gt(nodeVersion.version, latest.version) - ? nodeVersion - : latest; - }); - } else { - codename = codename.replace('lts/', '').toLowerCase(); - - nodeVersion = nodeVersions.find( - nodeVersion => `${nodeVersion.lts}`.toLowerCase() === codename - ); - } - - if (!nodeVersion) { - throw new Error( - `Couldn't find matching release for codename: '${codename}'` - ); - } - - return nodeVersion; -} - -function isPartialMatch(version: string): boolean { - return /^\d+(\.\d+(\.\d+)?)?$/.test(version); -} - -function stripVPrefix(version: string): string { - return /^v\d/.test(version) ? version.substring(1) : version; -} diff --git a/src/node-version.ts b/src/node-version.ts deleted file mode 100644 index eb49b049..00000000 --- a/src/node-version.ts +++ /dev/null @@ -1,21 +0,0 @@ -import * as hc from '@actions/http-client'; - -// -// Node versions interface -// see https://nodejs.org/dist/index.json -// -export interface INodeVersion { - version: string; - files: string[]; - lts: boolean | string; -} - -export async function getVersionsFromDist(): Promise { - let dataUrl = 'https://nodejs.org/dist/index.json'; - let httpClient = new hc.HttpClient('setup-node', [], { - allowRetries: true, - maxRetries: 3 - }); - let response = await httpClient.getJson(dataUrl); - return response.result || []; -} From 052bc8b6a3c7dc3762cc73df734ee0e34e66489b Mon Sep 17 00:00:00 2001 From: La'Kaleigh Harris <35268101+Xlient@users.noreply.github.com> Date: Fri, 1 Oct 2021 20:02:10 +0000 Subject: [PATCH 2/5] created test workflow & nvmrc file --- .github/workflows/version-file-workflow.yml | 33 +++++++++++++++++++++ .nvmrc | 1 + 2 files changed, 34 insertions(+) create mode 100644 .github/workflows/version-file-workflow.yml create mode 100644 .nvmrc diff --git a/.github/workflows/version-file-workflow.yml b/.github/workflows/version-file-workflow.yml new file mode 100644 index 00000000..102c1db7 --- /dev/null +++ b/.github/workflows/version-file-workflow.yml @@ -0,0 +1,33 @@ + + +name: version-file-test + + +on: + + push: + branches: [ add-node-version-file-support ] + pull_request: + branches: [ add-node-version-file-support ] + + + workflow_dispatch: + + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + + + steps: + + - uses: actions/checkout@v2 + - name: Setup node test + uses: actions/setup-node@v2 + with: + node-version-file: '.nvmrc' + - run: npm ci + - run: npm run build diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 00000000..e23e7f3a --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +v12.4.0 From 7f7335c2af83cd5a4962b6d36da37165478dd4a3 Mon Sep 17 00:00:00 2001 From: La'Kaleigh Harris <35268101+Xlient@users.noreply.github.com> Date: Mon, 4 Oct 2021 13:01:44 +0000 Subject: [PATCH 3/5] updated nvmrc & added ignore paths --- .github/workflows/version-file-workflow.yml | 8 ++++---- .nvmrc | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/version-file-workflow.yml b/.github/workflows/version-file-workflow.yml index 102c1db7..3ccc6a4b 100644 --- a/.github/workflows/version-file-workflow.yml +++ b/.github/workflows/version-file-workflow.yml @@ -7,8 +7,11 @@ on: push: branches: [ add-node-version-file-support ] + paths-ignore: + - '**.md' pull_request: - branches: [ add-node-version-file-support ] + paths-ignore: + - '**.md' workflow_dispatch: @@ -20,10 +23,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] - - steps: - - uses: actions/checkout@v2 - name: Setup node test uses: actions/setup-node@v2 diff --git a/.nvmrc b/.nvmrc index e23e7f3a..b009dfb9 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -v12.4.0 +lts/* From 1f26500ee68e8366e5d24071258c2aa6a1e0ae29 Mon Sep 17 00:00:00 2001 From: La'Kaleigh Harris <35268101+Xlient@users.noreply.github.com> Date: Mon, 4 Oct 2021 13:05:35 +0000 Subject: [PATCH 4/5] fixed error in workflow file --- .github/workflows/version-file-workflow.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/version-file-workflow.yml b/.github/workflows/version-file-workflow.yml index 3ccc6a4b..3f20f67a 100644 --- a/.github/workflows/version-file-workflow.yml +++ b/.github/workflows/version-file-workflow.yml @@ -7,7 +7,7 @@ on: push: branches: [ add-node-version-file-support ] - paths-ignore: + paths-ignore: - '**.md' pull_request: paths-ignore: From 510ffc93e95d84e358b0c224a206be4221fc4bd4 Mon Sep 17 00:00:00 2001 From: La'Kaleigh Harris <35268101+Xlient@users.noreply.github.com> Date: Mon, 4 Oct 2021 09:39:19 -0400 Subject: [PATCH 5/5] Update version-file-workflow.yml --- .github/workflows/version-file-workflow.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/version-file-workflow.yml b/.github/workflows/version-file-workflow.yml index 3f20f67a..7d00abad 100644 --- a/.github/workflows/version-file-workflow.yml +++ b/.github/workflows/version-file-workflow.yml @@ -26,7 +26,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Setup node test - uses: actions/setup-node@v2 + uses: ./ with: node-version-file: '.nvmrc' - run: npm ci