I’m working on a project, a WordPress plugin for the Divi theme, with the following file structure:
- root
- gulpfile.js
- package.json
- /src/divi4
- gulpfile.js
- package.json
- /src/divi5
- gulpfile.js
- package.json
- /src/utils
- /src/some_other_stuff
- /target/my-plugin
I use the gulpfile in the root directory to copy various php files from /src/utils and other subfolders to /target/my-plugin. I also minify some JS and CSS files and use the root scripts to zip the finished build.
Additionally, I have a gulpfile in /src/divi4 to build some legacy code for the Divi 4 theme. This also includes copying files to /target and minify JS and CSS. Besides that, I also run a Webpack build, based on some legacy scripts. The package.json containing the build scripts looks like this:
"build": "npm-run-all --parallel build:*",
"build:webpack": "node my-build-scripts/build.js",
"build:gulp": "gulp build"
The build scripts came from a Elegant Themes, the creator of the Divi theme and they require node 14 in order to work.
Elegant Themes now is working on Divi 5, where they completely redesigned their APIs and they want us to build Divi 5 modules using node 18. Therefore I use nvm
to switch to node 18, cd into /src/divi5 and run the node build there, which does more or less the same as the other parts of the build: copy php files, minify JS and CSS and run a webpack build.
Instead of having to run multiple commands in 3 separate folders for the finished zip file of my plugin to contain all required files like:
# build legacy Divi 4 modules:
cd src/divi4
nvm use 14
yarn install
yarn build
# build new Divi 5 modules
cd ../divi5
nvm use 18
yarn install
yarn build
# build the rest of the plugin and create the zip file
cd ../..
yarn install
yarn build
I would like to only have to run 1 single yarn install && yarn build
command in the root directory of my git repository. The Divi 4 and Divi 5 builds are independent from each other so they could theoretically run in parallel whereas the build in the root directory has to wait in order to get all files when creating the zip file.
Question: Do you know a way to trigger the Divi 4 and 5 builds from the root directory of my git plugin?
I’ve tried the following code in my root/gulpfile.js and wrapped the command [ -5 "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && cd src/divi4 && nvm use 14 && yarn install && yarn build
inside the function but some of our devs use Windows and though the command seemed to work on macOS, it didn’t work on Windows and I’m not sure if this would be the way to go. I hope you can give me a hint to a solution which works on every OS.
const { exec } = require('child_process');
function runCommand(command, callback) {
exec(command, (err, stdout, stderr) => {
if (err) {
console.log(stdout);
console.error(`Error: ${stderr}`);
callback(err);
} else {
console.log(stdout);
callback();
}
});
}