@@ -2,9 +2,8 @@ import { defaultExecOptions, execRoot, execRootSync } from "admina"
2
2
import { GITHUB_ACTIONS } from "ci-info"
3
3
import { info , warning } from "ci-log"
4
4
import escapeRegex from "escape-string-regexp"
5
- import { type ExecaError , type SyncOptions , execa } from "execa"
5
+ import { type ExecaError , execa } from "execa"
6
6
import { appendFile } from "fs/promises"
7
- import memoize from "micro-memoize"
8
7
import { sourceRC } from "os-env"
9
8
import { pathExists } from "path-exists"
10
9
import which from "which"
@@ -62,16 +61,15 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
62
61
63
62
// Install
64
63
try {
65
- execRootSync ( apt , [ "install" , "--fix-broken" , "-y" , ...needToInstall ] , getAptExecOptions ( { apt } ) )
64
+ execRootSync ( apt , [ "install" , "--fix-broken" , "-y" , ...needToInstall ] , { ... defaultExecOptions , env : getEnv ( apt ) } )
66
65
} 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 ) ) ) {
70
68
warning ( `Failed to install packages ${ needToInstall } . Retrying...` )
71
69
execRootSync (
72
70
apt ,
73
71
[ "install" , "--fix-broken" , "-y" , "-o" , aptTimeout , ...needToInstall ] ,
74
- getAptExecOptions ( { apt } ) ,
72
+ { ... defaultExecOptions , env : getEnv ( apt ) } ,
75
73
)
76
74
}
77
75
} else {
@@ -82,6 +80,10 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
82
80
return { binDir : "/usr/bin/" }
83
81
}
84
82
83
+ function isExecaError ( err : unknown ) : err is ExecaError {
84
+ return typeof ( err as ExecaError ) . stderr === "string"
85
+ }
86
+
85
87
export function hasNala ( ) {
86
88
return which . sync ( "nala" , { nothrow : true } ) !== null
87
89
}
@@ -112,18 +114,6 @@ function getEnv(apt: string) {
112
114
return env
113
115
}
114
116
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
-
127
117
export enum AptPackageType {
128
118
NameDashVersion = 0 ,
129
119
NameEqualsVersion = 1 ,
@@ -156,7 +146,7 @@ async function addRepositories(apt: string, packages: AptPackage[]) {
156
146
await installAddAptRepo ( apt )
157
147
for ( const repo of allRepositories ) {
158
148
// 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 ) } )
160
150
}
161
151
updateRepos ( apt )
162
152
didUpdate = true
@@ -169,15 +159,15 @@ async function aptPackageType(apt: string, name: string, version: string | undef
169
159
"search" ,
170
160
"--names-only" ,
171
161
`^${ escapeRegex ( name ) } -${ escapeRegex ( version ) } $` ,
172
- ] , getAptExecOptions ( { apt, pipe : true } ) )
162
+ ] , { env : getEnv ( apt ) , stdio : "pipe" } )
173
163
if ( stdout . trim ( ) !== "" ) {
174
164
return AptPackageType . NameDashVersion
175
165
}
176
166
177
167
try {
178
168
// check if apt-get show can find the version
179
169
// 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 ) } )
181
171
if ( stdout . trim ( ) === "" ) {
182
172
return AptPackageType . NameEqualsVersion
183
173
}
@@ -187,7 +177,7 @@ async function aptPackageType(apt: string, name: string, version: string | undef
187
177
}
188
178
189
179
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" } )
191
181
if ( showStdout . trim ( ) !== "" ) {
192
182
return AptPackageType . Name
193
183
}
@@ -213,8 +203,8 @@ async function getAptArg(apt: string, name: string, version: string | undefined)
213
203
case AptPackageType . NameEqualsVersion :
214
204
return `${ name } =${ version } `
215
205
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.` )
218
208
}
219
209
return name
220
210
default :
@@ -226,7 +216,7 @@ function updateRepos(apt: string) {
226
216
execRootSync (
227
217
apt ,
228
218
apt !== "nala" ? [ "update" , "-y" , "-o" , aptTimeout ] : [ "update" , "-o" , aptTimeout ] ,
229
- getAptExecOptions ( { apt } ) ,
219
+ { ... defaultExecOptions , env : getEnv ( apt ) } ,
230
220
)
231
221
}
232
222
@@ -237,7 +227,7 @@ async function installAddAptRepo(apt: string) {
237
227
execRootSync (
238
228
apt ,
239
229
[ "install" , "-y" , "--fix-broken" , "-o" , aptTimeout , "software-properties-common" ] ,
240
- getAptExecOptions ( { apt } ) ,
230
+ { ... defaultExecOptions , env : getEnv ( apt ) } ,
241
231
)
242
232
}
243
233
@@ -256,7 +246,10 @@ async function initApt(apt: string) {
256
246
] )
257
247
258
248
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
+ } )
260
253
}
261
254
262
255
const promises : Promise < string | void > [ ] = [
@@ -325,7 +318,7 @@ export async function updateAptAlternatives(name: string, path: string, rcPath:
325
318
export async function isPackageInstalled ( pack : string ) {
326
319
try {
327
320
// 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" } )
329
322
if ( typeof stdout !== "string" ) {
330
323
return false
331
324
}
@@ -340,7 +333,7 @@ export async function isPackageInstalled(pack: string) {
340
333
export async function isPackageRegexInstalled ( regexp : string ) {
341
334
try {
342
335
// 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" } )
344
337
if ( typeof stdout !== "string" ) {
345
338
return false
346
339
}
0 commit comments