Skip to content

Commit bc5a28c

Browse files
committed
fix(spec): fix locale codes imports/exports
1 parent cec9f49 commit bc5a28c

7 files changed

Lines changed: 36 additions & 28 deletions

File tree

.changeset/angry-pugs-smoke.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@replexica/spec": patch
3+
"@replexica/cli": patch
4+
"replexica": patch
5+
---
6+
7+
Fix spec imports

packages/cli/src/cli/i18n.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { loadConfig } from './../workers/config';
44
import { loadSettings } from './../workers/settings';
55
import Ora from 'ora';
66
import _ from 'lodash';
7-
import { bucketTypeSchema, targetLocaleSchema } from '@replexica/spec';
7+
import { bucketTypeSchema, localeCodeSchema } from '@replexica/spec';
88
import { createLockfileProcessor } from './../workers/lockfile';
99
import { createAuthenticator } from './../workers/auth';
1010
import { ReplexicaEngine } from '@replexica/sdk';
@@ -216,9 +216,9 @@ export default new Command()
216216

217217
async function loadFlags(options: any) {
218218
return Z.object({
219-
locale: targetLocaleSchema.optional(),
219+
locale: localeCodeSchema.optional(),
220220
bucket: bucketTypeSchema.optional(),
221221
force: Z.boolean().optional(),
222222
frozen: Z.boolean().optional(),
223223
}).parse(options);
224-
}
224+
}

packages/cli/src/cli/show/locale.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Command } from "commander";
22
import _ from "lodash";
33
import Z from 'zod';
44
import Ora from 'ora';
5-
import { sourceLocales, targetLocales } from "@replexica/spec";
5+
import { localeCodes } from "@replexica/spec";
66

77
export default new Command()
88
.command("locale")
@@ -16,10 +16,10 @@ export default new Command()
1616
switch (type) {
1717
default: throw new Error(`Invalid type: ${type}`);
1818
case 'sources':
19-
sourceLocales.forEach((locale) => console.log(locale));
19+
localeCodes.forEach((locale) => console.log(locale));
2020
break;
2121
case 'targets':
22-
targetLocales.forEach((locale) => console.log(locale));
22+
localeCodes.forEach((locale) => console.log(locale));
2323
break;
2424
}
2525
} catch (error: any) {

packages/cli/src/workers/bucket/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { allLocalesSchema, bucketTypeSchema } from '@replexica/spec';
1+
import { localeCodeSchema, bucketTypeSchema } from '@replexica/spec';
22
import Z from 'zod';
33
import { createFileLoader, IBucketLoader } from './loader';
44
import { createAndroidParser, createJsonParser, createMarkdownParser, createXcodeParser, createYamlParser, IBucketParser } from './parser';
@@ -63,15 +63,15 @@ type BucketProcessorParams = {
6363

6464
function composeBucketProcessor(bucketPath: string, params: BucketProcessorParams) {
6565
return {
66-
async load(locale: Z.infer<typeof allLocalesSchema>) {
66+
async load(locale: Z.infer<typeof localeCodeSchema>) {
6767
const filePath = await params.pathResolver(locale, bucketPath);
6868
const content = await params.storage.load(locale, filePath);
6969
if (!content) { return {}; }
7070

7171
const payload = await params.parser.deserialize(locale, content);
7272
return payload;
7373
},
74-
async save(locale: Z.infer<typeof allLocalesSchema>, payload: Record<string, string>) {
74+
async save(locale: Z.infer<typeof localeCodeSchema>, payload: Record<string, string>) {
7575
const serialized = await params.parser.serialize(locale, payload);
7676

7777
const filePath = await params.pathResolver(locale, bucketPath);

packages/cli/src/workers/bucket/loader.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { allLocalesSchema } from "@replexica/spec";
1+
import { localeCodeSchema } from "@replexica/spec";
22
import Z from 'zod';
33
import path from 'path';
44
import fs from 'fs';
55

66
export interface IBucketLoader {
7-
load: (locale: Z.infer<typeof allLocalesSchema>, localeFilePath: string) => Promise<any>;
8-
save: (locale: Z.infer<typeof allLocalesSchema>, localeFilePath: string, content: any) => Promise<void>;
7+
load: (locale: Z.infer<typeof localeCodeSchema>, localeFilePath: string) => Promise<any>;
8+
save: (locale: Z.infer<typeof localeCodeSchema>, localeFilePath: string, content: any) => Promise<void>;
99
}
1010

1111
export function createFileLoader(): IBucketLoader {
1212
return {
13-
async load(locale: Z.infer<typeof allLocalesSchema>, localeFilePath: string) {
13+
async load(locale: Z.infer<typeof localeCodeSchema>, localeFilePath: string) {
1414
return fs.existsSync(localeFilePath) ? fs.readFileSync(localeFilePath, "utf8") : null;
1515
},
16-
async save(locale: Z.infer<typeof allLocalesSchema>, localeFilePath: string, content: any) {
16+
async save(locale: Z.infer<typeof localeCodeSchema>, localeFilePath: string, content: any) {
1717
fs.mkdirSync(path.dirname(localeFilePath), { recursive: true });
1818
fs.writeFileSync(localeFilePath, content, "utf8");
1919
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { allLocalesSchema } from "@replexica/spec";
1+
import { localeCodeSchema } from "@replexica/spec";
22
import Z from 'zod';
33
import path from 'path';
44

55
export type BucketPathResolver = {
6-
(locale: Z.infer<typeof allLocalesSchema>, bucketPath: string): Promise<string>;
6+
(locale: Z.infer<typeof localeCodeSchema>, bucketPath: string): Promise<string>;
77
}
88

9-
export async function plainPathResolver(locale: Z.infer<typeof allLocalesSchema>, bucketPath: string) {
9+
export async function plainPathResolver(locale: Z.infer<typeof localeCodeSchema>, bucketPath: string) {
1010
return path.join(process.cwd(), bucketPath);
1111
}
1212

13-
export async function patternPathResolver(locale: Z.infer<typeof allLocalesSchema>, bucketPath: string) {
13+
export async function patternPathResolver(locale: Z.infer<typeof localeCodeSchema>, bucketPath: string) {
1414
if (!bucketPath.includes('[locale]')) {
1515
throw new Error('Bucket path must contain [locale] placeholder');
1616
}
@@ -21,7 +21,7 @@ export async function patternPathResolver(locale: Z.infer<typeof allLocalesSchem
2121
return result;
2222
}
2323

24-
export async function concreteNamePathResolveR(locale: Z.infer<typeof allLocalesSchema>, bucketPath: string) {
24+
export async function concreteNamePathResolveR(locale: Z.infer<typeof localeCodeSchema>, bucketPath: string) {
2525
const result = await plainPathResolver(locale, bucketPath);
2626

2727
const filename = 'Localizable.xcstrings';
@@ -30,4 +30,4 @@ export async function concreteNamePathResolveR(locale: Z.infer<typeof allLocales
3030
}
3131

3232
return result;
33-
}
33+
}

packages/spec/src/locales.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ export type LocaleCodeShort = keyof typeof localeMap;
4949
export type LocaleCodeFull = typeof localeMap[LocaleCodeShort][number];
5050
export type LocaleCode = LocaleCodeShort | LocaleCodeFull;
5151

52-
export const localeCodeSchema = Z.string().refine((value) => {
53-
const existingShortLocaleCode = Object.keys(localeMap).includes(value);
54-
if (existingShortLocaleCode) { return true; }
55-
56-
const existingFullLocaleCode = Object.values(localeMap).flat().includes(value as any);
57-
if (existingFullLocaleCode) { return true; }
52+
export const localeCodesShort = Object.keys(localeMap) as LocaleCodeShort[];
53+
export const localeCodesFull = Object.values(localeMap).flat() as LocaleCodeFull[];
54+
export const localeCodes = [...localeCodesShort, ...localeCodesFull] as LocaleCode[];
5855

59-
return false;
60-
});
56+
export const localeCodeSchema = Z
57+
.string()
58+
.refine(
59+
(value) => localeCodes.includes(value as any),
60+
{ message: 'Invalid locale code' },
61+
);
6162

6263
export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => {
6364
const existingFullLocaleCode = Object.values(localeMap).flat().includes(value as any);

0 commit comments

Comments
 (0)