Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding a troubleshooting message here, something like:

"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."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added with 9aba910

else
echo "All .js files within $base_dir have corresponding .min.js files."
exit 0
fi
Loading