@@ -2,9 +2,8 @@ import { defaultExecOptions, execRoot, execRootSync } from "admina"
22import { GITHUB_ACTIONS } from "ci-info"
33import { info , warning } from "ci-log"
44import escapeRegex from "escape-string-regexp"
5- import { type ExecaError , type SyncOptions , execa } from "execa"
5+ import { type ExecaError , execa } from "execa"
66import { appendFile } from "fs/promises"
7- import memoize from "micro-memoize"
87import { sourceRC } from "os-env"
98import { pathExists } from "path-exists"
109import which from "which"
@@ -62,16 +61,15 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
6261
6362 // Install
6463 try {
65- execRootSync ( apt , [ "install" , "--fix-broken" , "-y" , ...needToInstall ] , getAptExecOptions ( { apt } ) )
64+ execRootSync ( apt , [ "install" , "--fix-broken" , "-y" , ...needToInstall ] , { ... defaultExecOptions , env : getEnv ( apt ) } )
6665 } catch ( err ) {
67- if ( "stderr" in ( err as ExecaError ) ) {
68- const stderr = ( err as ExecaError ) . stderr
69- if ( retryErrors . some ( ( error ) => stderr . includes ( error ) ) ) {
66+ if ( isExecaError ( err ) ) {
67+ if ( retryErrors . some ( ( error ) => err . stderr . includes ( error ) ) ) {
7068 warning ( `Failed to install packages ${ needToInstall } . Retrying...` )
7169 execRootSync (
7270 apt ,
7371 [ "install" , "--fix-broken" , "-y" , "-o" , aptTimeout , ...needToInstall ] ,
74- getAptExecOptions ( { apt } ) ,
72+ { ... defaultExecOptions , env : getEnv ( apt ) } ,
7573 )
7674 }
7775 } else {
@@ -82,6 +80,10 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
8280 return { binDir : "/usr/bin/" }
8381}
8482
83+ function isExecaError ( err : unknown ) : err is ExecaError {
84+ return typeof ( err as ExecaError ) . stderr === "string"
85+ }
86+
8587export function hasNala ( ) {
8688 return which . sync ( "nala" , { nothrow : true } ) !== null
8789}
@@ -112,18 +114,6 @@ function getEnv(apt: string) {
112114 return env
113115}
114116
115- function getAptExecOptionsRaw ( givenOpts : { apt ?: string ; pipe ?: boolean } = { } ) : SyncOptions {
116- const opts = {
117- apt : "apt-get" ,
118- pipe : false ,
119- ...givenOpts ,
120- }
121-
122- return { env : getEnv ( opts . apt ) , ...defaultExecOptions , stdio : opts . pipe ? "pipe" : "inherit" }
123- }
124-
125- const getAptExecOptions = memoize ( getAptExecOptionsRaw )
126-
127117export enum AptPackageType {
128118 NameDashVersion = 0 ,
129119 NameEqualsVersion = 1 ,
@@ -156,7 +146,7 @@ async function addRepositories(apt: string, packages: AptPackage[]) {
156146 await installAddAptRepo ( apt )
157147 for ( const repo of allRepositories ) {
158148 // eslint-disable-next-line no-await-in-loop
159- execRootSync ( "add-apt-repository" , [ "-y" , "--no-update" , repo ] , getAptExecOptions ( ) )
149+ execRootSync ( "add-apt-repository" , [ "-y" , "--no-update" , repo ] , { ... defaultExecOptions , env : getEnv ( apt ) } )
160150 }
161151 updateRepos ( apt )
162152 didUpdate = true
@@ -169,15 +159,15 @@ async function aptPackageType(apt: string, name: string, version: string | undef
169159 "search" ,
170160 "--names-only" ,
171161 `^${ escapeRegex ( name ) } -${ escapeRegex ( version ) } $` ,
172- ] , getAptExecOptions ( { apt, pipe : true } ) )
162+ ] , { env : getEnv ( apt ) , stdio : "pipe" } )
173163 if ( stdout . trim ( ) !== "" ) {
174164 return AptPackageType . NameDashVersion
175165 }
176166
177167 try {
178168 // check if apt-get show can find the version
179169 // eslint-disable-next-line @typescript-eslint/no-shadow
180- const { stdout } = await execa ( "apt-cache" , [ "show" , `${ name } =${ version } ` ] , getAptExecOptions ( ) )
170+ const { stdout } = await execa ( "apt-cache" , [ "show" , `${ name } =${ version } ` ] , { env : getEnv ( apt ) } )
181171 if ( stdout . trim ( ) === "" ) {
182172 return AptPackageType . NameEqualsVersion
183173 }
@@ -187,7 +177,7 @@ async function aptPackageType(apt: string, name: string, version: string | undef
187177 }
188178
189179 try {
190- const { stdout : showStdout } = await execa ( "apt-cache" , [ "show" , name ] , getAptExecOptions ( { pipe : true } ) )
180+ const { stdout : showStdout } = await execa ( "apt-cache" , [ "show" , name ] , { env : getEnv ( apt ) , stdio : "pipe" } )
191181 if ( showStdout . trim ( ) !== "" ) {
192182 return AptPackageType . Name
193183 }
@@ -213,8 +203,8 @@ async function getAptArg(apt: string, name: string, version: string | undefined)
213203 case AptPackageType . NameEqualsVersion :
214204 return `${ name } =${ version } `
215205 case AptPackageType . Name :
216- if ( version !== undefined ) {
217- warning ( `Could not find package ${ name } ${ version } . Installing the latest version.` )
206+ if ( version !== undefined && version !== "" ) {
207+ warning ( `Could not find package ${ name } with version ${ version } . Installing the latest version.` )
218208 }
219209 return name
220210 default :
@@ -226,7 +216,7 @@ function updateRepos(apt: string) {
226216 execRootSync (
227217 apt ,
228218 apt !== "nala" ? [ "update" , "-y" , "-o" , aptTimeout ] : [ "update" , "-o" , aptTimeout ] ,
229- getAptExecOptions ( { apt } ) ,
219+ { ... defaultExecOptions , env : getEnv ( apt ) } ,
230220 )
231221}
232222
@@ -237,7 +227,7 @@ async function installAddAptRepo(apt: string) {
237227 execRootSync (
238228 apt ,
239229 [ "install" , "-y" , "--fix-broken" , "-o" , aptTimeout , "software-properties-common" ] ,
240- getAptExecOptions ( { apt } ) ,
230+ { ... defaultExecOptions , env : getEnv ( apt ) } ,
241231 )
242232}
243233
@@ -256,7 +246,10 @@ async function initApt(apt: string) {
256246 ] )
257247
258248 if ( toInstall . length !== 0 ) {
259- execRootSync ( apt , [ "install" , "-y" , "--fix-broken" , "-o" , aptTimeout , ...toInstall ] , getAptExecOptions ( { apt } ) )
249+ execRootSync ( apt , [ "install" , "-y" , "--fix-broken" , "-o" , aptTimeout , ...toInstall ] , {
250+ ...defaultExecOptions ,
251+ env : getEnv ( apt ) ,
252+ } )
260253 }
261254
262255 const promises : Promise < string | void > [ ] = [
@@ -325,7 +318,7 @@ export async function updateAptAlternatives(name: string, path: string, rcPath:
325318export async function isPackageInstalled ( pack : string ) {
326319 try {
327320 // check if a package is installed
328- const { stdout } = await execa ( "dpkg" , [ "-s" , pack ] , getAptExecOptions ( { pipe : true } ) )
321+ const { stdout } = await execa ( "dpkg" , [ "-s" , pack ] , { env : getEnv ( "apt-get" ) , stdio : "pipe" } )
329322 if ( typeof stdout !== "string" ) {
330323 return false
331324 }
@@ -340,7 +333,7 @@ export async function isPackageInstalled(pack: string) {
340333export async function isPackageRegexInstalled ( regexp : string ) {
341334 try {
342335 // check if a package matching the regexp is installed
343- const { stdout } = await execa ( "dpkg" , [ "-l" , regexp ] , getAptExecOptions ( { pipe : true } ) )
336+ const { stdout } = await execa ( "dpkg" , [ "-l" , regexp ] , { env : getEnv ( "apt-get" ) , stdio : "pipe" } )
344337 if ( typeof stdout !== "string" ) {
345338 return false
346339 }
0 commit comments