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]]
name = "format"
format_on_save = true

View File

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