forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathremoteLanguagePacks.ts
101 lines (92 loc) · 3.26 KB
/
remoteLanguagePacks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fs from 'fs';
import { FileAccess } from 'vs/base/common/network';
import * as path from 'vs/base/common/path';
import { URI } from 'vs/base/common/uri';
import * as lp from 'vs/base/node/languagePacks';
import product from 'vs/platform/product/common/product';
const metaData = path.join(FileAccess.asFileUri('', require).fsPath, 'nls.metadata.json');
const _cache: Map<string, Promise<lp.NLSConfiguration>> = new Map();
function exists(file: string) {
return new Promise(c => fs.exists(file, c));
}
export function getNLSConfiguration(language: string, userDataPath: string): Promise<lp.NLSConfiguration> {
return exists(metaData).then((fileExists) => {
if (!fileExists || !product.commit) {
// console.log(`==> MetaData or commit unknown. Using default language.`);
return Promise.resolve({ locale: 'en', availableLanguages: {} });
}
let key = `${language}||${userDataPath}`;
let result = _cache.get(key);
if (!result) {
result = lp.getNLSConfiguration(product.commit, userDataPath, metaData, language).then(value => {
if (InternalNLSConfiguration.is(value)) {
value._languagePackSupport = true;
}
/**
* If the configuration has no results keep trying since code-server
* doesn't restart when a language is installed so this result would
* persist (the plugin might not be installed yet or something).
*
* @author coder
*/
if (value.locale !== 'en' && value.locale !== 'en-us' && Object.keys(value.availableLanguages).length === 0) {
_cache.delete(key);
}
return value;
});
_cache.set(key, result);
}
return result;
});
}
export namespace InternalNLSConfiguration {
export function is(value: lp.NLSConfiguration): value is lp.InternalNLSConfiguration {
let candidate: lp.InternalNLSConfiguration = value as lp.InternalNLSConfiguration;
return candidate && typeof candidate._languagePackId === 'string';
}
}
/**
* @author coder
*/
export const getLocaleFromConfig = async (argvResource: URI): Promise<string> => {
try {
const content = stripComments(await fs.promises.readFile(argvResource.fsPath, 'utf8'));
return JSON.parse(content).locale;
} catch (error) {
if (error.code !== "ENOENT") {
console.warn(error)
}
return 'en';
}
};
/**
* Taken from src/main.js.
*
* @author coder
*/
const stripComments = (content: string): string => {
const regexp = /('(?:[^\\']*(?:\\.)?)*')|('(?:[^\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
return content.replace(regexp, (match, _m1, _m2, m3, m4) => {
// Only one of m1, m2, m3, m4 matches
if (m3) {
// A block comment. Replace with nothing
return '';
} else if (m4) {
// A line comment. If it ends in \r?\n then keep it.
const length_1 = m4.length;
if (length_1 > 2 && m4[length_1 - 1] === '\n') {
return m4[length_1 - 2] === '\r' ? '\r\n' : '\n';
}
else {
return '';
}
} else {
// We match a string
return match;
}
});
};