UNPKG

@snapcommit/cli

Version:

Instant AI commits. Beautiful progress tracking. Never write commit messages again.

154 lines (153 loc) 4.75 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.supabase = void 0; exports.storeAuthTokens = storeAuthTokens; exports.getStoredAuth = getStoredAuth; exports.clearStoredAuth = clearStoredAuth; exports.isAuthenticated = isAuthenticated; exports.getCurrentUser = getCurrentUser; exports.checkLicenseStatus = checkLicenseStatus; exports.syncStatsToCloud = syncStatsToCloud; const supabase_js_1 = require("@supabase/supabase-js"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const os = __importStar(require("os")); const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; if (!supabaseUrl || !supabaseAnonKey) { throw new Error('Missing Supabase environment variables. Please check your .env file.'); } exports.supabase = (0, supabase_js_1.createClient)(supabaseUrl, supabaseAnonKey); // Token storage path const TOKEN_DIR = path.join(os.homedir(), '.snapcommit'); const TOKEN_FILE = path.join(TOKEN_DIR, 'auth_token.json'); /** * Store authentication tokens locally */ function storeAuthTokens(auth) { if (!fs.existsSync(TOKEN_DIR)) { fs.mkdirSync(TOKEN_DIR, { recursive: true }); } fs.writeFileSync(TOKEN_FILE, JSON.stringify(auth, null, 2)); } /** * Get stored authentication tokens */ function getStoredAuth() { try { if (!fs.existsSync(TOKEN_FILE)) { return null; } const data = fs.readFileSync(TOKEN_FILE, 'utf-8'); return JSON.parse(data); } catch (error) { return null; } } /** * Clear stored authentication tokens */ function clearStoredAuth() { try { if (fs.existsSync(TOKEN_FILE)) { fs.unlinkSync(TOKEN_FILE); } } catch (error) { // Silent fail } } /** * Check if user is authenticated */ async function isAuthenticated() { const stored = getStoredAuth(); if (!stored) { return false; } // Check if token is expired if (Date.now() >= stored.expires_at) { clearStoredAuth(); return false; } return true; } /** * Get current user */ async function getCurrentUser() { const stored = getStoredAuth(); if (!stored) { return null; } const { data, error } = await exports.supabase.auth.getUser(stored.access_token); if (error || !data.user) { clearStoredAuth(); return null; } return data.user; } /** * Check license status */ async function checkLicenseStatus(userId) { const { data, error } = await exports.supabase.rpc('check_license_status', { user_id_param: userId, }); if (error) { console.error('Error checking license:', error); return null; } return data && data.length > 0 ? data[0] : null; } /** * Sync stats to cloud */ async function syncStatsToCloud(userId, date, commits, commands, insertions, deletions, filesChanged) { const { error } = await exports.supabase.rpc('update_daily_stats', { user_id_param: userId, date_param: date, commits_delta: commits, commands_delta: commands, insertions_delta: insertions, deletions_delta: deletions, files_changed_delta: filesChanged, }); if (error) { console.error('Error syncing stats:', error); } }