@@ -17,22 +17,67 @@ export type ApkPackage = {
17
17
fallBackToLatest ?: boolean
18
18
}
19
19
20
- export async function filterAndQualifyApkPackages ( packages : ApkPackage [ ] ) {
21
- // Filter out packages that are already installed
22
- const installedPackages = await Promise . all ( packages . map ( checkPackageInstalled ) )
23
- return packages . filter ( ( _pack , index ) => ! installedPackages [ index ] )
24
- }
25
20
/**
26
21
* Check if a package is already installed
27
- * @param pack The package to check
22
+ * @param pkg The package to check
28
23
* @returns Whether the package is installed
29
24
*/
30
-
31
- async function checkPackageInstalled ( pack : ApkPackage ) : Promise < boolean > {
25
+ export async function checkPackageInstalled ( pkg : ApkPackage ) : Promise < boolean > {
32
26
try {
33
- const { exitCode } = await execRoot ( "apk" , [ "info" , "-e" , pack . name ] , { reject : false } )
34
- return exitCode === 0
27
+ // First check if the package is installed at all
28
+ const { exitCode } = await execRoot ( "apk" , [ "info" , "-e" , pkg . name ] , {
29
+ reject : false ,
30
+ stdio : [ "ignore" , "pipe" , "ignore" ] ,
31
+ } )
32
+
33
+ if ( exitCode !== 0 ) {
34
+ return false
35
+ }
36
+
37
+ // If no specific version is required, we're done
38
+ if ( pkg . version === undefined ) {
39
+ return true
40
+ }
41
+
42
+ // Check the installed version
43
+ const { stdout : versionOutput } = await execRoot ( "apk" , [ "info" , "-v" , pkg . name ] , {
44
+ stdio : [ "ignore" , "pipe" , "ignore" ] ,
45
+ } )
46
+
47
+ // Parse the version from output (format is typically "package-name-version")
48
+ const installedVersion = versionOutput . trim ( ) . split ( "-" ) . slice ( - 1 ) [ 0 ]
49
+
50
+ return installedVersion === pkg . version
35
51
} catch ( error ) {
36
52
return false
37
53
}
38
54
}
55
+
56
+ /**
57
+ * Filter out packages that are already installed and qualify those that need installation
58
+ * @param packages List of packages to check
59
+ * @returns List of packages that need to be installed
60
+ */
61
+ export async function filterAndQualifyApkPackages ( packages : ApkPackage [ ] ) : Promise < string [ ] > {
62
+ const results = await Promise . all (
63
+ packages . map ( async ( pack ) => {
64
+ const isInstalled = await checkPackageInstalled ( pack )
65
+ return isInstalled ? undefined : pack
66
+ } ) ,
67
+ )
68
+
69
+ return results . filter ( ( pack ) : pack is ApkPackage => pack !== undefined )
70
+ . map ( formatPackageWithVersion )
71
+ }
72
+
73
+ /**
74
+ * Format a package with its version if specified
75
+ * @param pkg The package object
76
+ * @returns Formatted package string (name=version or just name)
77
+ */
78
+ export function formatPackageWithVersion ( pkg : ApkPackage ) : string {
79
+ if ( pkg . version !== undefined ) {
80
+ return `${ pkg . name } =${ pkg . version } `
81
+ }
82
+ return pkg . name
83
+ }
0 commit comments