haiku-bloom
Version:
A delightful CLI tool that generates beautiful haikus 🌸
98 lines (81 loc) • 3.46 kB
JavaScript
const fs = require('fs');
const os = require('os');
const path = require('path');
const { execSync } = require('child_process');
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
fgGreen: '\x1b[32m',
fgYellow: '\x1b[33m',
fgRed: '\x1b[31m',
fgCyan: '\x1b[36m'
};
function getShellConfigFile() {
const shell = process.env.SHELL || '';
const home = os.homedir();
if (shell.includes('zsh')) {
return path.join(home, '.zshrc');
} else if (shell.includes('bash')) {
// Check for .bash_profile on macOS, .bashrc on Linux
const bashProfile = path.join(home, '.bash_profile');
const bashrc = path.join(home, '.bashrc');
if (process.platform === 'darwin' && fs.existsSync(bashProfile)) {
return bashProfile;
}
return bashrc;
} else if (shell.includes('fish')) {
return path.join(home, '.config', 'fish', 'config.fish');
}
// Default to .bashrc
return path.join(home, '.bashrc');
}
function getAliasCommand(shellConfig) {
// Use npx which will find the package regardless of where it's installed
if (shellConfig.includes('fish')) {
return `alias haiku-bloom="npx haiku-bloom"`;
}
return `alias haiku-bloom="npx haiku-bloom"`;
}
function addAlias() {
try {
const shellConfig = getShellConfigFile();
const aliasCommand = getAliasCommand(shellConfig);
const aliasMarker = '# haiku-bloom alias';
console.log(`${colors.fgCyan}🌸 Setting up haiku-bloom...${colors.reset}`);
console.log(`${colors.fgYellow}Shell config file: ${shellConfig}${colors.reset}`);
// Create shell config file if it doesn't exist
if (!fs.existsSync(shellConfig)) {
fs.writeFileSync(shellConfig, '');
console.log(`${colors.fgGreen}Created ${shellConfig}${colors.reset}`);
}
// Read current shell config
let configContent = fs.readFileSync(shellConfig, 'utf8');
// Check if alias already exists
if (configContent.includes(aliasMarker)) {
console.log(`${colors.fgYellow}Alias already exists, updating...${colors.reset}`);
// Remove old alias
const lines = configContent.split('\n');
const newLines = lines.filter(line => !line.includes(aliasMarker) && !line.includes('alias haiku-bloom='));
configContent = newLines.join('\n');
}
// Add new alias
const newAlias = `\n${aliasMarker}\n${aliasCommand}\n`;
fs.appendFileSync(shellConfig, newAlias);
console.log(`${colors.fgGreen}✓ Added haiku-bloom alias to ${shellConfig}${colors.reset}`);
console.log(`\n${colors.bright}${colors.fgGreen}✨ Installation complete!${colors.reset}`);
console.log(`${colors.fgCyan}To use haiku-bloom immediately, run:${colors.reset}`);
console.log(` ${colors.bright}source ${shellConfig}${colors.reset}`);
console.log(`${colors.fgCyan}Then you can use:${colors.reset}`);
console.log(` ${colors.bright}haiku-bloom${colors.reset}`);
console.log(`\n${colors.fgYellow}Note: New terminal sessions will have the command available automatically.${colors.reset}\n`);
} catch (error) {
console.error(`${colors.fgRed}Error setting up alias: ${error.message}${colors.reset}`);
console.log(`${colors.fgYellow}You can manually add this to your shell config:${colors.reset}`);
console.log(` alias haiku-bloom="npx haiku-bloom"`);
}
}
// Only run if this is the main module
if (require.main === module) {
addAlias();
}