Skip to content

Commit 9de14df

Browse files
feat(cli): add --cache-only flag to replexica i18n (#52)
* feat(cli): add `--cache-only` flag to `replexica i18n` * chore: add changeset
1 parent a49de3f commit 9de14df

File tree

2 files changed

+71
-59
lines changed

2 files changed

+71
-59
lines changed

.changeset/angry-drinks-attend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"replexica": minor
3+
---
4+
5+
add --cache-only flag for working with API's cache mode

packages/cli/src/i18n.ts

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,78 @@ export default new Command()
1414
.command('i18n')
1515
.description('Process i18n with Replexica')
1616
.helpOption('-h, --help', 'Show help')
17-
.action(async () => {
18-
const authStatus = await checkAuth();
19-
if (!authStatus) {
20-
return process.exit(1);
21-
}
22-
17+
.option('--cache-only', 'Only use cached data, and fail if there is new i18n data to process')
18+
.action(async (options) => {
2319
const spinner = Ora();
24-
spinner.start('Loading Replexica build data...');
25-
const buildData = await loadBuildData();
26-
if (!buildData) {
27-
spinner.fail(`Couldn't load Replexica build data. Did you forget to build your app?`);
28-
return process.exit(1);
29-
}
30-
31-
const localeSource = buildData.settings?.locale?.source;
32-
if (!localeSource) {
33-
spinner.fail(`No source locale found in Replexica build data. Please check your Replexica configuration and try again.`);
34-
return process.exit(1);
35-
}
36-
37-
const localeTargets = buildData.settings?.locale?.targets || [];
38-
if (!localeTargets.length) {
39-
spinner.fail(`No target locales found in Replexica build data. Please check your Replexica configuration and try again.`);
40-
return process.exit(1);
41-
}
42-
43-
const localeSourceData = await loadLocaleData(localeSource);
44-
if (!localeSourceData) {
45-
spinner.fail(`Couldn't load source locale data for source locale ${localeSource}. Did you forget to build your app?`);
46-
return process.exit(1);
47-
}
48-
49-
spinner.succeed('Replexica build data loaded!');
50-
51-
const workflowId = createId();
52-
for (let i = 0; i < localeTargets.length; i++) {
53-
const targetLocale = localeTargets[i];
54-
const resultData: any = {};
55-
56-
const localeEntries = Object.entries(localeSourceData || {});
57-
for (let j = 0; j < localeEntries.length; j++) {
58-
const [localeFileId, localeFileData] = localeEntries[j];
59-
spinner.start(`[${targetLocale}] Processing file ${j + 1}/${localeEntries.length}...`);
6020

61-
const partialLocaleData = { [localeFileId]: localeFileData };
62-
const result = await processI18n(
63-
{ workflowId },
64-
{ source: localeSource, target: targetLocale },
65-
buildData.meta,
66-
partialLocaleData,
67-
);
68-
resultData[localeFileId] = result.data[localeFileId];
69-
70-
spinner.succeed(`[${targetLocale}] File ${j + 1}/${localeEntries.length} processed!`);
21+
try {
22+
const authStatus = await checkAuth();
23+
if (!authStatus) {
24+
return process.exit(1);
7125
}
72-
73-
await saveFullLocaleData(targetLocale, resultData);
74-
await saveClientLocaleData(targetLocale, resultData, buildData.meta);
26+
27+
spinner.start('Loading Replexica build data...');
28+
const buildData = await loadBuildData();
29+
if (!buildData) {
30+
spinner.fail(`Couldn't load Replexica build data. Did you forget to build your app?`);
31+
return process.exit(1);
32+
}
33+
34+
const localeSource = buildData.settings?.locale?.source;
35+
if (!localeSource) {
36+
spinner.fail(`No source locale found in Replexica build data. Please check your Replexica configuration and try again.`);
37+
return process.exit(1);
38+
}
39+
40+
const localeTargets = buildData.settings?.locale?.targets || [];
41+
if (!localeTargets.length) {
42+
spinner.fail(`No target locales found in Replexica build data. Please check your Replexica configuration and try again.`);
43+
return process.exit(1);
44+
}
45+
46+
const localeSourceData = await loadLocaleData(localeSource);
47+
if (!localeSourceData) {
48+
spinner.fail(`Couldn't load source locale data for source locale ${localeSource}. Did you forget to build your app?`);
49+
return process.exit(1);
50+
}
51+
52+
spinner.succeed('Replexica data loaded!');
53+
54+
const workflowId = createId();
55+
for (let i = 0; i < localeTargets.length; i++) {
56+
const targetLocale = localeTargets[i];
57+
const resultData: any = {};
58+
59+
const localeEntries = Object.entries(localeSourceData || {});
60+
for (let j = 0; j < localeEntries.length; j++) {
61+
const [localeFileId, localeFileData] = localeEntries[j];
62+
spinner.start(`[${targetLocale}] Processing file ${j + 1}/${localeEntries.length}...`);
63+
64+
const partialLocaleData = { [localeFileId]: localeFileData };
65+
const result = await processI18n(
66+
{ workflowId, cacheOnly: !!options.cacheOnly },
67+
{ source: localeSource, target: targetLocale },
68+
buildData.meta,
69+
partialLocaleData,
70+
);
71+
resultData[localeFileId] = result.data[localeFileId];
72+
73+
spinner.succeed(`[${targetLocale}] File ${j + 1}/${localeEntries.length} processed.`);
74+
}
75+
76+
await saveFullLocaleData(targetLocale, resultData);
77+
await saveClientLocaleData(targetLocale, resultData, buildData.meta);
78+
}
79+
80+
spinner.succeed('Replexica processing complete!');
81+
} catch (error: any) {
82+
spinner.fail(`Failed to process i18n: ${error.message}`);
83+
return process.exit(1);
7584
}
76-
77-
spinner.succeed('Replexica i18n processing complete!');
7885
});
7986

8087
async function processI18n(
81-
params: { workflowId: string },
88+
params: { workflowId: string, cacheOnly: boolean },
8289
locale: { source: string, target: string },
8390
meta: any,
8491
data: any,
@@ -101,7 +108,7 @@ async function processI18n(
101108
});
102109
if (!res.ok) {
103110
const errorText = await res.text();
104-
throw new Error(`Failed to process i18n: ${errorText}`);
111+
throw new Error(errorText);
105112
}
106113
const payload = await res.json();
107114
return payload;

0 commit comments

Comments
 (0)