Skip to content

Commit

Permalink
add support of raw string inputs for json-like config ENVs
Browse files Browse the repository at this point in the history
  • Loading branch information
tom2drum committed Aug 30, 2024
1 parent 030b510 commit cd8305d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
19 changes: 17 additions & 2 deletions configs/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,25 @@ export const getExternalAssetFilePath = (envName: string) => {
export const buildExternalAssetFilePath = (name: string, value: string) => {
try {
const fileName = name.replace(/^NEXT_PUBLIC_/, '').replace(/_URL$/, '').toLowerCase();
const url = new URL(value);
const fileExtension = url.pathname.match(regexp.FILE_EXTENSION)?.[1];

const fileExtension = getAssetFileExtension(value);
if (!fileExtension) {
throw new Error('Cannot get file path');
}
return `/assets/configs/${ fileName }.${ fileExtension }`;
} catch (error) {
return;
}
};

function getAssetFileExtension(value: string) {
try {
const url = new URL(value);
return url.pathname.match(regexp.FILE_EXTENSION)?.[1];
} catch (error) {
try {
parseEnvJson(value);
return 'json';
} catch (error) {}
}
}
35 changes: 27 additions & 8 deletions deploy/scripts/download_assets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ get_target_filename() {
# Extract the extension from the filename
local extension="${filename##*.}"
else
# Remove query parameters from the URL and get the filename
local filename=$(basename "${url%%\?*}")
# Extract the extension from the filename
local extension="${filename##*.}"
if [[ "$url" == http* ]]; then
# Remove query parameters from the URL and get the filename
local filename=$(basename "${url%%\?*}")
# Extract the extension from the filename
local extension="${filename##*.}"
else
local extension="json"
fi
fi

# Convert the extension to lowercase
Expand Down Expand Up @@ -80,16 +84,31 @@ download_and_save_asset() {
# Copy the local file to the destination
cp "${url#file://}" "$destination"
else
# Download the asset using curl
curl -s -o "$destination" "$url"
# Check if the value is a URL
if [[ "$url" == http* ]]; then
# Download the asset using curl
curl -s -o "$destination" "$url"
else
# Convert single-quoted JSON-like content to valid JSON
json_content=$(echo "${!env_var}" | sed "s/'/\"/g")

# Save the JSON content to a file
echo "$json_content" > "$destination"
fi
fi

if [[ "$url" == file://* ]] || [[ "$url" == http* ]]; then
local source_name=$url
else
local source_name="raw input"
fi

# Check if the download was successful
if [ $? -eq 0 ]; then
echo " [+] $env_var: Successfully saved file from $url to $destination."
echo " [+] $env_var: Successfully saved file from $source_name to $destination."
return 0
else
echo " [-] $env_var: Failed to save file from $url."
echo " [-] $env_var: Failed to save file from $source_name."
return 1
fi
}
Expand Down
4 changes: 2 additions & 2 deletions docs/ENVS.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Please be aware that all environment variables prefixed with `NEXT_PUBLIC_` will
| NEXT_PUBLIC_NETWORK_LOGO_DARK | `string` | Network logo for dark color mode; if not provided, **inverted** regular logo will be used instead | - | - | `https://placekitten.com/240/40` | v1.0.x+ |
| NEXT_PUBLIC_NETWORK_ICON | `string` | Network icon; used as a replacement for regular network logo when nav bar is collapsed; if not provided, placeholder will be shown; *Note* the icon size should be at least 60px by 60px | - | - | `https://placekitten.com/60/60` | v1.0.x+ |
| NEXT_PUBLIC_NETWORK_ICON_DARK | `string` | Network icon for dark color mode; if not provided, **inverted** regular icon will be used instead | - | - | `https://placekitten.com/60/60` | v1.0.x+ |
| NEXT_PUBLIC_FEATURED_NETWORKS | `string` | URL of configuration file (`.json` format only) which contains list of featured networks that will be shown in the network menu. See [below](#featured-network-configuration-properties) list of available properties for particular network | - | - | `https://example.com/featured_networks_config.json` | v1.0.x+ |
| NEXT_PUBLIC_FEATURED_NETWORKS | `string` | URL of configuration file (`.json` format only) or file content string representation. It contains list of featured networks that will be shown in the network menu. See [below](#featured-network-configuration-properties) list of available properties for particular network | - | - | `https://example.com/featured_networks_config.json` \| `[{'title':'Astar(EVM)','url':'https://astar.blockscout.com/','group':'Mainnets','icon':'https://example.com/astar.svg'}]` | v1.0.x+ |
| NEXT_PUBLIC_OTHER_LINKS | `Array<{url: string; text: string}>` | List of links for the "Other" navigation menu | - | - | `[{'url':'https://blockscout.com','text':'Blockscout'}]` | v1.0.x+ |
| NEXT_PUBLIC_NAVIGATION_HIDDEN_LINKS | `Array<LinkId>` | List of external links hidden in the navigation. Supported ids are `eth_rpc_api`, `rpc_api` | - | - | `['eth_rpc_api']` | v1.16.0+ |
| NEXT_PUBLIC_NAVIGATION_HIGHLIGHTED_ROUTES | `Array<string>` | List of menu item routes that should have a lightning label | - | - | `['/accounts']` | v1.31.0+ |
Expand All @@ -155,7 +155,7 @@ Please be aware that all environment variables prefixed with `NEXT_PUBLIC_` will

| Variable | Type| Description | Compulsoriness | Default value | Example value | Version |
| --- | --- | --- | --- | --- | --- | --- |
| NEXT_PUBLIC_FOOTER_LINKS | `string` | URL of configuration file (`.json` format only) which contains list of link groups to be displayed in the footer. See [below](#footer-links-configuration-properties) list of available properties for particular group | - | - | `https://example.com/footer_links_config.json` | v1.1.1+ |
| NEXT_PUBLIC_FOOTER_LINKS | `string` | URL of configuration file (`.json` format only) or file content string representation. It contains list of link groups to be displayed in the footer. See [below](#footer-links-configuration-properties) list of available properties for particular group | - | - | `https://example.com/footer_links_config.json` \| `[{'title':'My chain','links':[{'text':'About','url':'https://example.com/about'},{'text':'Contacts','url':'https://example.com/contacts'}]}]` | v1.1.1+ |

The app version shown in the footer is derived from build-time ENV variables `NEXT_PUBLIC_GIT_TAG` and `NEXT_PUBLIC_GIT_COMMIT_SHA` and cannot be overwritten at run-time.

Expand Down

0 comments on commit cd8305d

Please sign in to comment.