From 14e3cdc9f9014b3f522f7ce2eb404dcbb4e516fc Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Thu, 15 Aug 2024 11:45:21 -0400 Subject: [PATCH 01/16] Remove directory slash detection; properly name templateInitPath variable; remove composer block that wasnt doing anything --- packages/toolkit/scripts/project/init.js | 49 ++++++++---------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/packages/toolkit/scripts/project/init.js b/packages/toolkit/scripts/project/init.js index 1a251123..4d9e1e8e 100755 --- a/packages/toolkit/scripts/project/init.js +++ b/packages/toolkit/scripts/project/init.js @@ -116,10 +116,8 @@ const run = async () => { variables.wordpress_version = await getWordPressLatestVersion(); const toolkitPath = resolve(`${__dirname}/../../`); - const initPath = - projectLayout === 'modern' - ? `${resolve(cliPath)}${path.sep}wordpress${path.sep}wp-content` - : resolve(cliPath); + const templateInitPath = + projectLayout === 'modern' ? `${resolve(cliPath)}/wordpress/wp-content` : resolve(cliPath); template = results.template || template; const projectName = results.name || name; @@ -140,22 +138,22 @@ const run = async () => { const projectNameUppercaseUnderscore = projectName.replace(/ /g, '_').toUpperCase(); // Create dir if it does not exist - if (!fs.existsSync(initPath)) { - fs.mkdirSync(initPath, { recursive: true }); + if (!fs.existsSync(templateInitPath)) { + fs.mkdirSync(templateInitPath, { recursive: true }); } // If template is not empty, git clone template to init_path if (template !== 'none') { // Check if init_path directory is not empty - if (fs.readdirSync(initPath).length > 0) { + if (fs.readdirSync(templateInitPath).length > 0) { console.error( - `Directory ${initPath} is not empty. Please provide an empty directory to initialize the project.`, + `Directory ${templateInitPath} is not empty. Please provide an empty directory to initialize the project.`, ); process.exit(1); } - execSync(`git clone ${template} '${initPath}'`); - fs.rmSync(path.join(initPath, '.git'), { recursive: true }); + execSync(`git clone ${template} '${templateInitPath}'`); + fs.rmSync(path.join(templateInitPath, '.git'), { recursive: true }); } const tenupComposerFiles = []; @@ -180,7 +178,7 @@ const run = async () => { { from: /Tenup Theme/g, to: `${projectName} Theme` }, ]; - const files = await fg(`${initPath}/**/*`, { + const files = await fg(`${templateInitPath}/**/*`, { ignore: ['**/*/node_modules', '**/*/vendor', '**/*/dist'], dot: true, }); @@ -203,25 +201,12 @@ const run = async () => { } }); - const themePath = `${initPath}/themes/${projectNameLowercaseHypen}-theme`; - const pluginPath = `${initPath}/plugins/${projectNameLowercaseHypen}-plugin`; - const muPluginPath = `${initPath}/mu-plugins/${projectNameLowercaseHypen}-plugin`; + const themePath = `${templateInitPath}/themes/${projectNameLowercaseHypen}-theme`; + const pluginPath = `${templateInitPath}/plugins/${projectNameLowercaseHypen}-plugin`; + const muPluginPath = `${templateInitPath}/mu-plugins/${projectNameLowercaseHypen}-plugin`; // Copy contents of toolkitPath/project/local into cliPath execSync(`rsync -rc "${toolkitPath}/project/local/" "${cliPath}"`); - tenupComposerFiles.forEach((file) => { - // skip wp-content building, that's free in the build scripts already - if (path.basename(path.dirname(file)) !== 'wp-content') { - // const workingDir = path.dirname(file).slice(resolve(cliPath).length).replace('//', '/'); - // const command = `composer install --working-dir=.${workingDir}\n`; - - // FIXME: don't actually output the file, it results in brokeness - // fs.appendFileSync( - // `${cliPath}/scripts/build-${path.basename(path.dirname(file))}.sh`, - // command, - // ); - } - }); if (!skipComposer) { tenupComposerFiles.forEach((file) => { @@ -230,11 +215,11 @@ const run = async () => { } const renameDirs = [ - { from: `${initPath}/themes/tenup-theme`, to: themePath }, - { from: `${initPath}/plugins/tenup-plugin`, to: pluginPath }, - { from: `${initPath}/themes/10up-theme`, to: themePath }, - { from: `${initPath}/plugins/10up-plugin`, to: pluginPath }, - { from: `${initPath}/mu-plugins/10up-plugin`, to: muPluginPath }, + { from: `${templateInitPath}/themes/tenup-theme`, to: themePath }, + { from: `${templateInitPath}/plugins/tenup-plugin`, to: pluginPath }, + { from: `${templateInitPath}/themes/10up-theme`, to: themePath }, + { from: `${templateInitPath}/plugins/10up-plugin`, to: pluginPath }, + { from: `${templateInitPath}/mu-plugins/10up-plugin`, to: muPluginPath }, ]; renameDirs.forEach((dir) => { From 71547ffec7912570a9eb3c5bdbe9c7ec61430b88 Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Thu, 15 Aug 2024 16:02:50 -0400 Subject: [PATCH 02/16] Change standard layout to wpparent; remove create-payload and support a option in the build command that lets you specify --- packages/toolkit/PROJECTS.md | 1 - .../toolkit/project/default-variables.json | 1 - .../toolkit/project/gitlab/.gitlab-ci.tmpl | 2 +- .../scripts/project/bash/build-setup.sh | 16 +++---- .../scripts/project/bash/create-payload.sh | 20 --------- packages/toolkit/scripts/project/build.js | 14 ++++-- .../toolkit/scripts/project/create-payload.js | 44 ------------------- packages/toolkit/scripts/project/init.js | 23 ++-------- packages/toolkit/utils/project.js | 10 ----- 9 files changed, 24 insertions(+), 107 deletions(-) delete mode 100644 packages/toolkit/scripts/project/bash/create-payload.sh delete mode 100644 packages/toolkit/scripts/project/create-payload.js diff --git a/packages/toolkit/PROJECTS.md b/packages/toolkit/PROJECTS.md index 34461980..2c2de847 100644 --- a/packages/toolkit/PROJECTS.md +++ b/packages/toolkit/PROJECTS.md @@ -63,7 +63,6 @@ The following are additional optional variables that allow you to use custom scr ```yaml deploy_script_path: "" # Custom deploy script build_script_path: "" # For using a build script in a different location -create_payload_script_path: "" # Custom create payload script ``` ## Commands diff --git a/packages/toolkit/project/default-variables.json b/packages/toolkit/project/default-variables.json index b1b1a4a7..f810b37f 100644 --- a/packages/toolkit/project/default-variables.json +++ b/packages/toolkit/project/default-variables.json @@ -4,6 +4,5 @@ "build_script_path": "", "wordpress_version": "", "deploy_script_path": "", - "create_payload_script_path": "", "environments": [] } diff --git a/packages/toolkit/project/gitlab/.gitlab-ci.tmpl b/packages/toolkit/project/gitlab/.gitlab-ci.tmpl index 4057be5d..2f8bc3e1 100644 --- a/packages/toolkit/project/gitlab/.gitlab-ci.tmpl +++ b/packages/toolkit/project/gitlab/.gitlab-ci.tmpl @@ -25,7 +25,7 @@ build_plugins_and_themes: stage: build script: - nvm install 18 - - npx 10up-toolkit project create-payload + - npx 10up-toolkit project build --type=full artifacts: paths: - payload diff --git a/packages/toolkit/scripts/project/bash/build-setup.sh b/packages/toolkit/scripts/project/bash/build-setup.sh index 4a59c283..b91b8832 100644 --- a/packages/toolkit/scripts/project/bash/build-setup.sh +++ b/packages/toolkit/scripts/project/bash/build-setup.sh @@ -2,12 +2,12 @@ SHARE_DIR="$(dirname "$(realpath "$0")")" # Various tasks to determine some things like what kind of project is this -# such as standard, wp-content rooted...something else? +# such as wpparent, wp-content rooted...something else? function build:preflight { - PROJECT_TYPE="standard" - # Check for a default, standard layout that has a wordpress directory - if [ -d wordpress ] && [ -d build ]; then # this is probably a standard setup - echo "Detected standard WordPress repository layout" + PROJECT_TYPE="wpparent" + # Check for a parent layout that has a wordpress directory + if [ -d wordpress ] && [ -d build ]; then # this is probably a wpparent setup + echo "Detected parent WordPress repository layout" WORDPRESS_BUILD_ROOT="wordpress/wp-content" return @@ -61,7 +61,7 @@ function build:install { local WORDPRESS_VERSION=$(build:version) echo "Installing WordPress version: ${WORDPRESS_VERSION}" - if [ ${PROJECT_TYPE} = "standard" ]; then + if [ ${PROJECT_TYPE} = "wpparent" ]; then mkdir -p wordpress/wp-content pushd wordpress else @@ -88,7 +88,7 @@ function build:main { # but you are free to modify it as required for your project. Remember, you can also # drop in any number of scripts and they will be run in alphabetical order AFTER main.sh - # detect if this is a standard layout or not + # detect if this is a wpparent layout or not if [ -d wordpress/wp-content ]; then pushd wordpress/wp-content @@ -220,7 +220,7 @@ function build:package { fi if [ ! -d payload ]; then - echo "No payload directory found. Please run 10up-toolkit project create-payload first." + echo "No payload directory found. Please run 10up-toolkit project build --type=full first." exit 1 fi diff --git a/packages/toolkit/scripts/project/bash/create-payload.sh b/packages/toolkit/scripts/project/bash/create-payload.sh deleted file mode 100644 index 075f37e0..00000000 --- a/packages/toolkit/scripts/project/bash/create-payload.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -download() { - if [ `which curl` ]; then - curl -s "$1" > "$2"; - elif [ `which wget` ]; then - wget -nv -O "$2" "$1" - fi -} - -rm -rf payload -mkdir payload -cd payload - -echo "Downloading WordPress $WORDPRESS_VERSION..." -download https://wordpress.org/wordpress-${WORDPRESS_VERSION}.tar.gz wordpress.tar.gz -tar --strip-components=1 -zxmf wordpress.tar.gz -C . -rm wordpress.tar.gz - -rsync -avz --exclude-from=$rsync_file_excludes_absolute $project_root/$deploy_from $deploy_to_subdir diff --git a/packages/toolkit/scripts/project/build.js b/packages/toolkit/scripts/project/build.js index 928a75a1..8322ec6b 100644 --- a/packages/toolkit/scripts/project/build.js +++ b/packages/toolkit/scripts/project/build.js @@ -4,9 +4,17 @@ const chalk = require('chalk'); const { log } = console; const fs = require('fs'); -const { getProjectRoot, getProjectVariables, setEnvVariables } = require('../../utils'); +const { + getProjectRoot, + getProjectVariables, + setEnvVariables, + hasArgInCLI, + getArgFromCLI, +} = require('../../utils'); -const description = '10up-toolkit project build'; +const buildType = hasArgInCLI('--type') ? getArgFromCLI('--type') : 'local'; + +const description = '10up-toolkit project build [--type=]'; const run = async () => { const root = getProjectRoot(); @@ -27,7 +35,7 @@ const run = async () => { setEnvVariables(variables); if (fs.existsSync(variables.build_script_path)) { - execSync(`bash -l ${__dirname}/bash/build-setup.sh local`, { + execSync(`bash -l ${__dirname}/bash/build-setup.sh ${buildType}`, { stdio: 'inherit', }); } else { diff --git a/packages/toolkit/scripts/project/create-payload.js b/packages/toolkit/scripts/project/create-payload.js deleted file mode 100644 index a410009e..00000000 --- a/packages/toolkit/scripts/project/create-payload.js +++ /dev/null @@ -1,44 +0,0 @@ -const { execSync } = require('child_process'); -const chalk = require('chalk'); - -const { log } = console; - -const fs = require('fs'); -const { getProjectRoot, getProjectVariables, setEnvVariables } = require('../../utils'); - -const description = '10up-toolkit project create-payload'; - -const run = async () => { - const root = getProjectRoot(); - - if (!root) { - log(chalk.red('This is not a project.')); - process.exit(1); - } - - // combine project variables with actual environment variables - const variables = { ...getProjectVariables(), ...process.env }; - - // FIXME: This is a hack to force "create-payload" to behave like ci - variables.CI = true; - - if (!variables) { - log(chalk.red('No .tenup.yml found.')); - process.exit(1); - } - - setEnvVariables(variables); - - if (fs.existsSync(variables.build_script_path)) { - execSync(`bash -l ${__dirname}/bash/build-setup.sh full`, { - stdio: 'inherit', - }); - } else { - log(chalk.red('No build script found.')); - process.exit(1); - } - - log(chalk.green('Build complete.')); -}; - -module.exports = { run, description }; diff --git a/packages/toolkit/scripts/project/init.js b/packages/toolkit/scripts/project/init.js index b96bc4a4..b900dc77 100755 --- a/packages/toolkit/scripts/project/init.js +++ b/packages/toolkit/scripts/project/init.js @@ -14,7 +14,7 @@ const { getArgFromCLI, hasArgInCLI } = require('../../utils'); const cliPath = hasArgInCLI('--path') ? getArgFromCLI('--path') : '.'; -const projectLayout = hasArgInCLI('--layout') ? getArgFromCLI('--layout') : 'common'; +const projectLayout = hasArgInCLI('--layout') ? getArgFromCLI('--layout') : 'wpcontent'; const name = hasArgInCLI('--name') ? getArgFromCLI('--name') : ''; @@ -48,23 +48,6 @@ const run = async () => { } } - // we purposely do not actually expose projectLayout as a question for now and leave it as an advanced, semi-hidden option - /* - if (projectLayout === 'common') { - questions.push({ - type: 'input', - name: 'layout', - validate: (input) => { - if (!['common','monorepo'].includes(input)) { - return false; - } - return true; - }, - message: 'Project Layout (common or monorepo):', - }); - } - */ - if (!name) { questions.push({ type: 'input', @@ -117,7 +100,9 @@ const run = async () => { const toolkitPath = resolve(`${__dirname}/../../`); const templateInitPath = - projectLayout === 'modern' ? `${resolve(cliPath)}/wordpress/wp-content` : resolve(cliPath); + projectLayout === 'wpcontent' + ? `${resolve(cliPath)}/wordpress/wp-content` + : resolve(cliPath); template = results.template || template; const projectName = results.name || name; diff --git a/packages/toolkit/utils/project.js b/packages/toolkit/utils/project.js index 3df221b1..6f539f7f 100644 --- a/packages/toolkit/utils/project.js +++ b/packages/toolkit/utils/project.js @@ -120,16 +120,6 @@ const getProjectVariables = (path = '.') => { data.project_root = projectRoot; - if (data.create_payload_script_path) { - data.create_payload_script_path = resolve( - `${projectRoot}/${data.create_payload_script_path}`, - ); - } else { - data.create_payload_script_path = resolve( - `${__dirname}/../scripts/project/bash/create-payload.sh`, - ); - } - if (data.build_script_path) { data.build_script_path = resolve(`${projectRoot}/${data.build_script_path}`); } else { From 53698e1162260eb07bdcb7fe49066a3d7550ec05 Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Thu, 15 Aug 2024 16:03:22 -0400 Subject: [PATCH 03/16] Fix typo --- packages/toolkit/utils/project.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/toolkit/utils/project.js b/packages/toolkit/utils/project.js index 6f539f7f..781d680c 100644 --- a/packages/toolkit/utils/project.js +++ b/packages/toolkit/utils/project.js @@ -126,8 +126,8 @@ const getProjectVariables = (path = '.') => { data.build_script_path = `${projectRoot}/scripts/build.sh`; } - data.rsync_file_excludes = `./scripts/rsync-excludess.txt`; - data.rsync_file_excludes_absolute = `${projectRoot}/scripts/rsync-excludess.txt`; + data.rsync_file_excludes = `./scripts/rsync-excludes.txt`; + data.rsync_file_excludes_absolute = `${projectRoot}/scripts/rsync-excludes.txt`; data.toolkit_path = resolve(`${__dirname}/../`); From 81dc4c4b1bfcc206bf532f8a3a0dd4a2a8d5b43e Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Thu, 15 Aug 2024 16:18:12 -0400 Subject: [PATCH 04/16] Use wpparent --- packages/toolkit/scripts/project/bash/build-setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/toolkit/scripts/project/bash/build-setup.sh b/packages/toolkit/scripts/project/bash/build-setup.sh index b91b8832..21da5e7e 100644 --- a/packages/toolkit/scripts/project/bash/build-setup.sh +++ b/packages/toolkit/scripts/project/bash/build-setup.sh @@ -7,7 +7,7 @@ function build:preflight { PROJECT_TYPE="wpparent" # Check for a parent layout that has a wordpress directory if [ -d wordpress ] && [ -d build ]; then # this is probably a wpparent setup - echo "Detected parent WordPress repository layout" + echo "Detected wpparent WordPress repository layout" WORDPRESS_BUILD_ROOT="wordpress/wp-content" return From 2741ae77ce0674b1c829f6e01deff7346d268f47 Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Thu, 15 Aug 2024 16:21:07 -0400 Subject: [PATCH 05/16] Remove build_script_path as it's not needed anymore --- packages/toolkit/PROJECTS.md | 1 - packages/toolkit/project/default-variables.json | 1 - packages/toolkit/scripts/project/build.js | 11 +++-------- packages/toolkit/scripts/project/package.js | 11 +++-------- packages/toolkit/scripts/project/update-composer.js | 11 +++-------- packages/toolkit/utils/project.js | 6 ------ 6 files changed, 9 insertions(+), 32 deletions(-) diff --git a/packages/toolkit/PROJECTS.md b/packages/toolkit/PROJECTS.md index 2c2de847..8b19bd81 100644 --- a/packages/toolkit/PROJECTS.md +++ b/packages/toolkit/PROJECTS.md @@ -62,7 +62,6 @@ The following are additional optional variables that allow you to use custom scr ```yaml deploy_script_path: "" # Custom deploy script -build_script_path: "" # For using a build script in a different location ``` ## Commands diff --git a/packages/toolkit/project/default-variables.json b/packages/toolkit/project/default-variables.json index f810b37f..ed35d010 100644 --- a/packages/toolkit/project/default-variables.json +++ b/packages/toolkit/project/default-variables.json @@ -1,7 +1,6 @@ { "project_name": "10up Project", "php_version": "8.2", - "build_script_path": "", "wordpress_version": "", "deploy_script_path": "", "environments": [] diff --git a/packages/toolkit/scripts/project/build.js b/packages/toolkit/scripts/project/build.js index 8322ec6b..d3f2dea9 100644 --- a/packages/toolkit/scripts/project/build.js +++ b/packages/toolkit/scripts/project/build.js @@ -34,14 +34,9 @@ const run = async () => { setEnvVariables(variables); - if (fs.existsSync(variables.build_script_path)) { - execSync(`bash -l ${__dirname}/bash/build-setup.sh ${buildType}`, { - stdio: 'inherit', - }); - } else { - log(chalk.red('No build script found.')); - process.exit(1); - } + execSync(`bash -l ${__dirname}/bash/build-setup.sh ${buildType}`, { + stdio: 'inherit', + }); log(chalk.green('Build complete.')); }; diff --git a/packages/toolkit/scripts/project/package.js b/packages/toolkit/scripts/project/package.js index 9875f7c1..e16f8ffb 100644 --- a/packages/toolkit/scripts/project/package.js +++ b/packages/toolkit/scripts/project/package.js @@ -26,14 +26,9 @@ const run = async () => { setEnvVariables(variables); - if (fs.existsSync(variables.build_script_path)) { - execSync(`bash -l ${__dirname}/bash/build-setup.sh package`, { - stdio: 'inherit', - }); - } else { - log(chalk.red('No build script found.')); - process.exit(1); - } + execSync(`bash -l ${__dirname}/bash/build-setup.sh package`, { + stdio: 'inherit', + }); log(chalk.green('Packing process complete.')); }; diff --git a/packages/toolkit/scripts/project/update-composer.js b/packages/toolkit/scripts/project/update-composer.js index b0bccbc0..4f351889 100644 --- a/packages/toolkit/scripts/project/update-composer.js +++ b/packages/toolkit/scripts/project/update-composer.js @@ -26,14 +26,9 @@ const run = async () => { setEnvVariables(variables); - if (fs.existsSync(variables.build_script_path)) { - execSync(`bash -l ${__dirname}/bash/build-setup.sh update-composer`, { - stdio: 'inherit', - }); - } else { - log(chalk.red('No build script found.')); - process.exit(1); - } + execSync(`bash -l ${__dirname}/bash/build-setup.sh update-composer`, { + stdio: 'inherit', + }); log(chalk.green('Composer update complete.')); }; diff --git a/packages/toolkit/utils/project.js b/packages/toolkit/utils/project.js index 781d680c..c28ec2f4 100644 --- a/packages/toolkit/utils/project.js +++ b/packages/toolkit/utils/project.js @@ -120,12 +120,6 @@ const getProjectVariables = (path = '.') => { data.project_root = projectRoot; - if (data.build_script_path) { - data.build_script_path = resolve(`${projectRoot}/${data.build_script_path}`); - } else { - data.build_script_path = `${projectRoot}/scripts/build.sh`; - } - data.rsync_file_excludes = `./scripts/rsync-excludes.txt`; data.rsync_file_excludes_absolute = `${projectRoot}/scripts/rsync-excludes.txt`; From e31e26266677d650bcc2ada8421b7a3100afc06d Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Thu, 15 Aug 2024 16:22:06 -0400 Subject: [PATCH 06/16] Remove deploy_script_path --- packages/toolkit/PROJECTS.md | 6 ------ packages/toolkit/project/default-variables.json | 1 - 2 files changed, 7 deletions(-) diff --git a/packages/toolkit/PROJECTS.md b/packages/toolkit/PROJECTS.md index 8b19bd81..52e03990 100644 --- a/packages/toolkit/PROJECTS.md +++ b/packages/toolkit/PROJECTS.md @@ -58,12 +58,6 @@ In this scenario, `deploy_from` would be set to `wp` and `deploy_to_subdir` woul `deploy_type` currently supports `rsync`, `wpe` (WP Engine), and `pantheon`. It defaults to `rsync`. If WPE or Pantheon is choosen, `deploy_to` should contain a Git URL. More deploy types will be added in the future. -The following are additional optional variables that allow you to use custom scripts different than the ones provided by 10up Toolkit. You shouldn't need to use these unless you are doing something super custom. All these paths are relative from the root of your project. - -```yaml -deploy_script_path: "" # Custom deploy script -``` - ## Commands The project subcommand provides a variety of utlities for creating, building, and deploying 10up-specific projects. diff --git a/packages/toolkit/project/default-variables.json b/packages/toolkit/project/default-variables.json index ed35d010..f15098cb 100644 --- a/packages/toolkit/project/default-variables.json +++ b/packages/toolkit/project/default-variables.json @@ -2,6 +2,5 @@ "project_name": "10up Project", "php_version": "8.2", "wordpress_version": "", - "deploy_script_path": "", "environments": [] } From ea8fc46c2727f1b65ea68cbde312df5c44f478a1 Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Thu, 15 Aug 2024 16:25:43 -0400 Subject: [PATCH 07/16] Rename build-setup.sh to scripts.sh --- .../scripts/project/bash/{build-setup.sh => scripts.sh} | 0 packages/toolkit/scripts/project/build.js | 2 +- packages/toolkit/scripts/project/init.js | 4 ++-- packages/toolkit/scripts/project/package.js | 2 +- packages/toolkit/scripts/project/update-composer.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename packages/toolkit/scripts/project/bash/{build-setup.sh => scripts.sh} (100%) diff --git a/packages/toolkit/scripts/project/bash/build-setup.sh b/packages/toolkit/scripts/project/bash/scripts.sh similarity index 100% rename from packages/toolkit/scripts/project/bash/build-setup.sh rename to packages/toolkit/scripts/project/bash/scripts.sh diff --git a/packages/toolkit/scripts/project/build.js b/packages/toolkit/scripts/project/build.js index d3f2dea9..73876c40 100644 --- a/packages/toolkit/scripts/project/build.js +++ b/packages/toolkit/scripts/project/build.js @@ -34,7 +34,7 @@ const run = async () => { setEnvVariables(variables); - execSync(`bash -l ${__dirname}/bash/build-setup.sh ${buildType}`, { + execSync(`bash -l ${__dirname}/bash/scripts.sh ${buildType}`, { stdio: 'inherit', }); diff --git a/packages/toolkit/scripts/project/init.js b/packages/toolkit/scripts/project/init.js index b900dc77..573a4f00 100755 --- a/packages/toolkit/scripts/project/init.js +++ b/packages/toolkit/scripts/project/init.js @@ -223,10 +223,10 @@ const run = async () => { const cwd = process.cwd(); process.chdir(cliPath); - execSync(`bash -l ${__dirname}/bash/build-setup.sh update-composer`, { + execSync(`bash -l ${__dirname}/bash/scripts.sh update-composer`, { stdio: 'inherit', }); - execSync(`bash -l ${__dirname}/bash/build-setup.sh initialize-git`, { + execSync(`bash -l ${__dirname}/bash/scripts.sh initialize-git`, { stdio: 'inherit', }); process.chdir(cwd); diff --git a/packages/toolkit/scripts/project/package.js b/packages/toolkit/scripts/project/package.js index e16f8ffb..649bab2f 100644 --- a/packages/toolkit/scripts/project/package.js +++ b/packages/toolkit/scripts/project/package.js @@ -26,7 +26,7 @@ const run = async () => { setEnvVariables(variables); - execSync(`bash -l ${__dirname}/bash/build-setup.sh package`, { + execSync(`bash -l ${__dirname}/bash/scripts.sh package`, { stdio: 'inherit', }); diff --git a/packages/toolkit/scripts/project/update-composer.js b/packages/toolkit/scripts/project/update-composer.js index 4f351889..dbbaf0d3 100644 --- a/packages/toolkit/scripts/project/update-composer.js +++ b/packages/toolkit/scripts/project/update-composer.js @@ -26,7 +26,7 @@ const run = async () => { setEnvVariables(variables); - execSync(`bash -l ${__dirname}/bash/build-setup.sh update-composer`, { + execSync(`bash -l ${__dirname}/bash/scripts.sh update-composer`, { stdio: 'inherit', }); From 345a06e940de6b938ec6ab9e0fbad728f051bbed Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Thu, 15 Aug 2024 17:20:15 -0400 Subject: [PATCH 08/16] Now we get the git branch in node so we can pass the proper environment to bash including the proper WORDPRESS_VERSION --- .../toolkit/scripts/project/bash/scripts.sh | 18 +---- packages/toolkit/scripts/project/build.js | 6 +- packages/toolkit/scripts/project/package.js | 1 - packages/toolkit/utils/index.js | 2 + packages/toolkit/utils/project.js | 74 +++++++++++++++++-- 5 files changed, 77 insertions(+), 24 deletions(-) diff --git a/packages/toolkit/scripts/project/bash/scripts.sh b/packages/toolkit/scripts/project/bash/scripts.sh index 21da5e7e..5f7fece6 100644 --- a/packages/toolkit/scripts/project/bash/scripts.sh +++ b/packages/toolkit/scripts/project/bash/scripts.sh @@ -27,20 +27,10 @@ function build:preflight { # Routine to determine what version of WordPress to install function build:version { - WORDPRESS_VERSION="latest" - if [ ${CI:-false} = "true" ]; then - if [ -z ${WORDPRESS_VERSION} ]; then - WORDPRESS_VERSION="latest" - fi - else - GIT_BRANCH=$(git branch --format='%(refname:short)' --show-current) - GIT_BRANCH_SLUG=$(utilities:create-gitlab-slug ${GIT_BRANCH}) - ENVIRONMENT=$(yq eval '.environments | to_entries[] | select(.value.branch == "'${GIT_BRANCH_SLUG}'") | .key' ${TENUP_CI_FILE}) - - if [ ${ENVIRONMENT:-null} != "null" ]; then - WORDPRESS_VERSION=$(yq '.environments.'${ENVIRONMENT}'.wordpress_version' ${TENUP_CI_FILE}) - fi - fi + # If the WORDPRESS_VERSION is not set, set to "latest" + if [ -z ${WORDPRESS_VERSION} ]; then + WORDPRESS_VERSION="latest" + fi if [ "${WORDPRESS_VERSION}" == "latest" ]; then WORDPRESS_VERSION=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq '.offers[0].current' | tr -d '"') diff --git a/packages/toolkit/scripts/project/build.js b/packages/toolkit/scripts/project/build.js index 73876c40..bd6af3c2 100644 --- a/packages/toolkit/scripts/project/build.js +++ b/packages/toolkit/scripts/project/build.js @@ -3,7 +3,6 @@ const chalk = require('chalk'); const { log } = console; -const fs = require('fs'); const { getProjectRoot, getProjectVariables, @@ -13,8 +12,9 @@ const { } = require('../../utils'); const buildType = hasArgInCLI('--type') ? getArgFromCLI('--type') : 'local'; +const buildEnvironment = hasArgInCLI('--environment') ? getArgFromCLI('--environment') : null; -const description = '10up-toolkit project build [--type=]'; +const description = '10up-toolkit project build [--type=] [--environment=]'; const run = async () => { const root = getProjectRoot(); @@ -25,7 +25,7 @@ const run = async () => { } // combine project variables with actual environment variables - const variables = { ...getProjectVariables(), ...process.env }; + const variables = { ...getProjectVariables(buildEnvironment), ...process.env }; if (!variables) { log(chalk.red('No .tenup.yml found.')); diff --git a/packages/toolkit/scripts/project/package.js b/packages/toolkit/scripts/project/package.js index 649bab2f..4b7d24f3 100644 --- a/packages/toolkit/scripts/project/package.js +++ b/packages/toolkit/scripts/project/package.js @@ -3,7 +3,6 @@ const chalk = require('chalk'); const { log } = console; -const fs = require('fs'); const { getProjectRoot, getProjectVariables, setEnvVariables } = require('../../utils'); const description = '10up-toolkit project package'; diff --git a/packages/toolkit/utils/index.js b/packages/toolkit/utils/index.js index ca7245bf..71ffbafd 100644 --- a/packages/toolkit/utils/index.js +++ b/packages/toolkit/utils/index.js @@ -37,6 +37,7 @@ const { maybeInsertStyleVersionHash } = require('./blocks'); const { getProjectRoot, setEnvVariables, + getGitBranch, getProjectVariables, getEnvironmentFromBranch, replaceVariables, @@ -78,5 +79,6 @@ module.exports = { getTenUpScriptsPackageBuildConfig, hasWebpackConfig, displayWebpackStats, + getGitBranch, maybeInsertStyleVersionHash, }; diff --git a/packages/toolkit/utils/project.js b/packages/toolkit/utils/project.js index c28ec2f4..ba2c1844 100644 --- a/packages/toolkit/utils/project.js +++ b/packages/toolkit/utils/project.js @@ -2,6 +2,7 @@ const YAML = require('yaml'); const fs = require('fs'); const { resolve } = require('path'); +const { execSync } = require('child_process'); const fetch = require('node-fetch'); /** @@ -59,18 +60,43 @@ const getWordPressLatestVersion = async () => { return null; }; +/** + * Flatten an object + * + * @param {*} obj + * @param {*} parentKey + * @param {*} result + * @returns {*} + */ +const flattenObject = (obj, parentKey = '', result = {}) => { + Object.keys(obj).forEach((key) => { + const newKey = parentKey ? `${parentKey}__${key}` : key; + const value = obj[key]; + + if (typeof value === 'object' && value !== null && !Array.isArray(value)) { + // Recursively flatten the nested object + flattenObject(value, newKey, result); + } else { + // Assign the value to the result object with the concatenated key + result[newKey] = value; + } + }); + + return result; +}; + /** * Setuip environment variables * * @param {*} variables */ const setEnvVariables = (variables) => { + const flattenedVariables = flattenObject(variables); + // Loop through variables and set them as environment variables - Object.keys(variables).forEach((key) => { - process.env[key] = - typeof variables[key] === 'object' ? JSON.stringify(variables[key]) : variables[key]; - process.env[key.toUpperCase()] = - typeof variables[key] === 'object' ? JSON.stringify(variables[key]) : variables[key]; + Object.keys(flattenedVariables).forEach((key) => { + process.env[key] = flattenedVariables[key]; + process.env[key.toUpperCase()] = flattenedVariables[key]; }); }; @@ -94,13 +120,29 @@ const getEnvironmentFromBranch = (branch, environments = []) => { return matchedEnvironment; }; +const getGitBranch = () => { + let branch = null; + try { + // Get the current git branch into a variable. Ensure nothing is printed to the stdout + branch = execSync('git rev-parse --abbrev-ref HEAD 2>/dev/null', { + encoding: 'utf8', + }) + .toString() + .trim(); + } catch (error) { + // Do nothing + } + + return branch; +}; + /** * Get variables from .tenup.yml * * @param {string} path Path to check * @returns {object|null} */ -const getProjectVariables = (path = '.') => { +const getProjectVariables = (environment, path = '.') => { const projectRoot = getProjectRoot(path); if (!projectRoot) { @@ -118,6 +160,25 @@ const getProjectVariables = (path = '.') => { return null; } + if (!environment) { + const branch = getGitBranch(); + + if (branch) { + data.current_branch = branch; + const matchedEnvironment = getEnvironmentFromBranch(branch, data.environments); + + if (matchedEnvironment) { + data.current_environment = {}; + Object.keys(matchedEnvironment).forEach((key) => { + data.current_environment[key] = matchedEnvironment[key]; + + // We hoist these to the root of the object to make it easier to use in shell e.g. WORDPRESS_VERSION + data[key] = matchedEnvironment[key]; + }); + } + } + } + data.project_root = projectRoot; data.rsync_file_excludes = `./scripts/rsync-excludes.txt`; @@ -142,6 +203,7 @@ module.exports = { getProjectRoot, replaceVariables, setEnvVariables, + getGitBranch, getProjectVariables, getWordPressLatestVersion, getEnvironmentFromBranch, From 470ae80c436959bdeab36b6e41cdcc5ffa8abb6c Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Mon, 19 Aug 2024 12:41:49 -0400 Subject: [PATCH 09/16] Add skip ci option --- packages/toolkit/scripts/project/init.js | 10 +++++++--- packages/toolkit/scripts/project/update-composer.js | 1 - 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/toolkit/scripts/project/init.js b/packages/toolkit/scripts/project/init.js index 573a4f00..86d827c7 100755 --- a/packages/toolkit/scripts/project/init.js +++ b/packages/toolkit/scripts/project/init.js @@ -22,12 +22,14 @@ const confirm = !!hasArgInCLI('--confirm'); const skipComposer = !!hasArgInCLI('--skip-composer'); +const skipCI = !!hasArgInCLI('--skip-ci'); + let template = hasArgInCLI('--template') ? getArgFromCLI('--template') : ''; const variables = require(`../../project/default-variables.json`); const description = - '10up-toolkit project init [--path=] [--layout=] [--name=] [--template=