Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for base64 encoding in inject-site-configs command #3053

Merged
merged 8 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-shrimps-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/cli": minor
---

Add option for base64 encoding in `inject-site-configs` command
2 changes: 1 addition & 1 deletion demo/admin/src/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function createConfig() {
...cometConfig,
apiUrl: environmentVariables.API_URL,
adminUrl: environmentVariables.ADMIN_URL,
sitesConfig: JSON.parse(environmentVariables.PUBLIC_SITE_CONFIGS) as PublicSiteConfig[],
sitesConfig: JSON.parse(atob(environmentVariables.PUBLIC_SITE_CONFIGS)) as PublicSiteConfig[],
buildDate: environmentVariables.BUILD_DATE,
buildNumber: environmentVariables.BUILD_NUMBER,
commitSha: environmentVariables.COMMIT_SHA,
Expand Down
2 changes: 1 addition & 1 deletion demo/api/src/config/environment-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,6 @@ export class EnvironmentVariables {
SITE_PREVIEW_SECRET: string;

@IsArray()
@Transform(({ value }) => JSON.parse(value))
@Transform(({ value }) => JSON.parse(Buffer.from(value, "base64").toString()))
PRIVATE_SITE_CONFIGS: PrivateSiteConfig[];
}
2 changes: 1 addition & 1 deletion demo/site/src/util/siteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function getSiteConfigs() {
if (!siteConfigs) {
const json = process.env.PUBLIC_SITE_CONFIGS;
if (!json) throw new Error("process.env.PUBLIC_SITE_CONFIGS must be set.");
siteConfigs = JSON.parse(json) as PublicSiteConfig[];
siteConfigs = JSON.parse(atob(json)) as PublicSiteConfig[];
}
return siteConfigs;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"license": "BSD-2-Clause",
"scripts": {
"create-site-configs-env": "npx @comet/cli inject-site-configs -f demo/site-configs/site-configs.ts -i demo/.env.site-configs.tpl -o demo/.env.site-configs -d",
"create-site-configs-env": "npx @comet/cli inject-site-configs -f demo/site-configs/site-configs.ts -i demo/.env.site-configs.tpl -o demo/.env.site-configs --base64",
"build:storybook": "pnpm recursive --filter '@comet/*admin*' --filter '@comet/eslint-plugin' --filter '@comet/cli' run build && pnpm --filter comet-storybook run build-storybook",
"build:packages": "pnpm recursive --filter '@comet/*' run build",
"build:docs": "pnpm recursive --filter '@comet/eslint-plugin' --filter '@comet/admin*' --filter 'comet-docs' run build",
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/commands/site-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const injectSiteConfigsCommand = new Command("inject-site-configs")
.requiredOption("-i, --in-file <file>", "The filename of a template file to inject.")
.requiredOption("-o, --out-file <file>", "Write the injected template to a file.")
.option("-d --dotenv", "dotenv compatibility") // https://github.com/motdotla/dotenv/issues/521#issuecomment-999016064
.option("--base64", "use base64 encoding")
.option("-f, --site-config-file <file>", "Path to ts-file which provides a default export with (env: string) => SiteConfig[]")
.action(async (options) => {
const configFile = `${process.cwd()}/${options.siteConfigFile || "site-configs.ts"}`;
Expand Down Expand Up @@ -51,7 +52,11 @@ export const injectSiteConfigsCommand = new Command("inject-site-configs")
console.error(`inject-site-configs: ERROR: type must be ${Object.keys(replacerFunctions).join("|")} (got ${type})`);
return substr;
}
const ret = JSON.stringify(replacerFunctions[type](siteConfigs, env)).replace(/\\/g, "\\\\");
const str = replacerFunctions[type](siteConfigs, env);
if (options.base64) {
return Buffer.from(JSON.stringify(str)).toString("base64");
}
const ret = JSON.stringify(str).replace(/\\/g, "\\\\");
if (options.dotenv) return ret.replace(/\$/g, "\\$");
return ret;
});
Expand Down
Loading