From 48b6d269b6a21762f79a6dfb9a1e827b32b7fa85 Mon Sep 17 00:00:00 2001 From: Nevo Golan <20494189+Nevoss@users.noreply.github.com> Date: Sun, 21 Jul 2024 14:51:20 +0300 Subject: [PATCH 01/10] feat(setup-wp-env): init --- .github/actions/log/action.yml | 14 ---- .github/actions/setup-wp-env/action.yml | 49 ++++++++++++ .github/workflows/log-test.yml | 7 +- .gitignore | 1 + actions/log/action.yml | 14 ---- actions/log/dist/index.js | 13 --- actions/log/index.ts | 15 ---- dist/setup-wp-env/create-wp-env-config.js | 61 ++++++++++++++ scripts/remove-zx-imports-esbuild-plugin.js | 22 ------ scripts/remove-zx-imports-tsup-plugin.js | 25 ++++++ src/setup-wp-env/create-wp-env-config.ts | 88 +++++++++++++++++++++ tsup.config.ts | 10 +-- 12 files changed, 234 insertions(+), 85 deletions(-) delete mode 100644 .github/actions/log/action.yml create mode 100644 .github/actions/setup-wp-env/action.yml delete mode 100644 actions/log/action.yml delete mode 100755 actions/log/dist/index.js delete mode 100644 actions/log/index.ts create mode 100644 dist/setup-wp-env/create-wp-env-config.js delete mode 100644 scripts/remove-zx-imports-esbuild-plugin.js create mode 100644 scripts/remove-zx-imports-tsup-plugin.js create mode 100644 src/setup-wp-env/create-wp-env-config.ts diff --git a/.github/actions/log/action.yml b/.github/actions/log/action.yml deleted file mode 100644 index 52d1304..0000000 --- a/.github/actions/log/action.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: Log -description: Log a message to the console - -inputs: - message: - description: 'The message to log' - required: true - -runs: - using: 'composite' - steps: - - name: Log message - shell: bash - run: npx zx@8 ./actions/log/dist/index.js --message="${{ inputs.message }}" diff --git a/.github/actions/setup-wp-env/action.yml b/.github/actions/setup-wp-env/action.yml new file mode 100644 index 0000000..1fe9f20 --- /dev/null +++ b/.github/actions/setup-wp-env/action.yml @@ -0,0 +1,49 @@ +name: Setup WP Env +description: Setup WordPress environment for testing + +inputs: + wp: + description: WordPress version + required: true + php: + description: PHP version + required: true + plugins: + description: WordPress plugins + required: false + default: '' + themes: + description: WordPress themes + required: false + default: '' + active-theme: + description: Active theme + required: false + default: '' + mappings: + description: Mappings directories into the WordPress container (e.g. templates:/path/to/local/templates) + required: false + default: '' + +runs: + using: composite + steps: + - name: Creating wp-env config file + run: | + npx zx@8 ./dist/setup-wp-env/create-wp-env-config.js \ + --wp="${{ inputs.wp }}" \ + --php="${{ inputs.php }}" \ + --plugins="${{ inputs.plugins }}" \ + --themes="${{ inputs.themes }}" \ + --active-theme="${{ inputs.active-theme }}" \ + --mappings="${{ inputs.mappings }}" \ + --dir="./.wp-env-tmp" + shell: bash + + - name: Run wp-env + run: cd ./.wp-env-tmp && npx wp-env start + shell: bash + + - name: Cleanup wp-env tmp directory + run: rm -rf ./.wp-env-tmp + shell: bash diff --git a/.github/workflows/log-test.yml b/.github/workflows/log-test.yml index 024e42b..fcc679f 100644 --- a/.github/workflows/log-test.yml +++ b/.github/workflows/log-test.yml @@ -17,6 +17,9 @@ jobs: uses: actions/checkout@v4 - name: Log action - uses: ./actions/log + uses: ./.github/actions/setup-wp-env with: - message: 'Hello, Universe!' + php: '8.0' + wp: '6.6' + themes: 'https://downloads.wordpress.org/theme/hello-elementor.zip' + active-theme: 'hello-elementor' diff --git a/.gitignore b/.gitignore index ac7809f..ea56933 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ node_modules/ .idea/ .DS_Store/ tmp/ +.wp-env-temp/ *.log *.map diff --git a/actions/log/action.yml b/actions/log/action.yml deleted file mode 100644 index 52d1304..0000000 --- a/actions/log/action.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: Log -description: Log a message to the console - -inputs: - message: - description: 'The message to log' - required: true - -runs: - using: 'composite' - steps: - - name: Log message - shell: bash - run: npx zx@8 ./actions/log/dist/index.js --message="${{ inputs.message }}" diff --git a/actions/log/dist/index.js b/actions/log/dist/index.js deleted file mode 100755 index 1e387e8..0000000 --- a/actions/log/dist/index.js +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env zx - -// actions/log/index.ts -var args = minimist(process.argv.slice(2), { - string: ["message"], - default: { - message: "Hello, World!" - }, - alias: { - m: "message" - } -}); -echo(args.message); diff --git a/actions/log/index.ts b/actions/log/index.ts deleted file mode 100644 index 2c0e0d1..0000000 --- a/actions/log/index.ts +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env zx - -import { echo, minimist } from 'zx'; - -const args = minimist(process.argv.slice(2), { - string: ['message'], - default: { - message: 'Hello, World!', - }, - alias: { - m: 'message', - }, -}); - -echo(args.message); diff --git a/dist/setup-wp-env/create-wp-env-config.js b/dist/setup-wp-env/create-wp-env-config.js new file mode 100644 index 0000000..4bf9e1e --- /dev/null +++ b/dist/setup-wp-env/create-wp-env-config.js @@ -0,0 +1,61 @@ +// src/setup-wp-env/create-wp-env-config.ts + +async function main() { + const { + wp, + php, + plugins, + themes, + mappings, + dir, + "active-theme": activeTheme + } = getOptions({ + wp: { type: "string", default: null }, + php: { type: "string", default: null }, + plugins: { type: "string", default: "" }, + themes: { type: "string", default: "" }, + "active-theme": { type: "string", default: "" }, + mappings: { type: "string", default: "" }, + dir: { type: "string", default: "./" } + }); + const config = { + core: wp ? `WordPress/Wordpress#${wp}` : null, + phpVersion: php ? php : null, + themes: normalizeArray(themes), + mappings: normalizeMappings(mappings), + plugins: normalizeArray(plugins), + lifecycleScripts: { + afterStart: prepareCommands( + ["cli", "tests-cli"], + [ + activeTheme && `wp theme activate ${activeTheme}`, + `wp rewrite structure '/%postname%' --hard` + ] + ) + } + }; + await fs.ensureDir(dir); + await fs.writeJSON(`${dir}/.wp-env.json`, config, { spaces: 2 }); +} +function getOptions(args) { + const entries = Object.entries(args); + return minimist(process.argv.slice(2), { + string: entries.filter(([, { type }]) => type === "string").map(([key]) => key), + boolean: entries.filter(([, { type }]) => type === "boolean").map(([key]) => key), + default: Object.fromEntries( + entries.map(([key, { default: value }]) => [key, value]) + ) + }); +} +function normalizeMappings(mappings) { + const config = normalizeArray(mappings).map((mapping) => mapping.split(":")).filter(([from, to]) => from && to); + return Object.fromEntries(config); +} +function normalizeArray(array) { + return array.split(",").map((item) => item.trim()).filter(Boolean); +} +function prepareCommands(envs, commands) { + const mergedCommands = commands.filter(Boolean).join(" && "); + return envs.map((env) => `npx wp-env run ${env} bash -c '${mergedCommands}'`).join(" && "); +} +await main(); diff --git a/scripts/remove-zx-imports-esbuild-plugin.js b/scripts/remove-zx-imports-esbuild-plugin.js deleted file mode 100644 index 2033da7..0000000 --- a/scripts/remove-zx-imports-esbuild-plugin.js +++ /dev/null @@ -1,22 +0,0 @@ -import fs from 'node:fs/promises'; - -/** - * When running `zx` on GitHub Actions, - * the `zx` module cannot be found unless all Node packages are installed. - * However, `zx` can be run without the actual import. - * This workaround removes the zx imports from the files, which can make the action run faster. - */ -export default function removeZxImportsEsbuildPlugin() { - return { - name: 'remove-zx-imports', - setup(build) { - build.onLoad({ filter: /.*/ }, async (args) => ({ - contents: await fs - .readFile(args.path, 'utf-8') - .then((res) => - res.replaceAll(/^import .+ from 'zx';?$/gm, ''), - ), - })); - }, - }; -} diff --git a/scripts/remove-zx-imports-tsup-plugin.js b/scripts/remove-zx-imports-tsup-plugin.js new file mode 100644 index 0000000..52f1978 --- /dev/null +++ b/scripts/remove-zx-imports-tsup-plugin.js @@ -0,0 +1,25 @@ +import fs from 'node:fs/promises'; + +/** + * When running `zx` on GitHub Actions, + * the `zx` module cannot be found unless all Node packages are installed. + * However, `zx` can be run without the actual import. + * This workaround removes the zx imports from the files, which can make the action run faster. + * + * @returns {import('tsup').Plugin} + */ +export default function removeZxImportsTsupPlugin() { + return { + name: 'remove-zx-imports', + buildEnd: ({ writtenFiles }) => + Promise.all( + writtenFiles.map(async (file) => { + const content = ( + await fs.readFile(file.name, 'utf-8') + ).replaceAll(/^import .+ from "zx";?$/gm, ''); + + await fs.writeFile(file.name, content, 'utf-8'); + }), + ), + }; +} diff --git a/src/setup-wp-env/create-wp-env-config.ts b/src/setup-wp-env/create-wp-env-config.ts new file mode 100644 index 0000000..d9165ba --- /dev/null +++ b/src/setup-wp-env/create-wp-env-config.ts @@ -0,0 +1,88 @@ +import { minimist, fs } from 'zx'; + +async function main() { + const { + wp, + php, + plugins, + themes, + mappings, + dir, + 'active-theme': activeTheme, + } = getOptions({ + wp: { type: 'string', default: null }, + php: { type: 'string', default: null }, + plugins: { type: 'string', default: '' }, + themes: { type: 'string', default: '' }, + 'active-theme': { type: 'string', default: '' }, + mappings: { type: 'string', default: '' }, + dir: { type: 'string', default: './' }, + }); + + const config = { + core: wp ? `WordPress/Wordpress#${wp}` : null, + phpVersion: php ? php : null, + themes: normalizeArray(themes), + mappings: normalizeMappings(mappings), + plugins: normalizeArray(plugins), + lifecycleScripts: { + afterStart: prepareCommands( + ['cli', 'tests-cli'], + [ + activeTheme && `wp theme activate ${activeTheme}`, + `wp rewrite structure '/%postname%' --hard`, + ], + ), + }, + }; + + await fs.ensureDir(dir); + await fs.writeJSON(`${dir}/.wp-env.json`, config, { spaces: 2 }); +} + +function getOptions( + args: Record< + string, + { type: 'string' | 'boolean'; default: string | boolean | null } + >, +) { + const entries = Object.entries(args); + + return minimist(process.argv.slice(2), { + string: entries + .filter(([, { type }]) => type === 'string') + .map(([key]) => key), + boolean: entries + .filter(([, { type }]) => type === 'boolean') + .map(([key]) => key), + default: Object.fromEntries( + entries.map(([key, { default: value }]) => [key, value]), + ), + }); +} + +function normalizeMappings(mappings: string) { + const config = normalizeArray(mappings) + .map((mapping) => mapping.split(':')) + .filter(([from, to]) => from && to); + + return Object.fromEntries(config); +} + +function normalizeArray(array: string) { + return array + .split(',') + .map((item) => item.trim()) + .filter(Boolean); +} + +function prepareCommands(envs: Array<'cli' | 'tests-cli'>, commands: string[]) { + const mergedCommands = commands.filter(Boolean).join(' && '); + + return envs + .map((env) => `npx wp-env run ${env} bash -c '${mergedCommands}'`) + .join(' && '); +} + +// Run the main function +await main(); diff --git a/tsup.config.ts b/tsup.config.ts index 1d1a161..78fd432 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,13 +1,13 @@ import { defineConfig } from 'tsup'; -import removeZxImportsEsbuildPlugin from './scripts/remove-zx-imports-esbuild-plugin.js'; +import removeZxImportsTsupPlugin from './scripts/remove-zx-imports-tsup-plugin.js'; -export default [defineActionConfig('log')]; +export default [defineActionConfig('setup-wp-env')]; function defineActionConfig(action: string) { return defineConfig({ - entry: [`actions/${action}/index.ts`], - outDir: `actions/${action}/dist`, + entry: [`src/${action}/*.ts`], + outDir: `dist/${action}`, format: 'esm', - esbuildPlugins: [removeZxImportsEsbuildPlugin()], + plugins: [removeZxImportsTsupPlugin()], }); } From 6532e66ca2add6d2aa2815435f20bc56a70d4391 Mon Sep 17 00:00:00 2001 From: Nevo Golan <20494189+Nevoss@users.noreply.github.com> Date: Sun, 21 Jul 2024 14:57:25 +0300 Subject: [PATCH 02/10] wip --- .github/actions/setup-wp-env/action.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/actions/setup-wp-env/action.yml b/.github/actions/setup-wp-env/action.yml index 1fe9f20..dccedbf 100644 --- a/.github/actions/setup-wp-env/action.yml +++ b/.github/actions/setup-wp-env/action.yml @@ -40,8 +40,12 @@ runs: --dir="./.wp-env-tmp" shell: bash + - name: Output wp-env config file + run: cat ./.wp-env-tmp/.wp-env.json + shell: bash + - name: Run wp-env - run: cd ./.wp-env-tmp && npx wp-env start + run: cd ./.wp-env-tmp && npx @wordpress/env start shell: bash - name: Cleanup wp-env tmp directory From e2af6fb2b4c96f4d5100426f7458f86bf2994b75 Mon Sep 17 00:00:00 2001 From: Nevo Golan <20494189+Nevoss@users.noreply.github.com> Date: Sun, 21 Jul 2024 15:10:00 +0300 Subject: [PATCH 03/10] wip --- dist/setup-wp-env/create-wp-env-config.js | 4 +++- src/setup-wp-env/create-wp-env-config.ts | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/dist/setup-wp-env/create-wp-env-config.js b/dist/setup-wp-env/create-wp-env-config.js index 4bf9e1e..cd781a5 100644 --- a/dist/setup-wp-env/create-wp-env-config.js +++ b/dist/setup-wp-env/create-wp-env-config.js @@ -56,6 +56,8 @@ function normalizeArray(array) { } function prepareCommands(envs, commands) { const mergedCommands = commands.filter(Boolean).join(" && "); - return envs.map((env) => `npx wp-env run ${env} bash -c '${mergedCommands}'`).join(" && "); + return envs.map( + (env) => `npx @wordpress/env run ${env} bash -c '${mergedCommands}'` + ).join(" && "); } await main(); diff --git a/src/setup-wp-env/create-wp-env-config.ts b/src/setup-wp-env/create-wp-env-config.ts index d9165ba..d09a8fa 100644 --- a/src/setup-wp-env/create-wp-env-config.ts +++ b/src/setup-wp-env/create-wp-env-config.ts @@ -80,7 +80,10 @@ function prepareCommands(envs: Array<'cli' | 'tests-cli'>, commands: string[]) { const mergedCommands = commands.filter(Boolean).join(' && '); return envs - .map((env) => `npx wp-env run ${env} bash -c '${mergedCommands}'`) + .map( + (env) => + `npx @wordpress/env run ${env} bash -c '${mergedCommands}'`, + ) .join(' && '); } From 33410aa79f619afde0b51f700fc7e2aabf7fc291 Mon Sep 17 00:00:00 2001 From: Nevo Golan <20494189+Nevoss@users.noreply.github.com> Date: Sun, 21 Jul 2024 16:24:14 +0300 Subject: [PATCH 04/10] wip --- .github/workflows/{log-test.yml => test-actions.yml} | 6 +++--- .gitignore | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) rename .github/workflows/{log-test.yml => test-actions.yml} (92%) diff --git a/.github/workflows/log-test.yml b/.github/workflows/test-actions.yml similarity index 92% rename from .github/workflows/log-test.yml rename to .github/workflows/test-actions.yml index fcc679f..3aaa703 100644 --- a/.github/workflows/log-test.yml +++ b/.github/workflows/test-actions.yml @@ -1,4 +1,4 @@ -name: Log Test +name: Test Actions on: [pull_request, workflow_dispatch] @@ -8,8 +8,8 @@ concurrency: cancel-in-progress: true jobs: - log-test: - name: Log action test + test: + name: Setup WP Env runs-on: ubuntu-latest if: startsWith( github.repository, 'elementor/' ) steps: diff --git a/.gitignore b/.gitignore index ea56933..ac7809f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ node_modules/ .idea/ .DS_Store/ tmp/ -.wp-env-temp/ *.log *.map From b26c0d7976a2dcc41df4cdb2e4e9334ccc73609b Mon Sep 17 00:00:00 2001 From: Nevo Golan <20494189+Nevoss@users.noreply.github.com> Date: Sun, 21 Jul 2024 16:46:46 +0300 Subject: [PATCH 05/10] wip --- .github/workflows/test-actions.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-actions.yml b/.github/workflows/test-actions.yml index 3aaa703..91f8ee0 100644 --- a/.github/workflows/test-actions.yml +++ b/.github/workflows/test-actions.yml @@ -8,7 +8,7 @@ concurrency: cancel-in-progress: true jobs: - test: + test-wp-env: name: Setup WP Env runs-on: ubuntu-latest if: startsWith( github.repository, 'elementor/' ) From 9de9a58a731ad73726bbe14eaa539d29840af308 Mon Sep 17 00:00:00 2001 From: Nevo Golan <20494189+Nevoss@users.noreply.github.com> Date: Sun, 21 Jul 2024 16:48:34 +0300 Subject: [PATCH 06/10] wip --- .github/workflows/test-actions.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-actions.yml b/.github/workflows/test-actions.yml index 91f8ee0..bf58f8d 100644 --- a/.github/workflows/test-actions.yml +++ b/.github/workflows/test-actions.yml @@ -16,7 +16,7 @@ jobs: - name: Checkout source code uses: actions/checkout@v4 - - name: Log action + - name: Setup WP Env uses: ./.github/actions/setup-wp-env with: php: '8.0' From 7bfdce314ad75445b7c73dd51f2c31e6a77bb1fe Mon Sep 17 00:00:00 2001 From: Nevo Golan <20494189+Nevoss@users.noreply.github.com> Date: Sun, 21 Jul 2024 17:05:49 +0300 Subject: [PATCH 07/10] wip --- .github/actions/setup-wp-env/action.yml | 18 +++++++++--------- dist/setup-wp-env/create-wp-env-config.js | 20 ++++++++++---------- src/setup-wp-env/create-wp-env-config.ts | 20 ++++++++++---------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/.github/actions/setup-wp-env/action.yml b/.github/actions/setup-wp-env/action.yml index dccedbf..22d3519 100644 --- a/.github/actions/setup-wp-env/action.yml +++ b/.github/actions/setup-wp-env/action.yml @@ -3,25 +3,25 @@ description: Setup WordPress environment for testing inputs: wp: - description: WordPress version - required: true + description: WordPress version (e.g. `5.8`, `6.6`) + required: false php: - description: PHP version - required: true + description: PHP version (e.g. `7.4`, `8.0`) + required: false plugins: - description: WordPress plugins + description: A comma separated list of plugins to install (e.g. `./path/to/plugin, elementor/elementor#main, https://localhost/path/to/plugin.zip`) required: false default: '' themes: - description: WordPress themes + description: A comma separated list of themes to install (e.g. `./path/to/theme, elementor/hello-elementor#main, https://localhost/path/to/theme.zip`) required: false default: '' active-theme: - description: Active theme + description: The slug of a theme to activate (e.g. `hello-elementor`) required: false default: '' mappings: - description: Mappings directories into the WordPress container (e.g. templates:/path/to/local/templates) + description: Mappings directories into the WordPress container (e.g. `templates:/path/to/local/templates, plugins:/path/to/local/plugins`) required: false default: '' @@ -37,7 +37,7 @@ runs: --themes="${{ inputs.themes }}" \ --active-theme="${{ inputs.active-theme }}" \ --mappings="${{ inputs.mappings }}" \ - --dir="./.wp-env-tmp" + --config-dir="./.wp-env-tmp" shell: bash - name: Output wp-env config file diff --git a/dist/setup-wp-env/create-wp-env-config.js b/dist/setup-wp-env/create-wp-env-config.js index cd781a5..8944f08 100644 --- a/dist/setup-wp-env/create-wp-env-config.js +++ b/dist/setup-wp-env/create-wp-env-config.js @@ -7,7 +7,7 @@ async function main() { plugins, themes, mappings, - dir, + "config-dir": configDir, "active-theme": activeTheme } = getOptions({ wp: { type: "string", default: null }, @@ -16,14 +16,14 @@ async function main() { themes: { type: "string", default: "" }, "active-theme": { type: "string", default: "" }, mappings: { type: "string", default: "" }, - dir: { type: "string", default: "./" } + "config-dir": { type: "string", default: "./" } }); const config = { core: wp ? `WordPress/Wordpress#${wp}` : null, phpVersion: php ? php : null, - themes: normalizeArray(themes), - mappings: normalizeMappings(mappings), - plugins: normalizeArray(plugins), + themes: parseAsArray(themes), + mappings: mappingsFromString(mappings), + plugins: parseAsArray(plugins), lifecycleScripts: { afterStart: prepareCommands( ["cli", "tests-cli"], @@ -34,8 +34,8 @@ async function main() { ) } }; - await fs.ensureDir(dir); - await fs.writeJSON(`${dir}/.wp-env.json`, config, { spaces: 2 }); + await fs.ensureDir(configDir); + await fs.writeJSON(`${configDir}/.wp-env.json`, config, { spaces: 2 }); } function getOptions(args) { const entries = Object.entries(args); @@ -47,11 +47,11 @@ function getOptions(args) { ) }); } -function normalizeMappings(mappings) { - const config = normalizeArray(mappings).map((mapping) => mapping.split(":")).filter(([from, to]) => from && to); +function mappingsFromString(mappings) { + const config = parseAsArray(mappings).map((mapping) => mapping.split(":")).filter(([from, to]) => from && to); return Object.fromEntries(config); } -function normalizeArray(array) { +function parseAsArray(array) { return array.split(",").map((item) => item.trim()).filter(Boolean); } function prepareCommands(envs, commands) { diff --git a/src/setup-wp-env/create-wp-env-config.ts b/src/setup-wp-env/create-wp-env-config.ts index d09a8fa..003e1b7 100644 --- a/src/setup-wp-env/create-wp-env-config.ts +++ b/src/setup-wp-env/create-wp-env-config.ts @@ -7,7 +7,7 @@ async function main() { plugins, themes, mappings, - dir, + 'config-dir': configDir, 'active-theme': activeTheme, } = getOptions({ wp: { type: 'string', default: null }, @@ -16,15 +16,15 @@ async function main() { themes: { type: 'string', default: '' }, 'active-theme': { type: 'string', default: '' }, mappings: { type: 'string', default: '' }, - dir: { type: 'string', default: './' }, + 'config-dir': { type: 'string', default: './' }, }); const config = { core: wp ? `WordPress/Wordpress#${wp}` : null, phpVersion: php ? php : null, - themes: normalizeArray(themes), - mappings: normalizeMappings(mappings), - plugins: normalizeArray(plugins), + themes: parseAsArray(themes), + mappings: mappingsFromString(mappings), + plugins: parseAsArray(plugins), lifecycleScripts: { afterStart: prepareCommands( ['cli', 'tests-cli'], @@ -36,8 +36,8 @@ async function main() { }, }; - await fs.ensureDir(dir); - await fs.writeJSON(`${dir}/.wp-env.json`, config, { spaces: 2 }); + await fs.ensureDir(configDir); + await fs.writeJSON(`${configDir}/.wp-env.json`, config, { spaces: 2 }); } function getOptions( @@ -61,15 +61,15 @@ function getOptions( }); } -function normalizeMappings(mappings: string) { - const config = normalizeArray(mappings) +function mappingsFromString(mappings: string) { + const config = parseAsArray(mappings) .map((mapping) => mapping.split(':')) .filter(([from, to]) => from && to); return Object.fromEntries(config); } -function normalizeArray(array: string) { +function parseAsArray(array: string) { return array .split(',') .map((item) => item.trim()) From 1662ed563ec8873f10e5e64b0aff5b2cf5200ce0 Mon Sep 17 00:00:00 2001 From: Nevo Golan <20494189+Nevoss@users.noreply.github.com> Date: Mon, 22 Jul 2024 00:22:04 +0300 Subject: [PATCH 08/10] fix(setup-wp-env): action path --- .github/actions/setup-wp-env/action.yml | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/.github/actions/setup-wp-env/action.yml b/.github/actions/setup-wp-env/action.yml index 22d3519..0c341ab 100644 --- a/.github/actions/setup-wp-env/action.yml +++ b/.github/actions/setup-wp-env/action.yml @@ -29,14 +29,22 @@ runs: using: composite steps: - name: Creating wp-env config file + env: + INPUT_WP: ${{ inputs.wp }} + INPUT_PHP: ${{ inputs.php }} + INPUT_PLUGINS: ${{ inputs.plugins }} + INPUT_THEMES: ${{ inputs.themes }} + INPUT_ACTIVE_THEME: ${{ inputs.active-theme }} + INPUT_MAPPINGS: ${{ inputs.mappings }} + ACTION_PATH: ${{ github.action_path }} run: | - npx zx@8 ./dist/setup-wp-env/create-wp-env-config.js \ - --wp="${{ inputs.wp }}" \ - --php="${{ inputs.php }}" \ - --plugins="${{ inputs.plugins }}" \ - --themes="${{ inputs.themes }}" \ - --active-theme="${{ inputs.active-theme }}" \ - --mappings="${{ inputs.mappings }}" \ + npx zx@8 $ACTION_PATH/dist/setup-wp-env/create-wp-env-config.js \ + --wp="$INPUT_WP" \ + --php="$INPUT_PHP" \ + --plugins="$INPUT_PLUGINS" \ + --themes="$INPUT_THEMES" \ + --active-theme="$INPUT_ACTIVE_THEME" \ + --mappings="$INPUT_ACTIVE_THEME" \ --config-dir="./.wp-env-tmp" shell: bash From 0eec4d05ee633c0524e15829dc74a0a170011ec4 Mon Sep 17 00:00:00 2001 From: Nevo Golan <20494189+Nevoss@users.noreply.github.com> Date: Tue, 23 Jul 2024 08:17:32 +0300 Subject: [PATCH 09/10] wip --- .github/actions/setup-wp-env/action.yml | 2 +- src/setup-wp-env/create-wp-env-config.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/actions/setup-wp-env/action.yml b/.github/actions/setup-wp-env/action.yml index 0c341ab..4f245a9 100644 --- a/.github/actions/setup-wp-env/action.yml +++ b/.github/actions/setup-wp-env/action.yml @@ -38,7 +38,7 @@ runs: INPUT_MAPPINGS: ${{ inputs.mappings }} ACTION_PATH: ${{ github.action_path }} run: | - npx zx@8 $ACTION_PATH/dist/setup-wp-env/create-wp-env-config.js \ + npx zx@8 $ACTION_PATH/../../../dist/setup-wp-env/create-wp-env-config.js \ --wp="$INPUT_WP" \ --php="$INPUT_PHP" \ --plugins="$INPUT_PLUGINS" \ diff --git a/src/setup-wp-env/create-wp-env-config.ts b/src/setup-wp-env/create-wp-env-config.ts index 003e1b7..7ed3cdb 100644 --- a/src/setup-wp-env/create-wp-env-config.ts +++ b/src/setup-wp-env/create-wp-env-config.ts @@ -29,7 +29,8 @@ async function main() { afterStart: prepareCommands( ['cli', 'tests-cli'], [ - activeTheme && `wp theme activate ${activeTheme}`, + activeTheme && + `INPUT_ACTIVE_THEME="${activeTheme}" && wp theme activate "$INPUT_ACTIVE_THEME"`, `wp rewrite structure '/%postname%' --hard`, ], ), From b8ce44c25407146051a525933c50a85cc015e1ef Mon Sep 17 00:00:00 2001 From: Nevo Golan <20494189+Nevoss@users.noreply.github.com> Date: Tue, 23 Jul 2024 08:56:45 +0300 Subject: [PATCH 10/10] wip --- dist/setup-wp-env/create-wp-env-config.js | 16 +++++++++++++--- src/setup-wp-env/create-wp-env-config.ts | 23 ++++++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/dist/setup-wp-env/create-wp-env-config.js b/dist/setup-wp-env/create-wp-env-config.js index 8944f08..dcc6c25 100644 --- a/dist/setup-wp-env/create-wp-env-config.js +++ b/dist/setup-wp-env/create-wp-env-config.js @@ -14,7 +14,11 @@ async function main() { php: { type: "string", default: null }, plugins: { type: "string", default: "" }, themes: { type: "string", default: "" }, - "active-theme": { type: "string", default: "" }, + "active-theme": { + type: "string", + default: "", + validate: (value) => typeof value === "string" && /^[a-z0-9-]+$/.test(value) + }, mappings: { type: "string", default: "" }, "config-dir": { type: "string", default: "./" } }); @@ -28,7 +32,7 @@ async function main() { afterStart: prepareCommands( ["cli", "tests-cli"], [ - activeTheme && `wp theme activate ${activeTheme}`, + activeTheme && `INPUT_ACTIVE_THEME="${activeTheme}" && wp theme activate "$INPUT_ACTIVE_THEME"`, `wp rewrite structure '/%postname%' --hard` ] ) @@ -39,13 +43,19 @@ async function main() { } function getOptions(args) { const entries = Object.entries(args); - return minimist(process.argv.slice(2), { + const options = minimist(process.argv.slice(2), { string: entries.filter(([, { type }]) => type === "string").map(([key]) => key), boolean: entries.filter(([, { type }]) => type === "boolean").map(([key]) => key), default: Object.fromEntries( entries.map(([key, { default: value }]) => [key, value]) ) }); + entries.forEach(([key, { validate }]) => { + if (validate && !validate(options[key])) { + throw new Error(`Invalid value for option --${key}`); + } + }); + return options; } function mappingsFromString(mappings) { const config = parseAsArray(mappings).map((mapping) => mapping.split(":")).filter(([from, to]) => from && to); diff --git a/src/setup-wp-env/create-wp-env-config.ts b/src/setup-wp-env/create-wp-env-config.ts index 7ed3cdb..689fdf6 100644 --- a/src/setup-wp-env/create-wp-env-config.ts +++ b/src/setup-wp-env/create-wp-env-config.ts @@ -14,7 +14,12 @@ async function main() { php: { type: 'string', default: null }, plugins: { type: 'string', default: '' }, themes: { type: 'string', default: '' }, - 'active-theme': { type: 'string', default: '' }, + 'active-theme': { + type: 'string', + default: '', + validate: (value) => + typeof value === 'string' && /^[a-z0-9-]+$/.test(value), + }, mappings: { type: 'string', default: '' }, 'config-dir': { type: 'string', default: './' }, }); @@ -44,12 +49,16 @@ async function main() { function getOptions( args: Record< string, - { type: 'string' | 'boolean'; default: string | boolean | null } + { + type: 'string' | 'boolean'; + default: string | boolean | null; + validate?: (value: unknown) => boolean; + } >, ) { const entries = Object.entries(args); - return minimist(process.argv.slice(2), { + const options = minimist(process.argv.slice(2), { string: entries .filter(([, { type }]) => type === 'string') .map(([key]) => key), @@ -60,6 +69,14 @@ function getOptions( entries.map(([key, { default: value }]) => [key, value]), ), }); + + entries.forEach(([key, { validate }]) => { + if (validate && !validate(options[key])) { + throw new Error(`Invalid value for option --${key}`); + } + }); + + return options; } function mappingsFromString(mappings: string) {