Skip to content
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
44 changes: 44 additions & 0 deletions packages/generator/test/get-all-locales.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { suite } from 'uvu'
import * as assert from 'uvu/assert'
import { getAllLocales, type FileSystemUtil } from '../../shared/src/file.utils.mjs'

const test = suite('getAllLocales')

type Dirent = { name: string; isDirectory: () => boolean }

const folder = (name: string): Dirent => ({ name, isDirectory: () => true })
const file = (name: string): Dirent => ({ name, isDirectory: () => false })

const ROOT = './i18n'

// the order in which the filesystem returns directory entries is not guaranteed
// to be stable across platforms or runs, so `getAllLocales` must sort the result
// to produce a deterministic output
const createFileSystem = (rootEntries: Dirent[]): FileSystemUtil => ({
readFile: async () => '',
readdir: async (path) => (String(path) === ROOT ? rootEntries : [file('index.ts')]),
})

test('returns the discovered locales sorted alphabetically', async () => {
const fs = createFileSystem([folder('sk'), folder('cs'), folder('en')])

assert.equal(await getAllLocales(fs, ROOT, 'TypeScript'), ['cs', 'en', 'sk'])
})

test('sorts locales independently of the filesystem order', async () => {
const ascending = createFileSystem([folder('cs'), folder('en'), folder('sk')])
const descending = createFileSystem([folder('sk'), folder('en'), folder('cs')])

assert.equal(await getAllLocales(ascending, ROOT, 'TypeScript'), await getAllLocales(descending, ROOT, 'TypeScript'))
})

test('also sorts when looking for JavaScript locale files', async () => {
const fs: FileSystemUtil = {
readFile: async () => '',
readdir: async (path) => (String(path) === ROOT ? [folder('sk'), folder('cs')] : [file('index.js')]),
}

assert.equal(await getAllLocales(fs, ROOT, 'JavaScript'), ['cs', 'sk'])
})

test.run()
5 changes: 4 additions & 1 deletion packages/shared/src/file.utils.mts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ export const getAllLocales = async (
const fileEnding = outputFormat === 'JavaScript' ? '.js' : '.ts'
const files = await getFiles(fs, path, 1)

return files.filter(({ folder, name }) => folder && name === `index${fileEnding}`).map(({ folder }) => folder)
return files
.filter(({ folder, name }) => folder && name === `index${fileEnding}`)
.map(({ folder }) => folder)
.sort()
}