Add a script that inserts strict type declarations (#7716)

* Add a scripts that adds strict type declarations

Checks all php files in a given directory for existing strict type
declartions, and if it doesn't find one, adds it.

* add strict types on deploy

* add missing quotes

* Added npm command to trigger strict type additions and passed 'last deployed hash' to get changed themes

* fixed bash command and added error catching so errors won't stop deployment

* Add condition for debug output

---------

Co-authored-by: Jason Crist <jcrist@pbking.com>
This commit is contained in:
Vicente Canales 2024-04-15 15:57:02 -05:00 committed by GitHub
parent 3f3fb4de97
commit 419613f85f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 3 deletions

27
add-strict-types.sh Executable file
View file

@ -0,0 +1,27 @@
#!/bin/bash
# Verify an argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <directory>"
echo "Error: Directory argument missing."
exit 1
fi
# Directory containing PHP files, passed as an argument
DIRECTORY="$1"
# Loop over each PHP file in the specified directory
find "$DIRECTORY" -type f -name "*.php" | while read -r file; do
[ -n "$DEBUG" ] && echo "Processing file: $file"
# Check if the file contains the strict_types declaration
if ! grep -qE 'declare\s*\(\s*strict_types\s*=\s*1\s*\)\s*;' "$file"; then
[ -n "$DEBUG" ] && echo "Declaration not found in: $file"
# If not, prepend the strict_types declaration
{
echo '<?php declare( strict_types = 1 ); ?>'
cat "$file"
} > "$file.tmp" && mv "$file.tmp" "$file"
else
[ -n "$DEBUG" ] && echo "Declaration found in: $file"
fi
done

View file

@ -22,6 +22,7 @@
"deploy:theme": "node ./theme-utils.mjs deploy-theme", "deploy:theme": "node ./theme-utils.mjs deploy-theme",
"deploy:zip": "node ./theme-utils.mjs build-com-zip", "deploy:zip": "node ./theme-utils.mjs build-com-zip",
"deploy:land": "node ./theme-utils.mjs land-diff", "deploy:land": "node ./theme-utils.mjs land-diff",
"deploy:add-strict-typing": "node ./theme-utils.mjs add-strict-typing",
"pull:all": "node ./theme-utils.mjs pull-all-themes", "pull:all": "node ./theme-utils.mjs pull-all-themes",
"core:pull": "node ./theme-utils.mjs pull-core-themes", "core:pull": "node ./theme-utils.mjs pull-core-themes",
"core:push": "node ./theme-utils.mjs push-core-themes", "core:push": "node ./theme-utils.mjs push-core-themes",

View file

@ -65,6 +65,10 @@ const commands = {
additionalArgs: '<array of theme slugs>', additionalArgs: '<array of theme slugs>',
run: (args) => deployThemes(args?.[1].split(/[ ,]+/)) run: (args) => deployThemes(args?.[1].split(/[ ,]+/))
}, },
"add-strict-typing": {
helpText: 'Adds strict typing to any changed themes.',
run: () => addStrictTypesToChangedThemes()
},
"build-com-zip": { "build-com-zip": {
helpText: 'Build the production zip file for the specified theme.', helpText: 'Build the production zip file for the specified theme.',
additionalArgs: '<theme-slug>', additionalArgs: '<theme-slug>',
@ -205,6 +209,20 @@ async function deployPreview() {
console.log(`\n\nCommit log of changes to be deployed:\n\n${logs}\n\n`); console.log(`\n\nCommit log of changes to be deployed:\n\n${logs}\n\n`);
} }
async function addStrictTypesToChangedThemes() {
let hash = await getLastDeployedHash();
const changedThemes = await getChangedThemes(hash);
for (let theme of changedThemes) {
await executeCommand(`
bash -c "./add-strict-types.sh ${theme}"
`, true)
.catch((err) => {
console.log(`Error adding strict types to ${theme}: ${err}`);
});
}
}
/* /*
Execute the first phase of a deployment. Execute the first phase of a deployment.
* Gets the last deployed hash from the sandbox * Gets the last deployed hash from the sandbox
@ -240,8 +258,9 @@ async function pushButtonDeploy() {
try { try {
await cleanSandbox(); await cleanSandbox();
let hash = await getLastDeployedHash(); const hash = await getLastDeployedHash();
let thingsWentBump = await versionBumpThemes(); await addStrictTypesToChangedThemes();
const thingsWentBump = await versionBumpThemes();
if (thingsWentBump) { if (thingsWentBump) {
prompt = await inquirer.prompt([{ prompt = await inquirer.prompt([{
@ -257,7 +276,7 @@ async function pushButtonDeploy() {
} }
} }
let changedThemes = await getChangedThemes(hash); const changedThemes = await getChangedThemes(hash);
if (!changedThemes.length) { if (!changedThemes.length) {
console.log(`\n\nEverything is upto date. Nothing new to deploy.\n\n`); console.log(`\n\nEverything is upto date. Nothing new to deploy.\n\n`);