@@ -20,18 +20,57 @@ if [ ! -f "$TAR_FILE" ]; then
2020 exit 1
2121fi
2222
23+ if [ ! -r " $TAR_FILE " ]; then
24+ echo " Error: Tar file $TAR_FILE is not readable"
25+ exit 1
26+ fi
27+
2328if [ ! -d " $TARGET_DIR " ]; then
2429 echo " Error: Target directory $TARGET_DIR does not exist"
2530 exit 1
2631fi
2732
28- # Extract tar archive
29- tar zxvvf " $TAR_FILE " -C " $( dirname " $TARGET_DIR " ) "
33+ # Verify tar file is valid before proceeding
34+ if ! tar -tzf " $TAR_FILE " > /dev/null 2>&1 ; then
35+ echo " Error: Tar file $TAR_FILE is corrupted or invalid"
36+ exit 1
37+ fi
38+
39+ # Determine how many leading components to strip from the tarball
40+ # We count the number of directory separators in the first entry
41+ # e.g., "vue/v2/" -> 2 slashes -> strip 2, "react/" -> 1 slash -> strip 1
42+ # Use a temporary file to avoid broken pipe errors when piping tar output
43+ TEMP_LIST=$( mktemp)
44+ trap " rm -f $TEMP_LIST " EXIT
45+ if ! tar -tf " $TAR_FILE " > " $TEMP_LIST " 2> /dev/null; then
46+ echo " Error: Could not read tar file $TAR_FILE "
47+ exit 1
48+ fi
49+ FIRST_ENTRY=$( head -n 1 " $TEMP_LIST " )
50+ if [ -z " $FIRST_ENTRY " ]; then
51+ echo " Error: Tar file $TAR_FILE appears to be empty"
52+ exit 1
53+ fi
54+ STRIP_COMPONENTS=$( echo " $FIRST_ENTRY " | tr -cd ' /' | wc -c)
55+ rm -f " $TEMP_LIST "
56+ trap - EXIT
57+
58+ # Clean up target directory except for .git, node_modules, and CHANGELOG.md to ensure a clean sync
59+ # CHANGELOG.md is preserved because it's generated by semantic-release, not included in the tarball
60+ # This allows 'git add -A' to detect deletions
61+ find " $TARGET_DIR " -mindepth 1 -maxdepth 1 ! -name " .git" ! -name " node_modules" ! -name " CHANGELOG.md" -exec rm -rf {} +
62+
63+ # Extract tar archive directly into the target directory, stripping the prefix
64+ # Use explicit error handling to catch any extraction failures
65+ if ! tar zxf " $TAR_FILE " -C " $TARGET_DIR " --strip-components=" ${STRIP_COMPONENTS// / } " ; then
66+ echo " Error: Failed to extract tar file $TAR_FILE to $TARGET_DIR "
67+ exit 1
68+ fi
3069
3170# Add all changes and commit
3271cd " $TARGET_DIR "
3372git add -A
34- if git commit -m " chore : sync with web-shim-codegen v$VERSION " ; then
73+ if git commit -m " build : sync with web-shim-codegen v$VERSION " ; then
3574 echo " $REPO_NAME repository has been updated"
3675else
3776 echo " $REPO_NAME repository has NOT been updated, no changes to commit"
0 commit comments