make Replace work with both regex/nonregex, auto fix js/ts/tsx

This commit is contained in:
hophacker 2021-04-06 20:19:32 +08:00
parent ce0558a53f
commit 5442fdbb76
2 changed files with 9 additions and 2 deletions

View File

@ -93,3 +93,4 @@ name = "lang#swift"
[[layers]] [[layers]]
name = "format" name = "format"
format_on_save = true

View File

@ -107,6 +107,7 @@ function Replace () {
-s Source pattern -s Source pattern
-d Destination pattern -d Destination pattern
-r Remove line -r Remove line
--regex Match pattern with regex
--seperator= Seperator, # by default --seperator= Seperator, # by default
-h Display this message" -h Display this message"
} }
@ -116,6 +117,7 @@ function Replace () {
SEP=";" SEP=";"
DEBUG=false DEBUG=false
REMOVE=false REMOVE=false
REGEX=false
while getopts ":rhf:s:d:-:" opt while getopts ":rhf:s:d:-:" opt
do do
case "${opt}" in case "${opt}" in
@ -125,6 +127,9 @@ function Replace () {
debug) debug)
DEBUG=true DEBUG=true
;; ;;
regex)
REGEX=true
;;
seperator=*) seperator=*)
val=${OPTARG#*=} val=${OPTARG#*=}
SEP=$val SEP=$val
@ -142,8 +147,9 @@ function Replace () {
usage; return 1 ;; usage; return 1 ;;
esac esac
done done
shift $(($OPTIND-1)) SEARCH_CMD="ag `$REGEX || echo -Q` \"$SRC\" -l -G \"$FILE_REGEX\""
MATCHED_FILES=`ag -Q $SRC -l -G $FILE_REGEX` MATCHED_FILES=`eval "$SEARCH_CMD"`
echo "Replace in current files:$fg[green]\n$MATCHED_FILES$reset_color"
if $REMOVE; then if $REMOVE; then
SED_CMD=\\${SEP}$SRC${SEP}d SED_CMD=\\${SEP}$SRC${SEP}d
else else