From 644a4728f93f201ed0fbdadbb3efd7c947145553 Mon Sep 17 00:00:00 2001 From: Erik Golinelli Date: Fri, 1 Dec 2023 20:47:00 +0100 Subject: [PATCH 1/2] Add post-install commands to install.js Added functionality to handle and run post-install commands after the packages installation. This includes the extraction of 'postInstall' from the config data, running post-install commands concurrently after plugins and themes installation, and a new method 'runPostInstallCommands' for executing these commands. This allows users to perform necessary actions automatically after the package installation process. close #5 --- lib/install.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/install.js b/lib/install.js index dc2b5b8..e16b41b 100644 --- a/lib/install.js +++ b/lib/install.js @@ -26,7 +26,7 @@ class WordPressInstaller { * @return {Promise} A promise that resolves when all the packages are installed. */ async installPackages () { - const { wordpress, plugins, themes } = this.config; + const { wordpress, plugins, themes, postInstall } = this.config; // Create temp folder makeDir(this.tempDir); @@ -53,6 +53,39 @@ class WordPressInstaller { const themePackages = themes.map((theme) => new ThemePackage(theme, { ...defaultPaths, destFolder: this.themeFolder })); await Promise.all(themePackages.map((themePackage) => themePackage.install())); } + + // Install plugins and themes concurrently + await Promise.all(promises); + + // Run post-install commands + if (postInstall && postInstall.length > 0) { + console.log('🤖 Executing post-install commands...'); + await this.runPostInstallCommands(postInstall); + } + } + + /** + * Runs post-install commands asynchronously. + * + * @param {Array} commands - An array of WP-CLI commands to execute. + * @return {Promise} - A promise that resolves when the post-install commands complete. + */ + async runPostInstallCommands(commands) { + // Execute each post-install command + for (const command of commands) { + try { + console.log(`Executing: ${command}`); + const { stdout, stderr } = await exec(command); + if (stdout) { + console.log(`Command output:\n${stdout}`); + } + if (stderr) { + console.error(`Command error:\n${stderr}`); + } + } catch (error) { + console.error(`Error executing command: ${command}`, error); + } + } } /** From b573e791232df7b9f434144217607945a1137d6c Mon Sep 17 00:00:00 2001 From: Erik Golinelli Date: Fri, 1 Dec 2023 20:55:40 +0100 Subject: [PATCH 2/2] Add WP-CLI availability check before post-install commands Added a function `isWPCLIAvailable` in `utils.js` to check if WP-CLI (WordPress Command Line Interface) is available on the system. This function is then used in `install.js` before running post-install commands. This prevents errors and provides a better user experience if WP-CLI is not installed and a post-install command depends on it. --- lib/install.js | 7 ++----- lib/utils.js | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/install.js b/lib/install.js index e16b41b..69e99e6 100644 --- a/lib/install.js +++ b/lib/install.js @@ -1,5 +1,5 @@ const path = require('path'); -const { cleanup, makeDir } = require('./utils'); +const { cleanup, makeDir, isWPCLIAvailable} = require('./utils'); const { WordPressPackage, PluginPackage, ThemePackage } = require('./package'); /** @@ -54,11 +54,8 @@ class WordPressInstaller { await Promise.all(themePackages.map((themePackage) => themePackage.install())); } - // Install plugins and themes concurrently - await Promise.all(promises); - // Run post-install commands - if (postInstall && postInstall.length > 0) { + if (isWPCLIAvailable() && postInstall && postInstall.length > 0) { console.log('🤖 Executing post-install commands...'); await this.runPostInstallCommands(postInstall); } diff --git a/lib/utils.js b/lib/utils.js index 08d9675..38d664a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -286,6 +286,23 @@ function replaceEmptySalts (configContent) { return configContent; } +/** + * Checks if WP-CLI is available. + * + * @return {boolean} - A promise that resolves to true if WP-CLI is available, false otherwise. + */ +async function isWPCLIAvailable () { + try { + // Attempt to execute a simple WP-CLI command + await exec('wp --version'); + return true; // If successful, WP-CLI is available + } catch (error) { + console.log('🔴 WP-CLI is not available on this system. Please install WP-CLI and try again.'); + console.log('Read more about at https://make.wordpress.org/cli/handbook/guides/installing/'); + return false; // If an error occurs, WP-CLI is not available + } +} + module.exports = { getConfig, makeDir, @@ -299,5 +316,6 @@ module.exports = { installComposer, replaceDbConstant, replaceDbConstantBool, - replaceEmptySalts + replaceEmptySalts, + isWPCLIAvailable };