fixed merge conflicts

This commit is contained in:
La'Kaleigh Harris 2021-10-04 15:14:32 +00:00 committed by GitHub
commit 25794099fc
8 changed files with 85 additions and 96 deletions

View file

@ -0,0 +1,33 @@
name: version-file-test
on:
push:
branches: [ add-node-version-file-support ]
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
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: ./
with:
node-version-file: '.nvmrc'
- run: npm ci
- run: npm run build

1
.nvmrc Normal file
View file

@ -0,0 +1 @@
lts/*

View file

@ -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>

View file

@ -6,10 +6,7 @@ import cp from 'child_process';
import osm = require('os');
import path from 'path';
import * as main from '../src/main';
import * as nv from '../src/node-version';
import * as nvf from '../src/node-version-file';
import * as auth from '../src/authutil';
let nodeTestManifest = require('./data/versions-manifest.json');
let nodeTestDist = require('./data/node-dist-index.json');
@ -597,9 +594,7 @@ describe('setup-node', () => {
expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec);
expect(logSpy).toHaveBeenCalledWith(
`Resolved ${versionFile} as ${expectedVersionSpec}`);
});
});
});
describe('LTS version', () => {
beforeEach(() => {
os.platform = 'linux';
@ -819,3 +814,4 @@ describe('setup-node', () => {
});
});
});
});

View file

@ -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<INodeVersion[]> {
let dataUrl = 'https://nodejs.org/dist/index.json';
let httpClient = new hc.HttpClient('setup-node', [], {
allowRetries: true,
maxRetries: 3
});
let response = await httpClient.getJson<INodeVersion[]>(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<string> {
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;
}

View file

@ -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,7 +23,7 @@ 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}`);
@ -88,3 +87,4 @@ function isGhes(): boolean {
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}

View file

@ -1,65 +0,0 @@
import * as semvar from 'semver';
import {INodeVersion, getVersionsFromDist} from './node-version';
export async function parseNodeVersionFile(contents: string): Promise<string> {
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;
}

View file

@ -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<INodeVersion[]> {
let dataUrl = 'https://nodejs.org/dist/index.json';
let httpClient = new hc.HttpClient('setup-node', [], {
allowRetries: true,
maxRetries: 3
});
let response = await httpClient.getJson<INodeVersion[]>(dataUrl);
return response.result || [];
}