@@ -16,19 +16,59 @@ jobs:
1616 - name : Checkout repository
1717 uses : actions/checkout@v4
1818
19- - name : Replace require paths, add timestamp, and create dist file
19+ - name : Set up Node.js
20+ uses : actions/setup-node@v4
21+ with :
22+ node-version : ' 20'
23+
24+ - name : Install Terser
25+ run : npm install terser -g
26+
27+ - name : Build and Minify User Script
2028 run : |
29+ set -e
30+
2131 TIMESTAMP=$(date +%s)
32+ SOURCE_FILE="Picviewer CE+/Picviewer CE+.user.js"
33+ DIST_FILE="Picviewer CE+/dist.user.js"
34+
35+ TEMP_DIR=$(mktemp -d)
36+ TEMP_PROCESSED_FILE="$TEMP_DIR/processed.user.js"
37+ HEADER_FILE="$TEMP_DIR/header.txt"
38+ BODY_FILE="$TEMP_DIR/body.js"
39+ MINIFIED_BODY_FILE="$TEMP_DIR/body.min.js"
40+
41+ echo "Step 1: Replacing paths and adding timestamp..."
42+ sed -e "s#https://update.greasyfork.org/scripts/6158/[0-9]\+/GM_config%20CN.js#http://hoothin.github.io/UserScripts/Picviewer%20CE%2B/GM_config%20CN.js?v=$TIMESTAMP#" \
43+ -e "s#https://update.greasyfork.org/scripts/438080/[0-9]\+/pvcep_rules.js#http://hoothin.github.io/UserScripts/Picviewer%20CE%2B/pvcep_rules.js?v=$TIMESTAMP#" \
44+ -e "s#https://update.greasyfork.org/scripts/440698/[0-9]\+/pvcep_lang.js#http://hoothin.github.io/UserScripts/Picviewer%20CE%2B/pvcep_lang.js?v=$TIMESTAMP#" \
45+ "$SOURCE_FILE" > "$TEMP_PROCESSED_FILE"
46+ echo "Path replacement complete."
2247
23- sed -e "s#https://update.greasyfork.org/scripts/6158/23710/GM_config%20CN.js#http://hoothin.github.io/UserScripts/Picviewer%20CE%2B/GM_config%20CN.js?v=$TIMESTAMP#" \
24- -e "s#https://update.greasyfork.org/scripts/438080/1655629/pvcep_rules.js#http://hoothin.github.io/UserScripts/Picviewer%20CE%2B/pvcep_rules.js?v=$TIMESTAMP#" \
25- -e "s#https://update.greasyfork.org/scripts/440698/1653424/pvcep_lang.js#http://hoothin.github.io/UserScripts/Picviewer%20CE%2B/pvcep_lang.js?v=$TIMESTAMP#" \
26- "Picviewer CE+/Picviewer CE+.user.js" > "Picviewer CE+/dist.user.js"
48+ echo "Step 2: Separating UserScript header and body..."
49+ sed -n '/\/\/ ==UserScript==/,/\/\/ ==\/UserScript==/p' "$TEMP_PROCESSED_FILE" > "$HEADER_FILE"
50+ sed '1,/\/\/ ==\/UserScript==/d' "$TEMP_PROCESSED_FILE" > "$BODY_FILE"
51+ echo "Header and body separated."
52+
53+ echo "Step 3: Minifying the script body with Terser..."
54+ terser "$BODY_FILE" --compress --mangle -o "$MINIFIED_BODY_FILE"
55+ echo "Minification complete."
56+
57+ echo "Step 4: Combining header and minified body..."
58+ cat "$HEADER_FILE" > "$DIST_FILE"
59+ echo "" >> "$DIST_FILE"
60+ cat "$MINIFIED_BODY_FILE" >> "$DIST_FILE"
61+ echo "Final dist file created at $DIST_FILE."
2762
2863 - name : Commit and push dist.user.js
2964 run : |
3065 git config --local user.email "[email protected] " 3166 git config --local user.name "hoothin-update"
3267 git add "Picviewer CE+/dist.user.js"
33- git commit -m "chore(Picviewer CE+): Auto-generate dist.user.js with timestamp"
34- git push https://${{ secrets.ACTION_SECRET }}@github.com/${{ github.repository }}
68+ if git diff --staged --quiet; then
69+ echo "No changes to commit."
70+ else
71+ git commit -m "chore(Picviewer CE+): Auto-generate and minify dist.user.js"
72+ git push https://${{ secrets.ACTION_SECRET }}@github.com/${{ github.repository }}
73+ fi
74+
0 commit comments