1+ import { info } from "ci-log"
12import escapeRegex from "escape-string-regexp"
23import { execa } from "execa"
34import { getAptEnv } from "./apt-env.js"
@@ -42,35 +43,31 @@ async function aptPackageType(
4243 version : string | undefined ,
4344 fallBackToLatest : boolean ,
4445) : 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 ) ) {
5252 return AptPackageType . NameDashVersion
5353 }
5454
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
6458 }
6559 }
6660
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.` )
7164 }
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
7471 }
7572
7673 // If apt-cache fails, update the repos and try again
@@ -79,14 +76,47 @@ async function aptPackageType(
7976 return aptPackageType ( apt , name , version , fallBackToLatest )
8077 }
8178
82- if ( version === undefined || version === "" || fallBackToLatest ) {
79+ if ( canFallBackToLatest ) {
8380 // if the version is undefined or empty, return the name as a package name
81+ logFallback ( )
8482 return AptPackageType . Name
8583 }
8684
8785 return AptPackageType . None
8886}
8987
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+
90120async function getAptArg ( apt : string , pack : AptPackage ) {
91121 const { name, version, fallBackToLatest = false } = pack
92122
0 commit comments