Skip to content

Commit 61f976e

Browse files
authored
Ensure that the build has min.js files before generating the .zip file (#2149)
* Ensure that the build has min.js files before generating the .zip file * improve error message
1 parent f13b5c2 commit 61f976e

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"scripts": {
3434
"build:dev": "npm run build:js && npm run build:css",
3535
"build": "npm run build:js && npm run build:css && npm run makepot",
36-
"postbuild": "npm run -s archive",
36+
"postbuild": "npm run validate-build && npm run -s archive",
3737
"archive": "rm -rf $npm_package_name && composer archive --file=$npm_package_name --format=zip",
3838
"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",
3939
"prebuild:js": "rm -f $npm_package_assets_js_min",
@@ -58,7 +58,8 @@
5858
"lint:js-fix": "eslint assets/js --ext=js,jsx,ts,tsx --fix",
5959
"lint:php": "composer run-script phpcs ./inc",
6060
"wp-env": "wp-env",
61-
"test:e2e": "npm run wp-env run tests-cli 'wp theme activate storefront' && cross-env wp-scripts test-e2e"
61+
"test:e2e": "npm run wp-env run tests-cli 'wp theme activate storefront' && cross-env wp-scripts test-e2e",
62+
"validate-build": "./validate-build.sh"
6263
},
6364
"jest": {
6465
"preset": "jest-puppeteer",

validate-build.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
# Initialize the error flag
4+
error_flag=0
5+
6+
# Define the base directory to check
7+
base_dir="./assets"
8+
9+
# Check if the base directory exists
10+
if [ ! -d "$base_dir" ]; then
11+
echo "Error: The directory $base_dir does not exist."
12+
exit 1
13+
fi
14+
15+
# Function to check for corresponding .min.js file
16+
check_for_min_js() {
17+
local js_file="$1"
18+
local min_js_file="${js_file%.js}.min.js"
19+
if [ ! -f "$min_js_file" ]; then
20+
echo "Error: No corresponding .min.js file found for $js_file"
21+
return 1
22+
fi
23+
return 0
24+
}
25+
26+
# Find all .js files within the base directory (excluding .min.js files)
27+
while IFS= read -r -d '' js_file; do
28+
if ! check_for_min_js "$js_file"; then
29+
error_flag=1
30+
fi
31+
done < <(find "$base_dir" -type f -name "*.js" ! -name "*.min.js" -print0)
32+
33+
# Exit with an error code if any .js file is missing its corresponding .min.js file
34+
if [ "$error_flag" -ne 0 ]; then
35+
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."
36+
exit 1
37+
else
38+
echo "All .js files within $base_dir have corresponding .min.js files."
39+
exit 0
40+
fi

0 commit comments

Comments
 (0)