From 8c6c3576e56dad0f7eaccd21fcd1749b1fe8e5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moreno?= Date: Tue, 12 Jul 2022 07:58:14 +0200 Subject: [PATCH] Download policy translations for previous version if current version fails (#154891) download policy translations for previous version if current version fails --- build/lib/policies.js | 23 ++++++++++++++++++++++- build/lib/policies.ts | 25 ++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/build/lib/policies.js b/build/lib/policies.js index 3e2a10df350e1b..7b1bbdf394acdd 100644 --- a/build/lib/policies.js +++ b/build/lib/policies.js @@ -413,7 +413,7 @@ async function getLatestStableVersion(updateUrl) { const { name: version } = await res.json(); return version; } -async function getNLS(resourceUrlTemplate, languageId, version) { +async function getSpecificNLS(resourceUrlTemplate, languageId, version) { const resource = { publisher: 'ms-ceintl', name: `vscode-language-pack-${languageId}`, @@ -422,9 +422,30 @@ async function getNLS(resourceUrlTemplate, languageId, version) { }; const url = resourceUrlTemplate.replace(/\{([^}]+)\}/g, (_, key) => resource[key]); const res = await (0, node_fetch_1.default)(url); + if (res.status !== 200) { + throw new Error(`[${res.status}] Error downloading language pack ${languageId}@${version}`); + } const { contents: result } = await res.json(); return result; } +function previousVersion(version) { + const [, major, minor, patch] = /^(\d+)\.(\d+)\.(\d+)$/.exec(version); + return `${major}.${parseInt(minor) - 1}.${patch}`; +} +async function getNLS(resourceUrlTemplate, languageId, version) { + try { + return await getSpecificNLS(resourceUrlTemplate, languageId, version); + } + catch (err) { + if (/\[404\]/.test(err.message)) { + console.warn(`Language pack ${languageId}@${version} is missing. Downloading previous version...`); + return await getSpecificNLS(resourceUrlTemplate, languageId, previousVersion(version)); + } + else { + throw err; + } + } +} async function parsePolicies() { const parser = new Parser(); parser.setLanguage(typescript); diff --git a/build/lib/policies.ts b/build/lib/policies.ts index 62ea4d561e59dc..eaa8cb719a25ff 100644 --- a/build/lib/policies.ts +++ b/build/lib/policies.ts @@ -593,7 +593,7 @@ async function getLatestStableVersion(updateUrl: string) { return version; } -async function getNLS(resourceUrlTemplate: string, languageId: string, version: string) { +async function getSpecificNLS(resourceUrlTemplate: string, languageId: string, version: string) { const resource = { publisher: 'ms-ceintl', name: `vscode-language-pack-${languageId}`, @@ -603,10 +603,33 @@ async function getNLS(resourceUrlTemplate: string, languageId: string, version: const url = resourceUrlTemplate.replace(/\{([^}]+)\}/g, (_, key) => resource[key as keyof typeof resource]); const res = await fetch(url); + + if (res.status !== 200) { + throw new Error(`[${res.status}] Error downloading language pack ${languageId}@${version}`); + } + const { contents: result } = await res.json() as { contents: LanguageTranslations }; return result; } +function previousVersion(version: string): string { + const [, major, minor, patch] = /^(\d+)\.(\d+)\.(\d+)$/.exec(version)!; + return `${major}.${parseInt(minor) - 1}.${patch}`; +} + +async function getNLS(resourceUrlTemplate: string, languageId: string, version: string) { + try { + return await getSpecificNLS(resourceUrlTemplate, languageId, version); + } catch (err) { + if (/\[404\]/.test(err.message)) { + console.warn(`Language pack ${languageId}@${version} is missing. Downloading previous version...`); + return await getSpecificNLS(resourceUrlTemplate, languageId, previousVersion(version)); + } else { + throw err; + } + } +} + async function parsePolicies(): Promise { const parser = new Parser(); parser.setLanguage(typescript);