1
+ import { info } from "ci-log"
1
2
import escapeRegex from "escape-string-regexp"
2
3
import { execa } from "execa"
3
4
import { getAptEnv } from "./apt-env.js"
@@ -42,35 +43,31 @@ async function aptPackageType(
42
43
version : string | undefined ,
43
44
fallBackToLatest : boolean ,
44
45
) : Promise < AptPackageType > {
45
- if ( version !== undefined && version !== "" ) {
46
- const { stdout } = await execa ( "apt-cache" , [
47
- "search" ,
48
- "--names-only" ,
49
- `^${ escapeRegex ( name ) } -${ escapeRegex ( version ) } $` ,
50
- ] , { env : getAptEnv ( apt ) , stdio : "pipe" } )
51
- if ( stdout . trim ( ) !== "" ) {
46
+ const hasVersion = version !== undefined && version !== ""
47
+ const canFallBackToLatest = ! hasVersion || fallBackToLatest
48
+
49
+ if ( hasVersion ) {
50
+ // check if apt-get search can find the version
51
+ if ( await aptCacheSearchHasPackage ( apt , name , version ) ) {
52
52
return AptPackageType . NameDashVersion
53
53
}
54
54
55
- try {
56
- // check if apt-get show can find the version
57
- // eslint-disable-next-line @typescript-eslint/no-shadow
58
- const { stdout } = await execa ( "apt-cache" , [ "show" , `${ name } =${ version } ` ] , { env : getAptEnv ( apt ) } )
59
- if ( stdout . trim ( ) === "" ) {
60
- return AptPackageType . NameEqualsVersion
61
- }
62
- } catch {
63
- // ignore
55
+ // check if apt-get show can find the version
56
+ if ( await aptCacheShowHasPackage ( apt , `${ name } =${ version } ` ) ) {
57
+ return AptPackageType . NameEqualsVersion
64
58
}
65
59
}
66
60
67
- try {
68
- const { stdout : showStdout } = await execa ( "apt-cache" , [ "show" , name ] , { env : getAptEnv ( apt ) , stdio : "pipe" } )
69
- if ( showStdout . trim ( ) !== "" ) {
70
- return AptPackageType . Name
61
+ const logFallback = ( ) => {
62
+ if ( hasVersion && fallBackToLatest ) {
63
+ info ( `Could not find package ${ name } ${ version } . Falling back to latest version.` )
71
64
}
72
- } catch {
73
- // ignore
65
+ }
66
+
67
+ if ( canFallBackToLatest && await aptCacheShowHasPackage ( apt , name ) ) {
68
+ // if the version is undefined or empty, return the name as a package name
69
+ logFallback ( )
70
+ return AptPackageType . Name
74
71
}
75
72
76
73
// If apt-cache fails, update the repos and try again
@@ -79,14 +76,47 @@ async function aptPackageType(
79
76
return aptPackageType ( apt , name , version , fallBackToLatest )
80
77
}
81
78
82
- if ( version === undefined || version === "" || fallBackToLatest ) {
79
+ if ( canFallBackToLatest ) {
83
80
// if the version is undefined or empty, return the name as a package name
81
+ logFallback ( )
84
82
return AptPackageType . Name
85
83
}
86
84
87
85
return AptPackageType . None
88
86
}
89
87
88
+ async function aptCacheSearchHasPackage ( apt : string , name : string , version : string ) {
89
+ try {
90
+ const { stdout } = await execa ( "apt-cache" , [
91
+ "search" ,
92
+ "--names-only" ,
93
+ `^${ escapeRegex ( name ) } -${ escapeRegex ( version ) } $` ,
94
+ ] , { env : getAptEnv ( apt ) , stdio : "pipe" } )
95
+ if ( stdout . trim ( ) !== "" ) {
96
+ return true
97
+ }
98
+ } catch {
99
+ // ignore
100
+ }
101
+ return false
102
+ }
103
+
104
+ async function aptCacheShowHasPackage ( apt : string , arg : string ) {
105
+ try {
106
+ const { stdout } = await execa ( "apt-cache" , [ "show" , arg ] , {
107
+ env : getAptEnv ( apt ) ,
108
+ stdio : "pipe" ,
109
+ verbose : true ,
110
+ } )
111
+ if ( stdout . trim ( ) !== "" ) {
112
+ return true
113
+ }
114
+ } catch {
115
+ // ignore
116
+ }
117
+ return false
118
+ }
119
+
90
120
async function getAptArg ( apt : string , pack : AptPackage ) {
91
121
const { name, version, fallBackToLatest = false } = pack
92
122
0 commit comments