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

Commit

Permalink
use fs.readFileSync instead of require when loading json files
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Sep 3, 2018
1 parent a588022 commit 3ec7623
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function isBoolean(value: any): value is boolean {
return value === true || value === false;
}

function readJsonFileSync<T = any>(filename: string): T {
return JSON.parse(fs.readFileSync(filename, 'utf8')) as T;
}

export enum MessageFormat {
file = 'file',
bundle = 'bundle',
Expand Down Expand Up @@ -148,8 +152,8 @@ function initializeSettings() {
if (isString(vscodeOptions._translationsConfigFile)) {
options.translationsConfigFile = vscodeOptions._translationsConfigFile;
try {
options.translationsConfig = require(options.translationsConfigFile);
} catch(error) {
options.translationsConfig = readJsonFileSync(options.translationsConfigFile);
} catch (error) {
// We can't read the translation config file. Mark the cache as corrupted.
if (vscodeOptions._corruptedFile) {
fs.writeFile(vscodeOptions._corruptedFile, 'corrupted', 'utf8', (err) => {
Expand Down Expand Up @@ -292,7 +296,7 @@ function mkdir(directory: string) {
}

function createDefaultNlsBundle(folder: string): NlsBundle {
let metaData: MetaDataFile = require(path.join(folder, 'nls.metadata.json'));
let metaData: MetaDataFile = readJsonFileSync(path.join(folder, 'nls.metadata.json'));
let result: NlsBundle = Object.create(null);
for (let module in metaData) {
let entry = metaData[module];
Expand All @@ -306,8 +310,8 @@ function createNLSBundle(header: MetadataHeader, metaDataPath: string): NlsBundl
if (!languagePackLocation) {
return undefined;
}
let languagePack: I18nBundle = require(languagePackLocation).contents;
let metaData: MetaDataFile = require(path.join(metaDataPath, 'nls.metadata.json'));
let languagePack: I18nBundle = readJsonFileSync(languagePackLocation).contents;
let metaData: MetaDataFile = readJsonFileSync(path.join(metaDataPath, 'nls.metadata.json'));
let result: NlsBundle = Object.create(null);
for (let module in metaData) {
let entry = metaData[module];
Expand Down Expand Up @@ -419,7 +423,7 @@ function loadNlsBundle(header: MetadataHeader, bundlePath: string): NlsBundle |
let candidate = findInTheBoxBundle(bundlePath);
if (candidate) {
try {
return require(candidate);
return readJsonFileSync(candidate);
} catch (err) {
console.log(`Loading in the box message bundle failed.`, err);
}
Expand Down Expand Up @@ -496,7 +500,7 @@ export function loadMessageBundle(file?: string): LocalizeFunc {
if (options.messageFormat === MessageFormat.both || options.messageFormat === MessageFormat.file) {
// Try to load a single file bundle
try {
let json: SingleFileJsonFormat = require(resolveLanguage(file));
let json: SingleFileJsonFormat = readJsonFileSync(resolveLanguage(file));
if (Array.isArray(json)) {
return createScopedLocalizeFunction(json);
} else {
Expand Down Expand Up @@ -529,7 +533,7 @@ export function config(opts?: Options): LoadFunc {
resolvedBundles = Object.create(null);
}
if (opts.messageFormat !== undefined) {
options.messageFormat = opts.messageFormat;
options.messageFormat = opts.messageFormat;``
}
}
isPseudo = options.locale === 'pseudo';
Expand Down

0 comments on commit 3ec7623

Please sign in to comment.