Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit 3ec7623

Browse files
committed
use fs.readFileSync instead of require when loading json files
1 parent a588022 commit 3ec7623

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/main.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function isBoolean(value: any): value is boolean {
2525
return value === true || value === false;
2626
}
2727

28+
function readJsonFileSync<T = any>(filename: string): T {
29+
return JSON.parse(fs.readFileSync(filename, 'utf8')) as T;
30+
}
31+
2832
export enum MessageFormat {
2933
file = 'file',
3034
bundle = 'bundle',
@@ -148,8 +152,8 @@ function initializeSettings() {
148152
if (isString(vscodeOptions._translationsConfigFile)) {
149153
options.translationsConfigFile = vscodeOptions._translationsConfigFile;
150154
try {
151-
options.translationsConfig = require(options.translationsConfigFile);
152-
} catch(error) {
155+
options.translationsConfig = readJsonFileSync(options.translationsConfigFile);
156+
} catch (error) {
153157
// We can't read the translation config file. Mark the cache as corrupted.
154158
if (vscodeOptions._corruptedFile) {
155159
fs.writeFile(vscodeOptions._corruptedFile, 'corrupted', 'utf8', (err) => {
@@ -292,7 +296,7 @@ function mkdir(directory: string) {
292296
}
293297

294298
function createDefaultNlsBundle(folder: string): NlsBundle {
295-
let metaData: MetaDataFile = require(path.join(folder, 'nls.metadata.json'));
299+
let metaData: MetaDataFile = readJsonFileSync(path.join(folder, 'nls.metadata.json'));
296300
let result: NlsBundle = Object.create(null);
297301
for (let module in metaData) {
298302
let entry = metaData[module];
@@ -306,8 +310,8 @@ function createNLSBundle(header: MetadataHeader, metaDataPath: string): NlsBundl
306310
if (!languagePackLocation) {
307311
return undefined;
308312
}
309-
let languagePack: I18nBundle = require(languagePackLocation).contents;
310-
let metaData: MetaDataFile = require(path.join(metaDataPath, 'nls.metadata.json'));
313+
let languagePack: I18nBundle = readJsonFileSync(languagePackLocation).contents;
314+
let metaData: MetaDataFile = readJsonFileSync(path.join(metaDataPath, 'nls.metadata.json'));
311315
let result: NlsBundle = Object.create(null);
312316
for (let module in metaData) {
313317
let entry = metaData[module];
@@ -419,7 +423,7 @@ function loadNlsBundle(header: MetadataHeader, bundlePath: string): NlsBundle |
419423
let candidate = findInTheBoxBundle(bundlePath);
420424
if (candidate) {
421425
try {
422-
return require(candidate);
426+
return readJsonFileSync(candidate);
423427
} catch (err) {
424428
console.log(`Loading in the box message bundle failed.`, err);
425429
}
@@ -496,7 +500,7 @@ export function loadMessageBundle(file?: string): LocalizeFunc {
496500
if (options.messageFormat === MessageFormat.both || options.messageFormat === MessageFormat.file) {
497501
// Try to load a single file bundle
498502
try {
499-
let json: SingleFileJsonFormat = require(resolveLanguage(file));
503+
let json: SingleFileJsonFormat = readJsonFileSync(resolveLanguage(file));
500504
if (Array.isArray(json)) {
501505
return createScopedLocalizeFunction(json);
502506
} else {
@@ -529,7 +533,7 @@ export function config(opts?: Options): LoadFunc {
529533
resolvedBundles = Object.create(null);
530534
}
531535
if (opts.messageFormat !== undefined) {
532-
options.messageFormat = opts.messageFormat;
536+
options.messageFormat = opts.messageFormat;``
533537
}
534538
}
535539
isPseudo = options.locale === 'pseudo';

0 commit comments

Comments
 (0)