diff --git a/scripts/far.nu b/scripts/far.nu new file mode 100755 index 0000000..3622cc5 --- /dev/null +++ b/scripts/far.nu @@ -0,0 +1,42 @@ +#!/usr/bin/env nu +# SPDX-License-Identifier: AGPL-3.0-only +# Copyright (c) 2026 MatrixFurry + +use std/log + +# Find and replace text from all files in a directory, using the `str replace` syntax +export def main [ + --directory (-d): path = "." + --no-git (-g) # Skip git repository check + --regex (-r) + find: string + replace: string +] { + let $directory = $directory | path expand + + if not $no_git and (git status | complete).exit_code != 0 { + error make { + msg: "Not a git repository" + label: { + text: "Directory is not a git repo" + span: (metadata $directory).span + } + help: "This check exists to prevent accedental changes without a VCS to revert them. If you have a backup or other VCS, use --no-git (-g) to bypass this check." + } + } + + log info $"FAR|\"($find)\" -> \"($replace)\"" + + ls $directory + | where type == file + | get name + | each {|file| + log info $"FAR| in '($file)'" + open $file --raw + | str replace -am --regex=$regex $find $replace + | collect + | save -fp $file + } + + log info "FAR|Done!" +}