@actions/tool-cache: convert to an ESM module (#2274)

* `@actions/tool-cache`: convert to an ESM module

* Fix jest config

* Downgrade `nock` since it's conflicting with `@actions/attest`'s version
This commit is contained in:
Daniel Kennedy
2026-01-29 11:26:14 -05:00
committed by GitHub
parent 7a0147b5c6
commit 9d912b1840
10 changed files with 99 additions and 85 deletions
+22 -18
View File
@@ -1,12 +1,25 @@
import * as semver from 'semver'
import {debug} from '@actions/core'
import * as os from 'os'
import * as cp from 'child_process'
import * as fs from 'fs'
// needs to be require for core node modules to be mocked
/* eslint @typescript-eslint/no-require-imports: 0 */
// Internal object for testability (allows mocking in ESM)
export const _internal = {
readLinuxVersionFile(): string {
const lsbReleaseFile = '/etc/lsb-release'
const osReleaseFile = '/etc/os-release'
let contents = ''
import os = require('os')
import cp = require('child_process')
import fs = require('fs')
if (fs.existsSync(lsbReleaseFile)) {
contents = fs.readFileSync(lsbReleaseFile).toString()
} else if (fs.existsSync(osReleaseFile)) {
contents = fs.readFileSync(osReleaseFile).toString()
}
return contents
}
}
/*
NOTE: versions must be sorted descending by version in the manifest
@@ -86,7 +99,7 @@ export async function _findMatch(
let chk = item.arch === archFilter && item.platform === platFilter
if (chk && item.platform_version) {
const osVersion = module.exports._getOsVersion()
const osVersion = _getOsVersion()
if (osVersion === item.platform_version) {
chk = true
@@ -130,7 +143,7 @@ export function _getOsVersion(): string {
// DISTRIB_RELEASE=18.04
// DISTRIB_CODENAME=bionic
// DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"
const lsbContents = module.exports._readLinuxVersionFile()
const lsbContents = _internal.readLinuxVersionFile()
if (lsbContents) {
const lines = lsbContents.split('\n')
for (const line of lines) {
@@ -150,16 +163,7 @@ export function _getOsVersion(): string {
return version
}
// Alias for backwards compatibility
export function _readLinuxVersionFile(): string {
const lsbReleaseFile = '/etc/lsb-release'
const osReleaseFile = '/etc/os-release'
let contents = ''
if (fs.existsSync(lsbReleaseFile)) {
contents = fs.readFileSync(lsbReleaseFile).toString()
} else if (fs.existsSync(osReleaseFile)) {
contents = fs.readFileSync(osReleaseFile).toString()
}
return contents
return _internal.readLinuxVersionFile()
}
+3 -4
View File
@@ -2,7 +2,7 @@ import * as core from '@actions/core'
import * as io from '@actions/io'
import * as crypto from 'crypto'
import * as fs from 'fs'
import * as mm from './manifest'
import * as mm from './manifest.js'
import * as os from 'os'
import * as path from 'path'
import * as httpm from '@actions/http-client'
@@ -11,9 +11,8 @@ import * as stream from 'stream'
import * as util from 'util'
import {ok} from 'assert'
import {OutgoingHttpHeaders} from 'http'
import {exec} from '@actions/exec/lib/exec'
import {ExecOptions} from '@actions/exec/lib/interfaces'
import {RetryHelper} from './retry-helper'
import {exec, ExecOptions} from '@actions/exec'
import {RetryHelper} from './retry-helper.js'
export class HTTPError extends Error {
constructor(readonly httpStatusCode: number | undefined) {