Skip to content

Commit

Permalink
Add a script that inserts strict type declarations (#7716)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
vcanales and pbking committed Apr 15, 2024
1 parent 3f3fb4d commit 419613f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
27 changes: 27 additions & 0 deletions add-strict-types.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"deploy:theme": "node ./theme-utils.mjs deploy-theme",
"deploy:zip": "node ./theme-utils.mjs build-com-zip",
"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",
"core:pull": "node ./theme-utils.mjs pull-core-themes",
"core:push": "node ./theme-utils.mjs push-core-themes",
Expand Down
25 changes: 22 additions & 3 deletions theme-utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ const commands = {
additionalArgs: '<array of theme slugs>',
run: (args) => deployThemes(args?.[1].split(/[ ,]+/))
},
"add-strict-typing": {
helpText: 'Adds strict typing to any changed themes.',
run: () => addStrictTypesToChangedThemes()
},
"build-com-zip": {
helpText: 'Build the production zip file for the specified theme.',
additionalArgs: '<theme-slug>',
Expand Down Expand Up @@ -205,6 +209,20 @@ async function deployPreview() {
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.
* Gets the last deployed hash from the sandbox
Expand Down Expand Up @@ -240,8 +258,9 @@ async function pushButtonDeploy() {
try {
await cleanSandbox();

let hash = await getLastDeployedHash();
let thingsWentBump = await versionBumpThemes();
const hash = await getLastDeployedHash();
await addStrictTypesToChangedThemes();
const thingsWentBump = await versionBumpThemes();

if (thingsWentBump) {
prompt = await inquirer.prompt([{
Expand All @@ -257,7 +276,7 @@ async function pushButtonDeploy() {
}
}

let changedThemes = await getChangedThemes(hash);
const changedThemes = await getChangedThemes(hash);

if (!changedThemes.length) {
console.log(`\n\nEverything is upto date. Nothing new to deploy.\n\n`);
Expand Down

0 comments on commit 419613f

Please sign in to comment.