diff --git a/lib/modules/createConfig.js b/lib/modules/createConfig.js index 3c6ea83..765509b 100644 --- a/lib/modules/createConfig.js +++ b/lib/modules/createConfig.js @@ -1,5 +1,7 @@ /* eslint-disable global-require, import/no-dynamic-require */ const fs = require('fs-extra'); +const { copyDirSync } = require('../utils/copy'); +const { rmDirSync } = require('../utils/rm'); const path = require('path'); const glob = require('glob'); const { get, defaults } = require('lodash'); @@ -154,9 +156,19 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { htaccessFile = `${htaccessFile}-subfolder`; } + const composerWordpress = path.resolve( CWD, version[0] ); + const vendorSync = `${packageDir}/wordpress`; + if ( fs.existsSync( vendorSync ) ) { + rmDirSync( vendorSync ); + } + if ( fs.existsSync( composerWordpress ) ) { + copyDirSync( composerWordpress, vendorSync ); + } + const usableVersions = fs.existsSync( vendorSync ) ? [ 'wordpress' ] : validVersions; + await renderTemplate(`${packageDir}/lib/templates/dockerfile.ejs`, `${packageDir}/Dockerfile`, { isWpContent: config.wpContent, - versions: validVersions, + versions: usableVersions, vip: config.muPlugins ? config.muPlugins.vip : false, phpVersion: config.phpVersion || 7.4, phpMemoryLimit: config.phpMemoryLimit || '128M', @@ -166,7 +178,7 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { const wpCypressConfig = { ...config, version, - validVersions, + validVersions: usableVersions, volumes, activePlugins, activeTheme, diff --git a/lib/templates/dockerfile.ejs b/lib/templates/dockerfile.ejs index 58c5007..6417a63 100644 --- a/lib/templates/dockerfile.ejs +++ b/lib/templates/dockerfile.ejs @@ -22,15 +22,19 @@ RUN curl -sS https://getcomposer.org/installer --output composer-setup.php \ && php composer-setup.php --version=1.10.16 \ && mv composer.phar /bin/composer -<% versions.forEach((version) => { %> +<% versions.forEach((version) => { + if ( version.match(/(\d+.?)+/) ) { +%> RUN curl https://wordpress.org/wordpress-<%= version %>.tar.gz > wordpress-<%= version %>.tar.gz && \ mkdir -p /var/www/<%= version %> && \ tar -xzf wordpress-<%= version %>.tar.gz -C /var/www/<%= version %> && \ mv /var/www/<%= version %>/wordpress/* /var/www/<%= version %> && \ rm -rf /var/www/<%= version %>/wordpress && \ chown -R www-data:www-data /var/www/<%= version %><% if (isWpContent) { %> && \<% } else { %>;<% } %> - <% if (isWpContent) { %>rm -rf /var/www/<%= version %>/wp-content;<% } %> -<% }); %> + <% if (isWpContent) { %>rm -rf /var/www/<%= version %>/wp-content;<% } %> +<% } else { %> +ADD <%= version %> /var/www/wordpress +<% } }); %> <% if (vip) { %> RUN curl https://github.com/Automattic/vip-go-mu-plugins-built/archive/master.zip -L -o /usr/src/vip-mu-plugins.zip && \ diff --git a/lib/utils/rm.js b/lib/utils/rm.js new file mode 100644 index 0000000..c1285de --- /dev/null +++ b/lib/utils/rm.js @@ -0,0 +1,22 @@ +const fs = require('fs-extra'); +const path = require('path'); +const rmDirSync = (src) => { + if (!fs.lstatSync(src).isDirectory()) { + return; + } + + fs.readdirSync(src) + .forEach((item) => { + const srcItem = path.join(src, item); + + if (fs.lstatSync(srcItem).isDirectory()) { + rmDirSync(srcItem); + } else { + fs.unlinkSync(srcItem); + } + }); + fs.rmdirSync(src); +}; +module.exports = { + rmDirSync, +};