Working v1

This commit is contained in:
PearlDragon 2025-04-29 23:10:23 -06:00
parent c96350f10c
commit b213bef040
4 changed files with 37 additions and 27 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
package-lock.json
node_modules

View File

@ -1,2 +1,12 @@
# npm-gitea
NPM-GITEA
=========
Connect to gitea (using tea configuration) and list packages
SETUP
-----
```
npm install
npm install -g .
```

View File

@ -1,5 +1,6 @@
#!/usr/bin/env node
const debug = require('debug')('cli')
const https = require("https");
const fs = require("fs");
const path = require("path");
@ -7,12 +8,13 @@ const os = require("os");
const { URL } = require("url");
const yaml = require("yaml");
// CLI flags
const args = process.argv.slice(2);
const asJSON = args.includes("--json");
const asMarkdown = args.includes("--markdown");
// === CLI PARSING ===
const rawArgs = process.argv.slice(2);
const asJSON = rawArgs.includes("--json");
const asMarkdown = rawArgs.includes("--markdown");
const query = rawArgs.find(arg => !arg.startsWith("--")) || null;
// === STEP 1: Load config from tea YAML ===
// === CONFIG SEARCH ===
function findTeaConfig() {
const xdg = process.env.XDG_CONFIG_HOME;
const home = os.homedir();
@ -20,28 +22,17 @@ function findTeaConfig() {
const paths = [];
// 1. XDG_CONFIG_HOME override
if (xdg) {
paths.push(path.join(xdg, "tea", "config.yml"));
}
// 2. Platform-specific default
if (xdg) paths.push(path.join(xdg, "tea", "config.yml"));
if (platform === "darwin") {
// macOS: ~/Library/Application Support/tea/config.yml
paths.push(path.join(home, "Library", "Application Support", "tea", "config.yml"));
} else {
// Linux/others: ~/.config/tea/config.yml
paths.push(path.join(home, ".config", "tea", "config.yml"));
}
// 3. Legacy fallback
paths.push(path.join(home, ".tea", "tea.yml"));
// 4. First existing config wins
for (const p of paths) {
if (fs.existsSync(p)) return p;
}
return null;
}
@ -62,6 +53,12 @@ function loadGiteaLogin() {
}
const login = logins.find((l) => l.default) || logins[0];
debug(`🔧 Using config: ${configPath}`);
debug(`👤 Gitea user: ${login.user}`);
debug(`🌐 Gitea URL: ${login.url}`);
debug(`🔐 Auth token: ${login.token ? '[present]' : '[missing]'}`);
return {
baseUrl: login.url.replace(/\/$/, ""),
token: login.token,
@ -69,7 +66,6 @@ function loadGiteaLogin() {
};
}
// === STEP 2: Fetch package list from Gitea ===
function fetchJSON(baseUrl, path, token) {
return new Promise((resolve, reject) => {
const url = new URL(path, baseUrl);
@ -95,7 +91,6 @@ function fetchJSON(baseUrl, path, token) {
});
}
// === STEP 3: Output functions ===
function printPlain(packages) {
packages.forEach(pkg => {
console.log(`${pkg.name}@${pkg.version}${pkg.created_at}`);
@ -117,14 +112,14 @@ function printJSON(packages) {
console.log(JSON.stringify(packages, null, 2));
}
// === STEP 4: Main logic ===
async function listPackages() {
const { baseUrl, token, user } = loadGiteaLogin();
const isOrg = false; // You can change this or extend to CLI option
const endpoint = isOrg
? `/api/v1/orgs/${user}/packages/npm`
: `/api/v1/users/${user}/packages/npm`;
const queryParams = new URLSearchParams({ type: "npm" });
if (query) queryParams.append("q", query);
const endpoint = `/api/v1/packages/${user}?${queryParams.toString()}`;
debug(`📦 Fetching from: ${baseUrl}${endpoint}`);
try {
const packages = await fetchJSON(baseUrl, endpoint, token);

View File

@ -1,9 +1,11 @@
{
"name": "gitea-npm",
"name": "npm-gitea",
"version": "1.0.0",
"description": "Manage npm packages stored on gitea",
"main": "index.js",
"bin": "npm-gitea",
"bin": {
"npm-gitea": "index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
@ -15,6 +17,7 @@
"author": "William Ross <wnross@gwhc.net>",
"license": "ISC",
"dependencies": {
"debug": "^4.4.0",
"yaml": "^2.7.1"
}
}