forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocFileParser.ts
More file actions
72 lines (63 loc) · 2.01 KB
/
Copy pathLocFileParser.ts
File metadata and controls
72 lines (63 loc) · 2.01 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as jju from 'jju';
import { loader } from 'webpack';
import {
Logging,
ILoggerOptions
} from './Logging';
import { ILocalizationFile } from '../interfaces';
import { ResxReader } from './ResxReader';
import { Constants } from './Constants';
/**
* @internal
*/
export interface IParseLocFileOptions {
loggerOptions: ILoggerOptions;
filePath: string;
content: string;
}
interface IParseCacheEntry {
content: string;
parsedFile: ILocalizationFile;
}
const parseCache: Map<string, IParseCacheEntry> = new Map<string, IParseCacheEntry>();
/**
* @internal
*/
export class LocFileParser {
public static parseLocFileFromLoader(content: string, loaderContext: loader.LoaderContext): ILocalizationFile {
return LocFileParser.parseLocFile({
filePath: loaderContext.resourcePath,
loggerOptions: { writeError: loaderContext.emitError, writeWarning: loaderContext.emitWarning },
content
});
}
public static parseLocFile(options: IParseLocFileOptions): ILocalizationFile {
if (parseCache.has(options.filePath)) {
const entry: IParseCacheEntry = parseCache.get(options.filePath)!;
if (entry.content === options.content) {
return entry.parsedFile;
}
}
let parsedFile: ILocalizationFile;
if (/\.resx$/i.test(options.filePath)) {
parsedFile = ResxReader.readResxAsLocFile(
options.content,
{
...Logging.getLoggingFunctions(options.loggerOptions),
resxFilePath: options.filePath
}
);
} else {
parsedFile = jju.parse(options.content);
try {
Constants.LOC_JSON_SCHEMA.validateObject(parsedFile, options.filePath);
} catch (e) {
options.loggerOptions.writeError(`The loc file is invalid. Error: ${e}`);
}
}
parseCache.set(options.filePath, { content: options.content, parsedFile });
return parsedFile;
}
}