From 91fcbedc3bf7acdf4e68e026d8e075e6cf08b7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Ludvik?= Date: Tue, 17 Oct 2023 10:51:46 +0200 Subject: [PATCH] wip --- .github/keep-only-first-domain.js | 26 ++++++++++++------------- .github/keep-only-first-domain.sh | 21 ++++++++++++++++++++ .github/workflows/run-checks-tests.yaml | 4 ++-- 3 files changed, 35 insertions(+), 16 deletions(-) create mode 100755 .github/keep-only-first-domain.sh diff --git a/.github/keep-only-first-domain.js b/.github/keep-only-first-domain.js index b2201a7557..c0db678394 100644 --- a/.github/keep-only-first-domain.js +++ b/.github/keep-only-first-domain.js @@ -5,19 +5,17 @@ const path = require('path'); const filePath = path.join(__dirname, '../storefront/next.config.js'); // Read the file -let file = fs.readFileSync(filePath, 'utf8'); +let fileContent = fs.readFileSync(filePath, 'utf8'); -// Use eval to convert the file content to a JavaScript object -// WARNING: eval can be dangerous because it executes the code it's given. -// Only use it if you trust the source of the code. -let nextConfig; -eval('nextConfig = ' + file); +// Use a regular expression to find and modify the domains array +fileContent = fileContent.replace( + /nextConfig\.publicRuntimeConfig\.domains = \[([^\]]+)\];/s, + (match, domains) => { + const domainsArray = JSON.parse(`[${domains}]`); + const firstDomain = [domainsArray[0]]; + return `nextConfig.publicRuntimeConfig.domains = ${JSON.stringify(firstDomain, null, 4)};`; + } +); -// Modify the domains array -nextConfig.publicRuntimeConfig.domains = [nextConfig.publicRuntimeConfig.domains[0]]; - -// Convert the object back to a string -file = 'const nextConfig = ' + JSON.stringify(nextConfig, null, 4) + ';'; - -// Write the file -fs.writeFileSync(filePath, file); +// Write the modified content back to the file +fs.writeFileSync(filePath, fileContent); diff --git a/.github/keep-only-first-domain.sh b/.github/keep-only-first-domain.sh new file mode 100755 index 0000000000..8554761b14 --- /dev/null +++ b/.github/keep-only-first-domain.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Define the path to your next.config.js file +NEXT_CONFIG_FILE="../storefront/next.config.js" + +# Define the path to a temporary file +TMP_FILE="tmp_next.config.js" + +touch "$TMP_FILE" + +# Extract the first domain +FIRST_DOMAIN=$(cat "$NEXT_CONFIG_FILE" | grep -oP 'publicGraphqlEndpoint:.*?,' | head -n 1) + +# Create a temporary modified next.config.js file +cat "$NEXT_CONFIG_FILE" | sed "s/publicRuntimeConfig\.domains:\s*\[/{\n$FIRST_DOMAIN/" > "$TMP_FILE" + +# Replace the original next.config.js with the modified file +mv "$TMP_FILE" "$NEXT_CONFIG_FILE" + +# Cleanup the temporary file +rm "$TMP_FILE" diff --git a/.github/workflows/run-checks-tests.yaml b/.github/workflows/run-checks-tests.yaml index a0238c726a..3053d8622e 100644 --- a/.github/workflows/run-checks-tests.yaml +++ b/.github/workflows/run-checks-tests.yaml @@ -80,8 +80,8 @@ jobs: ref: ${{ github.ref }} - name: Remove all but first domain from next.config.js run: | - node ./.github/keep-only-first-domain.js - cat ./.github/keep-only-first-domain.js + ./.github/keep-only-first-domain.sh + cat ./next.config.js - name: Configure application run: echo 1 | ./scripts/configure.sh - name: Remove other domains than first one from configs