From 77416b633e078e491335cb123ece4f5ca4368430 Mon Sep 17 00:00:00 2001 From: Simon Smith Date: Tue, 19 Nov 2024 17:22:09 +0000 Subject: [PATCH] fix: handle missing locale files when pulling --- src/commands/pull.ts | 10 +++++++-- src/models/MasterFile.ts | 44 ++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/commands/pull.ts b/src/commands/pull.ts index e07fc16..e9b03e4 100644 --- a/src/commands/pull.ts +++ b/src/commands/pull.ts @@ -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[]) ); diff --git a/src/models/MasterFile.ts b/src/models/MasterFile.ts index 3f294ba..ad7d2d1 100644 --- a/src/models/MasterFile.ts +++ b/src/models/MasterFile.ts @@ -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(); @@ -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( @@ -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()}`); } @@ -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;