Skip to content

Commit

Permalink
Ensure that the build has min.js files before generating the .zip file (
Browse files Browse the repository at this point in the history
#2149)

* Ensure that the build has min.js files before generating the .zip file

* improve error message
  • Loading branch information
gigitux authored May 17, 2024
1 parent f13b5c2 commit 61f976e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"scripts": {
"build:dev": "npm run build:js && npm run build:css",
"build": "npm run build:js && npm run build:css && npm run makepot",
"postbuild": "npm run -s archive",
"postbuild": "npm run validate-build && npm run -s archive",
"archive": "rm -rf $npm_package_name && composer archive --file=$npm_package_name --format=zip",
"postarchive": "rm -rf $npm_package_name && unzip $npm_package_name.zip -d $npm_package_name && rm $npm_package_name.zip && zip -r $npm_package_name.zip $npm_package_name && rm -rf $npm_package_name",
"prebuild:js": "rm -f $npm_package_assets_js_min",
Expand All @@ -58,7 +58,8 @@
"lint:js-fix": "eslint assets/js --ext=js,jsx,ts,tsx --fix",
"lint:php": "composer run-script phpcs ./inc",
"wp-env": "wp-env",
"test:e2e": "npm run wp-env run tests-cli 'wp theme activate storefront' && cross-env wp-scripts test-e2e"
"test:e2e": "npm run wp-env run tests-cli 'wp theme activate storefront' && cross-env wp-scripts test-e2e",
"validate-build": "./validate-build.sh"
},
"jest": {
"preset": "jest-puppeteer",
Expand Down
40 changes: 40 additions & 0 deletions validate-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

# Initialize the error flag
error_flag=0

# Define the base directory to check
base_dir="./assets"

# Check if the base directory exists
if [ ! -d "$base_dir" ]; then
echo "Error: The directory $base_dir does not exist."
exit 1
fi

# Function to check for corresponding .min.js file
check_for_min_js() {
local js_file="$1"
local min_js_file="${js_file%.js}.min.js"
if [ ! -f "$min_js_file" ]; then
echo "Error: No corresponding .min.js file found for $js_file"
return 1
fi
return 0
}

# Find all .js files within the base directory (excluding .min.js files)
while IFS= read -r -d '' js_file; do
if ! check_for_min_js "$js_file"; then
error_flag=1
fi
done < <(find "$base_dir" -type f -name "*.js" ! -name "*.min.js" -print0)

# Exit with an error code if any .js file is missing its corresponding .min.js file
if [ "$error_flag" -ne 0 ]; then
echo "Error: the ZIP could not be built because minified scripts are missing. Please ensure you are using the correct versions of NPM and Node.js."
exit 1
else
echo "All .js files within $base_dir have corresponding .min.js files."
exit 0
fi

0 comments on commit 61f976e

Please sign in to comment.