Skip to content

Commit

Permalink
fix: handle missing locale files when pulling
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Smith committed Nov 19, 2024
1 parent ebdfed3 commit 77416b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
10 changes: 8 additions & 2 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ export default class Pull extends Command {
}
return acc.concat({
title: `Pulling ${current.name}`,
task: async () =>
task: async (ctx, task) =>
await new MasterFile(masterFileToUpdate?.id).show(
current,
configPath
),
).catch((error) => {
if (error.message.includes('no such file or directory')) {
task.skip(`${current.name} failed to pull: This locale file is missing locally. Skipping...`);
} else {
task.skip(`${current.name} failed to pull: ${error.message}: Skipping...`);
}
}),
});
}, [] as Listr.ListrTask<Listr.ListrContext>[])
);
Expand Down
44 changes: 24 additions & 20 deletions src/models/MasterFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export class MasterFile {

const response = await wtiPost('/files', formData, pathParam);
if (!response.ok) {
throw new Error(`Failed to push new master file: ${await response.text()}`);
throw new Error(
`Failed to push new master file: ${await response.text()}`
);
}

const fileId = await response.text();
Expand All @@ -78,25 +80,20 @@ export class MasterFile {
}

async show(file: WTIProjectFile, pathParam?: string) {
try {
const response = await wtiGet(
`/files/${this._masterFileId}/locales/${file.locale_code}`,
pathParam
);
if (!response.ok) {
throw new Error(`Failed to get master file: ${await response.text()}`);
}
const response = await wtiGet(
`/files/${this._masterFileId}/locales/${file.locale_code}`,
pathParam
);
if (!response.ok) {
throw new Error(`Failed to get master file: ${await response.text()}`);
}

const result = (await response.json()) as Translations;
const result = (await response.json()) as Translations;

await writeTranslations(
file.name,
JSON.stringify(replaceNullValues(result), null, 2)
);
} catch (err) {
console.error(String(err));
process.exit(1);
}
await writeTranslations(
file.name,
JSON.stringify(replaceNullValues(result), null, 2)
);
}

async update(
Expand Down Expand Up @@ -125,7 +122,11 @@ export class MasterFile {
formData.append('label', options.label);
}

const response = await wtiPut(`/files/${this._masterFileId}/locales/${locale}`, formData, pathParam);
const response = await wtiPut(
`/files/${this._masterFileId}/locales/${locale}`,
formData,
pathParam
);
if (!response.ok) {
throw new Error(`Failed to get master file: ${await response.text()}`);
}
Expand All @@ -139,7 +140,10 @@ export class MasterFile {
try {
// return 202 if success with empty body
// return 404 with WtiErrorResponse if file not found
const response = await wtiDelete(`/files/${this._masterFileId}`, pathParam);
const response = await wtiDelete(
`/files/${this._masterFileId}`,
pathParam
);

if (response.status !== 202) {
const parsedResponse = (await response.json()) as WtiErrorResponse;
Expand Down

0 comments on commit 77416b6

Please sign in to comment.