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

Migrate localization validations to Core #5165

Merged
merged 1 commit into from
Jan 16, 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/pink-guests-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/app': patch
---

Remove localization file size validations from the CLI and move them into Shopify's backend.
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@ 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 there are no defaults', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
// Given
Expand Down Expand Up @@ -92,23 +74,4 @@ describe('loadLocalesConfig', () => {
await expect(got).rejects.toThrow(/Error loading checkout_ui/)
})
})

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 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/)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ 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

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 defaultLanguageCode = findDefaultLocale(localesPaths)

if (defaultLanguageCode.length === 0)
Expand All @@ -26,20 +22,9 @@ 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}`,
)

// Locale validations
for (const locale of localesPaths) {
const size = fs.statSync(locale).size
if (size > L10N_FILE_SIZE_LIMIT)
throw new AbortError(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will users continue to see a validation error if it's too big? Essentially we're just moving the validation from pre to post-upload attempt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly, users will still see the validation, but now they will be coming the post-upload system.

`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,10 +49,6 @@ 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 failIfUnset<T>(value: T | undefined, message: string) {
if (value === undefined) {
throw new BugError(message)
Expand Down
Loading