forked from hmafzal/resx-to-ts-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
252 lines (198 loc) · 9.64 KB
/
index.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
declare const __dirname: string;
declare const require: any;
const addTypeScriptFile = require('add-typescript-file-to-project');
const fs = require('fs');
const mkpath = require('mkpath');
const search = require('recursive-search');
const xml2js = require('xml2js');
const virtualProjectRoot = '\\..\\..\\..\\';
let nestedResources: boolean = true;
interface Dictionary {
[key: string]: string | Dictionary;
}
export function executeResxToTs(typeScriptResourcesNamespace: string, virtualResxFolder: string, virtualTypeScriptFolder: string, generateNestedResources: boolean = true): void {
nestedResources = generateNestedResources;
let files = getFilesFromFolder(virtualResxFolder);
if (files !== undefined && files !== null) {
for (let i = 0, length = files.length; i < length; i++) {
const resxFilename = files[i];
convertResxToTypeScriptModel(resxFilename, typeScriptResourcesNamespace, virtualTypeScriptFolder);
}
}
}
export function executeResxToJson(virtualResxFolder: string, virtualJsonFolder: string, fileNameLanguage?: string, generateNestedResources: boolean = true): void {
nestedResources = generateNestedResources;
let files = getFilesFromFolder(virtualResxFolder);
if (files !== undefined && files !== null) {
for (let i = 0, length = files.length; i < length; i++) {
const resxFilename = files[i];
convertResxToJson(resxFilename, virtualJsonFolder, fileNameLanguage);
}
}
}
function getFilesFromFolder(virtualResxFolder: string): any {
let files: any = null;
if (virtualResxFolder === undefined || virtualResxFolder === '') {
files = search.recursiveSearchSync(/.resx$/, __dirname + virtualProjectRoot );
}
else {
virtualResxFolder = virtualResxFolder.replace(/\//g, '\\');
let safeVirtualFolder = virtualResxFolder;
if (safeVirtualFolder.charAt(0) === '\\')
{
safeVirtualFolder = safeVirtualFolder.substr(1);
}
if (safeVirtualFolder.charAt(safeVirtualFolder.length-1) === '\\')
{
safeVirtualFolder = safeVirtualFolder.substr(0, safeVirtualFolder.length-1);
}
files = search.recursiveSearchSync(/.resx$/, __dirname + virtualProjectRoot + safeVirtualFolder );
}
if (files !== undefined && files !== null) {
const filesAsString = JSON.stringify(files).replace('[', "").replace(']', "");
const splittedFiles = filesAsString.split(',');
let cleanedFiles = splittedFiles.map((fileName) => fileName.trim().replace(/"/g,"").replace(/\\\\/g,"\\"));
return cleanedFiles;
}
}
function convertResxToTypeScriptModel(resxFilename: string, typeScriptResourcesNamespace: string, virtualTypeScriptFolder: string): void {
fs.readFile(resxFilename, function(err: any, data: any) {
const parser = new xml2js.Parser();
parser.parseString(data, function (err: any, result: any) {
if (result !== undefined) {
convertXmlToTypeScriptModelFile(result, resxFilename, typeScriptResourcesNamespace, virtualTypeScriptFolder);
}
});
});
}
function convertResxToJson(resxFilename: string, virtualJsonFolder: string, fileNameLanguage?: string): void {
fs.readFile(resxFilename, function(err: any, data: any) {
const parser = new xml2js.Parser();
parser.parseString(data, function (err: any, result: any) {
if (result !== undefined) {
convertXmlToJsonFile(result, resxFilename, virtualJsonFolder, fileNameLanguage);
}
});
});
}
function convertXmlToDictionary(xmlObject: any) {
let dictionary: Dictionary = {};
if (xmlObject.root.data !== undefined) {
for (let i = 0, nrOfResourcesInFile = xmlObject.root.data.length; i < nrOfResourcesInFile; i++) {
const key = xmlObject.root.data[i].$.name; //
const value = xmlObject.root.data[i].value.toString();
parseToDictionaryItem(key, value, dictionary);
}
}
return dictionary;
}
function parseToDictionaryItem(key: string, value: string, dictionary: Dictionary) {
if (!dictionary) {
dictionary = {};
}
if (!key.length) {
return;
}
let nestedKeyIndex = key.indexOf("_");
if (nestedKeyIndex >= 0 && nestedResources) {
let firstKey = key.substring(0, nestedKeyIndex);
let restKey = key.substring(nestedKeyIndex + 1);
if (!dictionary.hasOwnProperty(firstKey)) {
dictionary[firstKey] = <Dictionary>{};
}
parseToDictionaryItem(restKey, value, <Dictionary>dictionary[firstKey])
} else {
dictionary[key] = value;
}
}
function convertDictionaryToTsMapping(dictionary: Dictionary, nest: number) {
let nestedTabs = "";
for (var i = 0; i < nest; i++) {
nestedTabs += "\t";
}
var childNestedTabs = nestedTabs + "\t";
var result = "{\n";
var keys = Object.keys(dictionary);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = dictionary[key];
if (typeof value == "string") {
result += childNestedTabs + key + ": string";
}
else if (typeof value == "object") {
result += childNestedTabs + key + ": " + convertDictionaryToTsMapping(<Dictionary>value, nest + 1);
}
result += ";\n";
}
result += nestedTabs + "}";
return result;
}
function convertXmlToTypeScriptModelFile(xmlObject: any, resxFilename: string, typeScriptResourcesNamespace: string, virtualTypeScriptFolder: string): void {
const projectRoot = getProjectRoot();
const relativeResxFilename = resxFilename.replace(projectRoot, "").replace(/\\/g, "/");
const className = resxFilename.substr(resxFilename.lastIndexOf("\\") + 1).replace('.resx', '').replace(".", "_");
let content = '// TypeScript Resx model for: ' + relativeResxFilename + '\n' +
'// Auto generated by resx-to-ts-json (npm package)' + '\n' + '\n';
content = content + 'declare module ' + typeScriptResourcesNamespace + ' {\n';
content = content + '\texport class ' + className + ' ';
let dictionary = convertXmlToDictionary(xmlObject);
content = content + convertDictionaryToTsMapping(dictionary, 1);
content = content + '\n}\n';
// Write model if resources found
if (Object.keys(dictionary).length > 0) {
const relativeTsFilename = relativeResxFilename.replace('.resx', '.d.ts');
const tsFileName = resxFilename.replace('.resx', '.d.ts');
if (virtualTypeScriptFolder === undefined || virtualTypeScriptFolder === '') {
// Write the file aside of the the resx file.
fs.writeFile(tsFileName, content, null);
addTypeScriptFile.execute(tsFileName);
}
else {
// Write the file to the given output folder.
const tsFileNameWithoutPath = tsFileName.substr(tsFileName.lastIndexOf('\\') + 1);
const outputFileName = (projectRoot + virtualTypeScriptFolder + '\\' + tsFileNameWithoutPath).split('/').join('\\');
const relativeOutputFileName = virtualTypeScriptFolder + '/' + tsFileNameWithoutPath;
mkpath.sync(projectRoot + virtualTypeScriptFolder, '0700');
fs.writeFile(outputFileName, content, null);
addTypeScriptFile.execute(relativeOutputFileName);
}
}
}
function convertXmlToJsonFile(xmlObject: any, resxFilename: string, virtualJsonFolder: string, fileNameLanguage?: string): void {
const projectRoot = getProjectRoot();
const relativeResxFilename = resxFilename.replace(projectRoot, "").replace(/\\/g, "/");
let dictionary = convertXmlToDictionary(xmlObject);
let content = JSON.stringify(dictionary);
// Write model if resources found
if (Object.keys(dictionary).length > 0) {
const relativeJsonFilename = relativeResxFilename.replace('.resx', '.json');
const jsonFileName = resxFilename.replace('.resx', '.json');
if (virtualJsonFolder === undefined || virtualJsonFolder === '') {
// Write the file aside of the the resx file.
fs.writeFile(jsonFileName, content, null);
}
else {
// Write the file to the given output folder.
let jsonFileNameWithoutPath = jsonFileName.substr(jsonFileName.lastIndexOf('\\') + 1);
if (fileNameLanguage) {
let fileNameWithoutExtension = jsonFileNameWithoutPath.substring(0, jsonFileNameWithoutPath.indexOf(".json"))
jsonFileNameWithoutPath = `${fileNameWithoutExtension}.${fileNameLanguage}.json`;
}
const outputFileName = (projectRoot + virtualJsonFolder + '\\' + jsonFileNameWithoutPath).split('/').join('\\');
const relativeOutputFileName = virtualJsonFolder + '/' + jsonFileNameWithoutPath;
mkpath.sync(projectRoot + virtualJsonFolder, '0700');
fs.writeFile(outputFileName, content, null);
}
}
}
function getProjectRoot(): string {
const splittedDirName = __dirname.split('\\');
const splittedRootDirName: Array<string> = [];
for (let i = 0, length = splittedDirName.length - 3; i < length; i++) {
splittedRootDirName.push(splittedDirName[i]);
}
return splittedRootDirName.join('\\');
}
function decapitalizeFirstLetter(input: string) {
return input.charAt(0).toLowerCase() + input.slice(1);
}