diff --git a/add-strict-types.sh b/add-strict-types.sh new file mode 100755 index 000000000..b4420a9b6 --- /dev/null +++ b/add-strict-types.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Verify an argument is provided +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + 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 '' + cat "$file" + } > "$file.tmp" && mv "$file.tmp" "$file" + else + [ -n "$DEBUG" ] && echo "Declaration found in: $file" + fi +done diff --git a/package.json b/package.json index a2c6895e0..4d2be0a06 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/theme-utils.mjs b/theme-utils.mjs index afd8479f9..72197813c 100644 --- a/theme-utils.mjs +++ b/theme-utils.mjs @@ -65,6 +65,10 @@ const commands = { additionalArgs: '', 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: '', @@ -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 @@ -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([{ @@ -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`);