Skip to content

Commit

Permalink
Merge pull request #12 from erikyo/enhancement/post-install
Browse files Browse the repository at this point in the history
Add post-install commands
  • Loading branch information
erikyo authored Dec 1, 2023
2 parents a79fc50 + 6b6f0f7 commit 2f3355f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
34 changes: 32 additions & 2 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -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');

/**
Expand All @@ -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);
Expand Down Expand Up @@ -58,6 +58,36 @@ class WordPressInstaller {

// Install plugins and themes concurrently
await Promise.all(promises);

// Run post-install commands
if (isWPCLIAvailable() && 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<void>} - 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);
}
}
}

/**
Expand Down
20 changes: 19 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,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,
Expand All @@ -298,5 +315,6 @@ module.exports = {
installComposer,
replaceDbConstant,
replaceDbConstantBool,
replaceEmptySalts
replaceEmptySalts,
isWPCLIAvailable
};

0 comments on commit 2f3355f

Please sign in to comment.