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

View File

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