Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/bootstrap-esm.ts
3285 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as fs from 'node:fs';
7
import { register } from 'node:module';
8
import { product, pkg } from './bootstrap-meta.js';
9
import './bootstrap-node.js';
10
import * as performance from './vs/base/common/performance.js';
11
import { INLSConfiguration } from './vs/nls.js';
12
13
// Install a hook to module resolution to map 'fs' to 'original-fs'
14
if (process.env['ELECTRON_RUN_AS_NODE'] || process.versions['electron']) {
15
const jsCode = `
16
export async function resolve(specifier, context, nextResolve) {
17
if (specifier === 'fs') {
18
return {
19
format: 'builtin',
20
shortCircuit: true,
21
url: 'node:original-fs'
22
};
23
}
24
25
// Defer to the next hook in the chain, which would be the
26
// Node.js default resolve if this is the last user-specified loader.
27
return nextResolve(specifier, context);
28
}`;
29
register(`data:text/javascript;base64,${Buffer.from(jsCode).toString('base64')}`, import.meta.url);
30
}
31
32
// Prepare globals that are needed for running
33
globalThis._VSCODE_PRODUCT_JSON = { ...product };
34
globalThis._VSCODE_PACKAGE_JSON = { ...pkg };
35
globalThis._VSCODE_FILE_ROOT = import.meta.dirname;
36
37
//#region NLS helpers
38
39
let setupNLSResult: Promise<INLSConfiguration | undefined> | undefined = undefined;
40
41
function setupNLS(): Promise<INLSConfiguration | undefined> {
42
if (!setupNLSResult) {
43
setupNLSResult = doSetupNLS();
44
}
45
46
return setupNLSResult;
47
}
48
49
async function doSetupNLS(): Promise<INLSConfiguration | undefined> {
50
performance.mark('code/willLoadNls');
51
52
let nlsConfig: INLSConfiguration | undefined = undefined;
53
54
let messagesFile: string | undefined;
55
if (process.env['VSCODE_NLS_CONFIG']) {
56
try {
57
nlsConfig = JSON.parse(process.env['VSCODE_NLS_CONFIG']);
58
if (nlsConfig?.languagePack?.messagesFile) {
59
messagesFile = nlsConfig.languagePack.messagesFile;
60
} else if (nlsConfig?.defaultMessagesFile) {
61
messagesFile = nlsConfig.defaultMessagesFile;
62
}
63
64
globalThis._VSCODE_NLS_LANGUAGE = nlsConfig?.resolvedLanguage;
65
} catch (e) {
66
console.error(`Error reading VSCODE_NLS_CONFIG from environment: ${e}`);
67
}
68
}
69
70
if (
71
process.env['VSCODE_DEV'] || // no NLS support in dev mode
72
!messagesFile // no NLS messages file
73
) {
74
return undefined;
75
}
76
77
try {
78
globalThis._VSCODE_NLS_MESSAGES = JSON.parse((await fs.promises.readFile(messagesFile)).toString());
79
} catch (error) {
80
console.error(`Error reading NLS messages file ${messagesFile}: ${error}`);
81
82
// Mark as corrupt: this will re-create the language pack cache next startup
83
if (nlsConfig?.languagePack?.corruptMarkerFile) {
84
try {
85
await fs.promises.writeFile(nlsConfig.languagePack.corruptMarkerFile, 'corrupted');
86
} catch (error) {
87
console.error(`Error writing corrupted NLS marker file: ${error}`);
88
}
89
}
90
91
// Fallback to the default message file to ensure english translation at least
92
if (nlsConfig?.defaultMessagesFile && nlsConfig.defaultMessagesFile !== messagesFile) {
93
try {
94
globalThis._VSCODE_NLS_MESSAGES = JSON.parse((await fs.promises.readFile(nlsConfig.defaultMessagesFile)).toString());
95
} catch (error) {
96
console.error(`Error reading default NLS messages file ${nlsConfig.defaultMessagesFile}: ${error}`);
97
}
98
}
99
}
100
101
performance.mark('code/didLoadNls');
102
103
return nlsConfig;
104
}
105
106
//#endregion
107
108
export async function bootstrapESM(): Promise<void> {
109
110
// NLS
111
await setupNLS();
112
}
113
114