Skip to content

Commit

Permalink
Download policy translations for previous version if current version …
Browse files Browse the repository at this point in the history
…fails (#154891)

download policy translations for previous version if current version fails
  • Loading branch information
joaomoreno committed Jul 12, 2022
1 parent 004e755 commit 8c6c357
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
23 changes: 22 additions & 1 deletion build/lib/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand All @@ -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);
Expand Down
25 changes: 24 additions & 1 deletion build/lib/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand All @@ -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<Policy[]> {
const parser = new Parser();
parser.setLanguage(typescript);
Expand Down

0 comments on commit 8c6c357

Please sign in to comment.