From b213bef04032791d0c266c94c8e2ee80a21ad254 Mon Sep 17 00:00:00 2001 From: Bill Ross Date: Tue, 29 Apr 2025 23:10:23 -0600 Subject: [PATCH] Working v1 --- .gitignore | 2 ++ README.md | 12 +++++++++++- index.js | 43 +++++++++++++++++++------------------------ package.json | 7 +++++-- 4 files changed, 37 insertions(+), 27 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a7b33a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +package-lock.json +node_modules diff --git a/README.md b/README.md index 263c14c..91debf7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,12 @@ -# npm-gitea +NPM-GITEA +========= +Connect to gitea (using tea configuration) and list packages + +SETUP +----- + +``` +npm install +npm install -g . +``` diff --git a/index.js b/index.js index 53cd201..103c771 100755 --- a/index.js +++ b/index.js @@ -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); diff --git a/package.json b/package.json index da48ca6..cff6d96 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "ISC", "dependencies": { + "debug": "^4.4.0", "yaml": "^2.7.1" } }