vibetime
Version:
Track your Claude AI usage and costs. Built on ccusage. See rankings, sync data, and monitor your AI spending. Works with all Claude models.
127 lines (109 loc) • 3.39 kB
JavaScript
/**
* Smart postinstall script for vibetime CLI
* Attempts to use git submodule first, falls back to npm package
*/
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const projectRoot = join(__dirname, '..');
const ccusagePath = join(projectRoot, 'ccusage');
console.log('📦 Setting up ccusage integration...');
// Check if we're in a git repository with submodules
function isInGitRepoWithSubmodules() {
try {
// Check if .git exists
if (!existsSync(join(projectRoot, '.git'))) {
return false;
}
// Check if .gitmodules exists
if (!existsSync(join(projectRoot, '.gitmodules'))) {
return false;
}
return true;
} catch (error) {
return false;
}
}
// Try to initialize submodules
function tryInitSubmodules() {
try {
console.log('🔄 Attempting to initialize git submodules...');
execSync('git submodule update --init --recursive', {
stdio: 'inherit',
cwd: projectRoot
});
return true;
} catch (error) {
console.log('⚠️ Failed to initialize git submodules:', error.message);
return false;
}
}
// Build ccusage bundle from submodule
function buildCcusageBundle() {
try {
console.log('🔨 Building ccusage bundle from submodule...');
execSync('node build-ccusage-bundle.js', {
stdio: 'inherit',
cwd: __dirname
});
return true;
} catch (error) {
console.log('⚠️ Failed to build ccusage bundle:', error.message);
return false;
}
}
// Create fallback bundle using npm package
function createFallbackBundle() {
try {
console.log('🔧 Creating fallback bundle using npm ccusage package...');
execSync('node create-fallback-bundle.js', {
stdio: 'inherit',
cwd: __dirname
});
return true;
} catch (error) {
console.error('❌ Failed to create fallback bundle:', error.message);
return false;
}
}
// Main setup logic
async function main() {
let success = false;
// Strategy 1: Try git submodule approach
if (isInGitRepoWithSubmodules()) {
console.log('📁 Detected git repository with submodules');
if (tryInitSubmodules() && existsSync(ccusagePath)) {
console.log('✅ Submodules initialized successfully');
if (buildCcusageBundle()) {
console.log('✅ ccusage bundle built from submodule');
success = true;
}
}
} else {
console.log('📦 Not in git repository or no submodules detected');
}
// Strategy 2: Fallback to npm package
if (!success) {
console.log('🔄 Falling back to npm ccusage package...');
if (createFallbackBundle()) {
console.log('✅ Fallback ccusage bundle created from npm package');
success = true;
}
}
if (success) {
console.log('🎉 ccusage integration setup complete!');
console.log(' You can now use vibetime CLI with full ccusage compatibility.');
} else {
console.error('❌ Failed to setup ccusage integration');
console.error(' Please check your environment and try again.');
process.exit(1);
}
}
main().catch(error => {
console.error('❌ Postinstall failed:', error);
process.exit(1);
});