async function queryLatestMatch()

in sample.ts [126:168]


async function queryLatestMatch(versionSpec: string): Promise<string> {
    //
    // Hopefully your tool supports an easy way to get a version list.
    // Node offers a json list of versions.
    //
    let dataFileName: string;
    switch (osPlat) {
        case "linux": dataFileName = "linux-" + osArch; break;
        case "darwin": dataFileName = "osx-" + osArch + '-tar'; break;
        case "win32": dataFileName = "win-" + osArch + '-exe'; break;
        default: throw new Error(`Unexpected OS '${osPlat}'`);
    }

    let versions: string[] = [];
    let dataUrl = "https://nodejs.org/dist/index.json";
    let rest: restm.RestClient = new restm.RestClient('vsts-node-tool');
    let nodeVersions: INodeVersion[] = (await rest.get<INodeVersion[]>(dataUrl)).result;
    nodeVersions.forEach((nodeVersion:INodeVersion) => {
        //
        // Ensure this version supports your os and platform.
        //
        if (nodeVersion.files.indexOf(dataFileName) >= 0) {
            versions.push(nodeVersion.version);
        }
    });

    //
    // If there is no data driven way to get versions supported,
    // a last option is to tool.scrape() with a regex.
    //
    // For example:
    //      let scrapeUrl = 'https://nodejs.org/dist/';
    //      let re: RegExp = /v(\d+\.)(\d+\.)(\d+)/g;
    //      versions = await toolLib.scrape(scrapeUrl, re);
    //

    //
    // Get the latest version that matches the version spec.
    //
    let version: string = toolLib.evaluateVersions(versions, versionSpec);

    return version;
}