-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): fetch locales at build time
- Loading branch information
Showing
24 changed files
with
138 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@bigcommerce/catalyst-core": minor | ||
--- | ||
|
||
fetch available locales at build time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,6 @@ client/generated | |
|
||
# secrets | ||
.catalyst | ||
|
||
# Build config | ||
build-config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import rawBuildConfig from './build-config.json'; | ||
import { buildConfigSchema, BuildConfigSchema } from './schema'; | ||
|
||
class BuildConfig { | ||
private config = buildConfigSchema.parse(rawBuildConfig); | ||
|
||
get<K extends keyof BuildConfigSchema>(key: K): BuildConfigSchema[K] { | ||
if (key in this.config) { | ||
return this.config[key]; | ||
} | ||
|
||
throw new Error(`Key "${key}" not found in BuildConfig`); | ||
} | ||
} | ||
|
||
export const buildConfig = new BuildConfig(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { z } from 'zod'; | ||
|
||
export const buildConfigSchema = z.object({ | ||
locales: z.array( | ||
z.object({ | ||
code: z.string(), | ||
isDefault: z.boolean(), | ||
}), | ||
), | ||
}); | ||
|
||
export type BuildConfigSchema = z.infer<typeof buildConfigSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint-disable no-console */ | ||
import { writeFile } from 'node:fs/promises'; | ||
import { dirname, join } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { z } from 'zod'; | ||
|
||
import { buildConfigSchema } from './schema'; | ||
|
||
const destinationPath = dirname(fileURLToPath(import.meta.url)); | ||
const CONFIG_FILE = join(destinationPath, 'build-config.json'); | ||
|
||
// This fn is only intended to be used in the build process (next.config.ts) | ||
export async function writeBuildConfig(data: unknown) { | ||
try { | ||
buildConfigSchema.parse(data); | ||
|
||
await writeFile(CONFIG_FILE, JSON.stringify(data), 'utf8'); | ||
} catch (error) { | ||
if (error instanceof z.ZodError) { | ||
console.error('Data validation failed:', error.errors); | ||
} else { | ||
console.error('Error writing build-config.json:', error); | ||
} | ||
|
||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,10 @@ | ||
import { type LocaleType } from './i18n/routing'; | ||
|
||
export type RecordFromLocales = { | ||
[K in LocaleType]: string; | ||
}; | ||
|
||
// Set overrides per locale | ||
const localeToChannelsMappings: Partial<RecordFromLocales> = { | ||
const localeToChannelsMappings: Record<string, string> = { | ||
// es: '12345', | ||
}; | ||
|
||
function getChannelIdFromLocale(locale?: string) { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
return localeToChannelsMappings[locale as LocaleType] ?? process.env.BIGCOMMERCE_CHANNEL_ID; | ||
function getChannelIdFromLocale(locale = '') { | ||
return localeToChannelsMappings[locale] ?? process.env.BIGCOMMERCE_CHANNEL_ID; | ||
} | ||
|
||
export { getChannelIdFromLocale }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.