UNPKG

@nesardramos/fmg

Version:

A CLI tool to manage path aliases in your terminal.

151 lines (123 loc) 3.85 kB
// commands/setup.js const fs = require("fs"); const path = require("path"); const os = require("os"); const { readAliases } = require("../utils/aliases"); const functionsFilePath = path.join(os.homedir(), ".fmg-functions.sh"); const completionFilePath = path.join(os.homedir(), ".fmg-completion.zsh"); const shellScriptContent = ` # This file is managed by the fmg CLI. Do not edit manually. # Function to change directory using an alias fcd () { local alias_path # Try to get alias path and suppress any errors alias_path="$(fmg get "$1" 2>/dev/null)" if [ $? -eq 0 ]; then # If alias exists, change to its path cd "$alias_path" else # If no alias found, treat it as a regular path cd "$1" fi } # Function to copy files/folders using an alias # Function to copy files/folders using an alias or regular path fcp () { local source_path="$1" local destination_path="$2" local source_resolved_path source_resolved_path="$(fmg get "$source_path" 2>/dev/null)" # Check if the first argument is a valid alias. if [ $? -eq 0 ]; then source_path="$source_resolved_path" fi local destination_resolved_path destination_resolved_path="$(fmg get "$destination_path" 2>/dev/null)" # Check if the second argument is a valid alias. if [ $? -eq 0 ]; then destination_path="$destination_resolved_path" fi cp -r "$source_path" "$destination_path" } # Function to move files/folders using an alias fmv () { local alias_path alias_path="$(fmg get "$1")" if [ $? -eq 0 ]; then local source_path="$2" local destination_path="$3" local source_resolved_path="$(fmg get "$source_path" 2>/dev/null)" if [ $? -eq 0 ]; then source_path="$source_resolved_path" fi local destination_resolved_path="$(fmg get "$destination_path" 2>/dev/null)" if [ $? -eq 0 ]; then destination_path="$destination_resolved_path" fi mv "$source_path" "$destination_path" else echo "Error: Alias '$1' not found." fi } `; function generateCompletionScript() { return ` #compdef fmg fcd fcp fmv _fmg_completions() { local -a aliases aliases=($(fmg _get-aliases)) _describe 'aliases' aliases } _fmg() { local -a commands commands=(set get list delete setup remove completion) _arguments '*:command:commands' case "$words[2]" in set) _arguments '*:alias:(set_alias) *:(path):_files' ;; get) _fmg_completions ;; list|setup|remove|completion) # No arguments for these commands ;; delete) _fmg_completions ;; *) # Fallback for main command ;; esac } _fmg_fcd() { _alternative 'files:->files:_files' 'aliases:->aliases:_fmg_completions' } _fmg_fcp() { _alternative 'files:->files:_files' 'aliases:->aliases:_fmg_completions' } _fmg_fmv() { _alternative 'files:->files:_files' 'aliases:->aliases:_fmg_completions' } `; } exports.command = "setup"; exports.describe = "Installs fcd, fcp, fmv functions & sets up autocompletion."; exports.handler = (argv) => { try { fs.writeFileSync(functionsFilePath, shellScriptContent.trim(), "utf8"); console.log(`Shell functions written to: ${functionsFilePath}`); const completionScript = generateCompletionScript(); fs.writeFileSync(completionFilePath, completionScript, "utf8"); console.log(`Zsh completion script written to: ${completionFilePath}`); console.log(` To complete the setup, add these lines to your ~/.zshrc file: source ${functionsFilePath} source ${completionFilePath} Then, run 'source ~/.zshrc' to apply the changes. `); } catch (err) { console.error("Error during setup:", err); process.exit(1); } };