Last active
November 7, 2023 17:51
-
-
Save fsgreco/1ea27b503008baa44c47691ceea79713 to your computer and use it in GitHub Desktop.
A vanilla nodeJs script to fetch list of active plugins from wordpress production environment, and subsequently create a set of wp-cli commands to install them in localhost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* MIT © Santiago Greco - fsgreco@hey.com | |
* This script fetches a list of active plugins from your production wordpress environment. | |
* The goal is to quickly install the same plugins on local environment (matching their version numbers). | |
* How it works: | |
* Once it retrieves the list it creates a second script (in bash) ready to run: `install-plugins.sh` | |
* The script generated consist on a set of instructions to install everything using wp-cli. | |
* This second script can be modified or adapted. | |
* For more context and information please consult the documentation of the entire project: | |
* `docker-wordpress` at https://github.com/fsgreco/docker-wordpress#sync-plugins-matching-production-environment | |
*/ | |
/* Native requirements (there's no need to install anything via npm) */ | |
const https = require('https') | |
const fs = require('fs') | |
/* SET YOUR CREDENTIALS */ | |
let hostname = 'your-website-hostname.com' // do not write the protocol ('http://') | |
let username = 'admin' | |
let appPassword = 'rXe3 sjask ecc... ecc..' | |
let headers = { | |
'Authorization': `Basic ${Buffer.from(`${username}:${appPassword}`).toString('base64')}` | |
} | |
/* MAKE THE REQUEST TO WP REST API, FETCH THE PLUGIN LIST AND GENERATE THE SECOND SCRIPT */ | |
const request = https.request({ hostname, path:'/wp-json/wp/v2/plugins', port: 443, method: 'GET', rejectUnauthorized: false, headers }, response => { | |
response.setEncoding('utf8'); | |
let returnData = ''; | |
response.on('data', (chunk) => { | |
returnData += chunk; | |
}) | |
response.on('end', () => { | |
let parsedResponse = JSON.parse(returnData); | |
let pluginList = parsedResponse.map( ({plugin,version,status, name}) => ({ plugin,version,status,name })) | |
let onlyActivePlugins = pluginList.filter( plgin => plgin.status === 'active') | |
const commands = onlyActivePlugins.map(pgin => { | |
let pluginSlug = pgin.plugin.replace(/(.*)(\/)(.*)/,'$1') | |
return `wp plugin install ${pluginSlug} --version=${pgin.version} --activate \n` | |
}).join('') | |
const script = '#!/usr/bin/env bash \nsleep 5; \n\n' + commands | |
fs.writeFileSync('install-plugins.sh', script) | |
fs.chmodSync('install-plugins.sh', 0o775 ) | |
}); | |
response.on('error', (error) => { | |
throw error; | |
}); | |
}) | |
request.end() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment