Browse Source

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>
Vicente Canales 1 year ago
parent
commit
419613f85f
3 changed files with 50 additions and 3 deletions
  1. 27 0
      add-strict-types.sh
  2. 1 0
      package.json
  3. 22 3
      theme-utils.mjs

+ 27 - 0
add-strict-types.sh

@@ -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 - 0
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",

+ 22 - 3
theme-utils.mjs

@@ -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>',
@@ -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`);