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

[WIP] Migrate localization validations to Core #5165

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ describe('loadLocalesConfig', () => {
})
})

test('Throws if one locale is too big', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
// Given
const localesPath = joinPath(tmpDir, 'locales')
const enDefault = joinPath(localesPath, 'en.default.json')
const es = joinPath(localesPath, 'es.json')

await mkdir(localesPath)
await writeFile(enDefault, JSON.stringify({hello: 'Hello'}))
const bigArray = new Array(6000).fill('a')
await writeFile(es, JSON.stringify(bigArray))

// When
const got = loadLocalesConfig(tmpDir, 'checkout_ui')
await expect(got).rejects.toThrow(/Error loading checkout_ui/)
})
})
// test('Throws if one locale is too big', async () => {
// await inTemporaryDirectory(async (tmpDir: string) => {
// // Given
// const localesPath = joinPath(tmpDir, 'locales')
// const enDefault = joinPath(localesPath, 'en.default.json')
// const es = joinPath(localesPath, 'es.json')

// await mkdir(localesPath)
// await writeFile(enDefault, JSON.stringify({hello: 'Hello'}))
// const bigArray = new Array(6000).fill('a')
// await writeFile(es, JSON.stringify(bigArray))

// // When
// const got = loadLocalesConfig(tmpDir, 'checkout_ui')
// await expect(got).rejects.toThrow(/Error loading checkout_ui/)
// })
// })

test('Throws if there are no defaults', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
Expand Down Expand Up @@ -93,22 +93,22 @@ describe('loadLocalesConfig', () => {
})
})

test('Throws if bundle is too big', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
// Given
const localesPath = joinPath(tmpDir, 'locales')
const en = joinPath(localesPath, 'en.default.json')
const es = joinPath(localesPath, 'es.json')
// test('Throws if bundle is too big', async () => {
// await inTemporaryDirectory(async (tmpDir: string) => {
// // Given
// const localesPath = joinPath(tmpDir, 'locales')
// const en = joinPath(localesPath, 'en.default.json')
// const es = joinPath(localesPath, 'es.json')

await mkdir(localesPath)
const bigArray = JSON.stringify(new Array(4000).fill('a'))
// await mkdir(localesPath)
// const bigArray = JSON.stringify(new Array(4000).fill('a'))

await writeFile(en, JSON.stringify(bigArray))
await writeFile(es, JSON.stringify(bigArray))
// await writeFile(en, JSON.stringify(bigArray))
// await writeFile(es, JSON.stringify(bigArray))

// When
const got = loadLocalesConfig(tmpDir, 'checkout_ui')
await expect(got).rejects.toThrow(/Error loading checkout_ui/)
})
})
// // When
// const got = loadLocalesConfig(tmpDir, 'checkout_ui')
// await expect(got).rejects.toThrow(/Error loading checkout_ui/)
// })
// })
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {glob} from '@shopify/cli-kit/node/fs'
import {AbortError, BugError} from '@shopify/cli-kit/node/error'
import fs from 'fs'

const L10N_FILE_SIZE_LIMIT = 20 * 1024
const L10N_BUNDLE_SIZE_LIMIT = 256 * 1024
// const L10N_FILE_SIZE_LIMIT = 20 * 1024
// const L10N_BUNDLE_SIZE_LIMIT = 256 * 1024

export async function loadLocalesConfig(extensionPath: string, extensionIdentifier: string) {
const localesPaths = await glob(joinPath(extensionPath, 'locales/*.json'))
if (localesPaths.length === 0) return {}

// Bundle validations
const totalBundleSize = bundleSize(localesPaths)
// const totalBundleSize = bundleSize(localesPaths)
const defaultLanguageCode = findDefaultLocale(localesPaths)

if (defaultLanguageCode.length === 0)
Expand All @@ -26,20 +26,20 @@ export async function loadLocalesConfig(extensionPath: string, extensionIdentifi
`There must be one (and only one) locale identified as the default locale: e.g. "en.default.json"`,
)

if (totalBundleSize > L10N_BUNDLE_SIZE_LIMIT)
throw new AbortError(
`Error loading ${extensionIdentifier}`,
`Total size of all locale files must be less than ${L10N_BUNDLE_SIZE_LIMIT}`,
)
// if (totalBundleSize > L10N_BUNDLE_SIZE_LIMIT)
// throw new AbortError(
// `Error loading ${extensionIdentifier}`,
// `Total size of all locale files must be less than ${L10N_BUNDLE_SIZE_LIMIT}`,
// )

// Locale validations
for (const locale of localesPaths) {
const size = fs.statSync(locale).size
if (size > L10N_FILE_SIZE_LIMIT)
throw new AbortError(
`Error loading ${extensionIdentifier}`,
`Locale file ${locale} size must be less than ${L10N_FILE_SIZE_LIMIT}`,
)
// if (size > L10N_FILE_SIZE_LIMIT)
// throw new AbortError(
// `Error loading ${extensionIdentifier}`,
// `Locale file ${locale} size must be less than ${L10N_FILE_SIZE_LIMIT}`,
// )
if (size === 0) throw new AbortError(`Error loading ${extensionIdentifier}`, `Locale file ${locale} can't be empty`)
}

Expand All @@ -64,9 +64,9 @@ function getAllLocales(localesPath: string[]) {
return all
}

function bundleSize(localesPaths: string[]) {
return localesPaths.map((locale) => fs.statSync(locale).size).reduce((acc, size) => acc + size, 0)
}
// function bundleSize(localesPaths: string[]) {
// return localesPaths.map((locale) => fs.statSync(locale).size).reduce((acc, size) => acc + size, 0)
// }

function failIfUnset<T>(value: T | undefined, message: string) {
if (value === undefined) {
Expand Down
Loading