Skip to content

Commit

Permalink
Use environement variable in awk for a more portable shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Feb 27, 2024
1 parent 7c15635 commit c28fe19
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,14 +608,14 @@ export function viteEnvs(params?: {
`#!/bin/sh`,
``,
`replaceAll() {`,
` inputString="$1"`,
` pattern="$2"`,
` replacement="$3"`,
` export inputString="$1"`,
` export pattern="$2"`,
` export replacement="$3"`,
``,
` echo "$inputString" | awk -v pat="$pattern" -v rep="$replacement" '{`,
` gsub(pat, rep)`,
` print`,
` }'`,
` echo "$inputString" | awk '{`,
` gsub(ENVIRON["pattern"], ENVIRON["replacement"])`,
` print`,
` }'`,
`}`,
``,
`html=$(echo "${Buffer.from(processedHtml, "utf8").toString(
Expand Down Expand Up @@ -657,19 +657,19 @@ export function viteEnvs(params?: {
),
`json="$json}"`,
``,
`script="\n"`,
`script="$script <script data-script-description=\\"Environment variables injected by vite-envs\\">\n"`,
`script="$script var envWithValuesInBase64 = $json;\n"`,
`script="$script var env = {};\n"`,
`script="$script Object.keys(envWithValuesInBase64).forEach(function (name) {\n"`,
`script="$script env[name] = new TextDecoder().decode(\n"`,
`script="$script Uint8Array.from(\n"`,
`script="$script atob(envWithValuesInBase64[name]),\n"`,
`script="$script c => c.charCodeAt(0))\n"`,
`script="$script );\n"`,
`script="$script });\n"`,
`script="$script window.${nameOfTheGlobal} = env;\n"`,
`script="$script </script>\n"`,
`script="`,
` <script data-script-description=\\"Environment variables injected by vite-envs\\">`,
` var envWithValuesInBase64 = $json;`,
` var env = {};`,
` Object.keys(envWithValuesInBase64).forEach(function (name) {`,
` env[name] = new TextDecoder().decode(`,
` Uint8Array.from(`,
` atob(envWithValuesInBase64[name]),`,
` c => c.charCodeAt(0))`,
` );`,
` });`,
` window.${nameOfTheGlobal} = env;`,
` </script>"`,
``,
`scriptPlaceholder="${placeholderForViteEnvsScript}"`,
``,
Expand Down

0 comments on commit c28fe19

Please sign in to comment.