Skip to content

Commit 380faec

Browse files
committed
tmp
1 parent 91b4388 commit 380faec

5 files changed

Lines changed: 155 additions & 17 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# NPM
2+
node_modules/
3+
14
# Python
25
__pycache__
36

generate-db.js

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { glob, readFile, writeFile } from "node:fs/promises";
33
import { gunzip } from "node:zlib";
44
import { join } from "node:path";
55
import { promisify } from "node:util";
6+
import assert from "node:assert/strict";
67

78
const gunzipAsync = promisify(gunzip);
89

@@ -21,9 +22,11 @@ if (!TERMUX_ARCH) {
2122
}
2223

2324
const binPrefix = TERMUX_PREFIX.substring(1) + "/bin/";
25+
/**@type {unknown}*/
2426
const 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
*/
4246
async 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+
*/
141153
async 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+
*/
264284
const promises = [];
265285

266286
for (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

272309
await Promise.all(promises);

jsconfig.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* https://aka.ms/tsconfig */
2+
{
3+
"compilerOptions": {
4+
"target": "ESNext",
5+
//"lib": [],
6+
//"moduleDetection": "auto",
7+
"module": "ESNext",
8+
//"moduleResolution": "node16",
9+
"allowJs": true,
10+
"checkJs": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"strict": true,
13+
"noUnusedLocals": true,
14+
"noUnusedParameters": true,
15+
"exactOptionalPropertyTypes": true,
16+
"noImplicitReturns": true,
17+
"noFallthroughCasesInSwitch": true,
18+
"noUncheckedIndexedAccess": true,
19+
"noImplicitOverride": true,
20+
"noPropertyAccessFromIndexSignature": true,
21+
"allowUnreachableCode": false,
22+
//"skipDefaultLibCheck": true,
23+
"skipLibCheck": true
24+
}
25+
}

package-lock.json

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
{
2+
"name": "cmd-not-found-gen-db",
23
"type": "module",
3-
"private": true
4+
"private": true,
5+
"version": "0.1.0",
6+
"bugs": "https://github.com/termux/command-not-found/issues",
7+
"repository": "github:termux/command-not-found",
8+
"devDependencies": {
9+
"@types/node": "^24.0.10",
10+
"prettier": "^3.6.2",
11+
"typescript": "^5.8.3"
12+
}
413
}

0 commit comments

Comments
 (0)