Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post-install commands #12

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
};