@@ -3,6 +3,7 @@ import { glob, readFile, writeFile } from "node:fs/promises";
33import { gunzip } from "node:zlib" ;
44import { join } from "node:path" ;
55import { promisify } from "node:util" ;
6+ import assert from "node:assert/strict" ;
67
78const gunzipAsync = promisify ( gunzip ) ;
89
@@ -21,9 +22,11 @@ if (!TERMUX_ARCH) {
2122}
2223
2324const binPrefix = TERMUX_PREFIX . substring ( 1 ) + "/bin/" ;
25+ /**@type {unknown }*/
2426const repos = JSON . parse (
25- await readFile ( join ( TERMUX_SCRIPTDIR , "repo.json" ) ) ,
27+ await readFile ( join ( TERMUX_SCRIPTDIR , "repo.json" ) , "utf8" ) ,
2628) ;
29+ assert ( typeof repos == "object" && repos ) ;
2730
2831/**
2932 * Parses an alternative file and returns an array of alternative entries.
@@ -38,6 +41,7 @@ const repos = JSON.parse(
3841 * - `priority`: The priority of the alternative.
3942 *
4043 * Note that both the name and path do not start with TERMUX_PREFIX, but instead start with the relative path from TERMUX_PREFIX.
44+ * @param {string } filePath
4145 */
4246async function parseAlternativeFile ( filePath ) {
4347 const content = await readFile ( filePath , "utf8" ) ;
@@ -56,7 +60,6 @@ async function parseAlternativeFile(filePath) {
5660 let match = line . match ( / \s * # .* / ) ;
5761 line = line . substring ( 0 , match === null ? line . length : match . index ) ;
5862
59-
6063 if ( line . startsWith ( "Name: " ) ) {
6164 if ( parsingDependents ) {
6265 parsingDependents = false ;
@@ -138,6 +141,15 @@ async function parseAlternativeFile(filePath) {
138141 return alternatives ;
139142}
140143
144+ /**
145+ @param {{
146+ name: string,
147+ url: URL,
148+ distribution: string
149+ }} repo
150+ @param {string } repoPath
151+ @param {string } arch
152+ */
141153async function processRepo ( repo , repoPath , arch ) {
142154 // Fetch the Contents.gz file for the given architecture from the apt mirror
143155 const url = `${ repo . url } /dists/${ repo . distribution } /Contents-${ arch } .gz` ;
@@ -157,14 +169,20 @@ async function processRepo(repo, repoPath, arch) {
157169 // is the name of the package that provides this file.
158170 const lines = data . toString ( ) . split ( "\n" ) ;
159171
160- // Stores mappings of binary names to package names
161- // The key is the binary name, and the value is an array of package names
162- // that provide this binary
172+ /**
173+ * Stores mappings of binary names to package names
174+ * The key is the binary name, and the value is an array of package names
175+ * that provide this binary
176+ * @type {Map<string, string[]> }
177+ */
163178 const binMap = new Map ( ) ;
164179
165- // Stores mappings of file paths to package names
166- // This is needed to resolve the package names for binaries that are setup
167- // using the alternatives system
180+ /**
181+ * Stores mappings of file paths to package names
182+ * This is needed to resolve the package names for binaries that are setup
183+ * using the alternatives system
184+ * @type {Map<string, string> }
185+ */
168186 const fileMap = new Map ( ) ;
169187 // Populate the fileMap
170188 lines . forEach ( ( line ) => {
@@ -185,13 +203,13 @@ async function processRepo(repo, repoPath, arch) {
185203 if ( ! binMap . has ( packageName ) ) {
186204 binMap . set ( packageName , [ ] ) ;
187205 }
188- binMap . get ( packageName ) . push ( binary ) ;
206+ /** @type { string[] }*/ ( binMap . get ( packageName ) ) . push ( binary ) ;
189207 } ) ;
190208 } ) ;
191209
192210 // Now go through all the *.alternatives files in the repository and parse
193211 // them to find the alternatives and their dependents
194- repoPath = join ( TERMUX_SCRIPTDIR , repoPath ) ;
212+ repoPath = join ( /** @type { string }*/ ( TERMUX_SCRIPTDIR ) , repoPath ) ;
195213 for await ( const file of glob ( `${ repoPath } /*/*.alternatives` , {
196214 nodir : true ,
197215 } ) ) {
@@ -214,7 +232,7 @@ async function processRepo(repo, repoPath, arch) {
214232 if ( ! binMap . has ( packageName ) ) {
215233 binMap . set ( packageName , [ ] ) ;
216234 }
217- binMap . get ( packageName ) . push ( binary ) ;
235+ /** @type { string[] }*/ ( binMap . get ( packageName ) ) . push ( binary ) ;
218236 alternativeEntry . dependents . forEach ( ( { link, name : _ , path } ) => {
219237 if ( link . startsWith ( "bin/" ) ) {
220238 const depPackageName = fileMap . get (
@@ -224,7 +242,7 @@ async function processRepo(repo, repoPath, arch) {
224242 if ( ! binMap . has ( depPackageName ) ) {
225243 binMap . set ( depPackageName , [ ] ) ;
226244 }
227- binMap . get ( depPackageName ) . push ( depBinary ) ;
245+ /** @type { string[] }*/ ( binMap . get ( depPackageName ) ) . push ( depBinary ) ;
228246 }
229247 // Register the link in the fileMap for the package
230248 // This is used by vim.alternatives where bin/vim is a link with alternative libexec/vim/vim
@@ -233,7 +251,7 @@ async function processRepo(repo, repoPath, arch) {
233251 if ( ! binMap . has ( packageName ) ) {
234252 binMap . set ( packageName , [ ] ) ;
235253 }
236- binMap . get ( packageName ) . push ( binary ) ;
254+ /** @type { string[] }*/ ( binMap . get ( packageName ) ) . push ( binary ) ;
237255 } ) ;
238256 }
239257 // Register the link in the fileMap for the package
@@ -250,8 +268,7 @@ async function processRepo(repo, repoPath, arch) {
250268 const header = Array . from ( binMap . keys ( ) )
251269 . sort ( )
252270 . map ( ( packageName ) => {
253- const binaries = binMap
254- . get ( packageName )
271+ const binaries = /**@type {string[] }*/ ( binMap . get ( packageName ) )
255272 . sort ( )
256273 . map ( ( bin ) => `" ${ bin } ",` ) ;
257274 return `"${ packageName } ",\n${ binaries . join ( "\n" ) } ` ;
@@ -261,12 +278,32 @@ async function processRepo(repo, repoPath, arch) {
261278 await writeFile ( headerFile , header ) ;
262279}
263280
281+ /**
282+ @type {Promise<void>[] }
283+ */
264284const promises = [ ] ;
265285
266286for ( const path in repos ) {
267287 if ( path === "pkg_format" ) continue ;
288+
289+ //@ts -expect-error
268290 const repo = repos [ path ] ;
269- promises . push ( processRepo ( repo , path , TERMUX_ARCH ) ) ;
291+ assert ( typeof repo == "object" && repo ) ;
292+ assert ( "name" in repo && typeof repo . name == "string" ) ;
293+ assert ( "url" in repo && typeof repo . url == "string" ) ;
294+ assert ( "distribution" in repo && typeof repo . distribution == "string" ) ;
295+
296+ promises . push (
297+ processRepo (
298+ {
299+ name : repo . name ,
300+ url : new URL ( repo . url ) ,
301+ distribution : repo . distribution ,
302+ } ,
303+ path ,
304+ TERMUX_ARCH ,
305+ ) ,
306+ ) ;
270307}
271308
272309await Promise . all ( promises ) ;
0 commit comments