/** * Push project to Gitea without system git (uses isomorphic-git) * Usage: node server/scripts/push-gitea-node.js */ const fs = require('fs'); const path = require('path'); const git = require('isomorphic-git'); const http = require('isomorphic-git/http/node'); const ROOT = path.resolve(__dirname, '..', '..'); function loadEnvGitea() { const envFile = path.join(ROOT, '.env.gitea'); if (!fs.existsSync(envFile)) return; for (const line of fs.readFileSync(envFile, 'utf8').split(/\r?\n/)) { if (/^\s*#/.test(line) || /^\s*$/.test(line)) continue; const idx = line.indexOf('='); if (idx <= 0) continue; const key = line.slice(0, idx).trim(); const value = line.slice(idx + 1).trim(); if (!process.env[key]) process.env[key] = value; } } loadEnvGitea(); const GITEA_URL = process.env.GITEA_URL || 'https://git.xdjchuanhui.com'; const GITEA_USER = process.env.GITEA_USER || process.env.GITEA_OWNER || 'jxc'; const GITEA_PASS = process.env.GITEA_PASS || process.env.GITEA_TOKEN || process.env.GITEA_PASSWORD || ''; const REPO_NAME = process.env.GITEA_REPO || 'ai-fresh-delivery'; if (!GITEA_PASS) { console.error( 'Missing Gitea credentials. Either:\n' + ' 1) Create .env.gitea in project root (copy from .env.gitea.example), or\n' + ' 2) Set env: $env:GITEA_PASS = "your_token_or_password"; npm run push:gitea' ); process.exit(1); } const authHeader = 'Basic ' + Buffer.from(`${GITEA_USER}:${GITEA_PASS}`).toString('base64'); async function api(method, apiPath, body) { const res = await fetch(`${GITEA_URL}${apiPath}`, { method, headers: { Authorization: authHeader, ...(body ? { 'Content-Type': 'application/json; charset=utf-8' } : {}), }, body: body ? JSON.stringify(body) : undefined, }); const text = await res.text(); let data = null; try { data = text ? JSON.parse(text) : null; } catch { data = text; } if (!res.ok && !(res.status === 409 && method === 'POST')) { throw new Error(`${method} ${apiPath} failed (${res.status}): ${text}`); } return { ok: res.ok, status: res.status, data }; } function shouldIgnore(rel) { const parts = rel.split(/[/\\]/); const ignore = new Set([ 'node_modules', '.git', 'dist', '.tools', 'server/uploads', '.env', '.env.production', '.env.gitea', '.env.split', '.env.tencent', ]); if (parts.some((p) => ignore.has(p))) return true; if (rel.endsWith('.log')) return true; if (rel === 'server/.env') return true; if (rel.startsWith('deploy/id_rsa')) return true; if (rel === 'SecretKey.csv') return true; if (rel === 'miniprogram/config.js') return true; return false; } function walk(dir, base = dir, out = []) { for (const name of fs.readdirSync(dir)) { const full = path.join(dir, name); const rel = path.relative(base, full).replace(/\\/g, '/'); if (shouldIgnore(rel)) continue; const stat = fs.statSync(full); if (stat.isDirectory()) walk(full, base, out); else out.push(rel); } return out; } async function main() { console.log('Creating repository on Gitea...'); const create = await api('POST', '/api/v1/user/repos', { name: REPO_NAME, description: 'AI生鲜配送系统 - 智能生鲜供应链管理平台', private: false, auto_init: false, }); if (create.status === 409) console.log('Repository already exists, continuing.'); else console.log('Repository created.'); const gitDir = path.join(ROOT, '.git'); if (fs.existsSync(gitDir)) fs.rmSync(gitDir, { recursive: true, force: true }); await git.init({ fs, dir: ROOT, defaultBranch: 'main' }); const files = walk(ROOT); console.log(`Adding ${files.length} files...`); for (const rel of files) { await git.add({ fs, dir: ROOT, filepath: rel }); } await git.commit({ fs, dir: ROOT, message: process.env.GITEA_COMMIT_MESSAGE || 'Update: split deployment and latest fixes', author: { name: GITEA_USER, email: `${GITEA_USER}@local` }, }); const encodedUser = encodeURIComponent(GITEA_USER); const encodedRepo = encodeURIComponent(REPO_NAME); const remoteUrl = `${GITEA_URL}/${encodedUser}/${encodedRepo}.git`; await git.addRemote({ fs, dir: ROOT, remote: 'origin', url: remoteUrl, }); console.log('Pushing to', remoteUrl); const pushResult = await git.push({ fs, http, dir: ROOT, remote: 'origin', ref: 'main', force: true, onAuth: () => ({ username: GITEA_USER, password: GITEA_PASS }), }); console.log('Push complete:', pushResult); console.log('Repository:', `${GITEA_URL}/${GITEA_USER}/${REPO_NAME}`); } main().catch((err) => { console.error(err); process.exit(1); });