-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix-typings.js
78 lines (67 loc) · 2.79 KB
/
fix-typings.js
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
const fs = require('fs');
const typesSupressions = [
'CS8601',
'CS8603',
'CS8604',
'CS8618',
'CS8619',
'CS0111',
'CA1507',
'CA1715',
'CA1724',
'CA1062',
'CA2227',
'CA1724',
'CA1002',
'CA1034',
'CA1056',
'CA1707',
'CA1720',
'CA1052',
'CA1819',
'CA1716'
]
const operationsSupressions = [
'CS8625',
'CA1052',
'CA2211'
];
let types = fs.readFileSync(__dirname + '/Criipto.Signatures/Models.cs').toString();
let compositionTypes = [...types.matchAll(/public interface (.+) \{/g)].map(result => result[1]);
for (const compositionType of compositionTypes) {
const listRegex = new RegExp(`^(.*?)List<${compositionType}>(.*?)$`, 'gm');
types = types.replace(listRegex, function (match) {
var indent = match.match(/^\s+/);
return indent[0] + '[JsonConverter(typeof(CompositionTypeListConverter))]\n' + match;
});
const typeRegex = new RegExp(`^(.*?)(public|private|protected) ${compositionType} (.*?)$`, 'gm');
types = types.replace(typeRegex, function (match) {
if (match.includes('class') || match.includes('interface')) return match;
var indent = match.match(/^\s+/);
return (indent ? indent[0] : '') + '[JsonConverter(typeof(CompositionTypeConverter))]\n' + match;
});
}
let enumTypes = [...types.matchAll(/public enum (.+) \{/g)].map(result => result[1]);
for (const enumType of enumTypes) {
const typeRegex = new RegExp(`^(.*?)(public|private|protected) enum ${enumType} {((.|\n)*?)}$`, 'gm');
const [match] = Array.from(types.match(typeRegex));
const lines = match.split('\n');
var indent = lines[lines.length - 2].match(/^\s+/);
lines[lines.length - 2] = lines[lines.length - 2] + ',';
lines.splice(lines.length - 1, undefined, indent + 'FUTURE_ADDED_VALUE = 999');
types = types.replace(match, lines.join('\n'));
const converterRegex = new RegExp(`^(.*?)(public|private|protected) ${enumType}(.*?)$`, 'gm');
types = types.replace(converterRegex, function (match) {
if (match.includes('class') || match.includes('interface') || match.includes('enum')) return match;
var indent = match.match(/^\s+/);
return (indent ? indent[0] : '') + '[JsonConverter(typeof(TolerantEnumConverter))]\n' + match;
});
}
types = types.replace('namespace Criipto.Signatures {', 'namespace Criipto.Signatures.Models {');
types = types.replace('public class Types {', '');
types = types.replace(/}(?:\s+)}(?:\s+)$/, '}');
types = typesSupressions.map(s => `#pragma warning disable ${s}`).join('\n') + '\n' + types;
fs.writeFileSync(__dirname + '/Criipto.Signatures/Models.cs', types);
let operations = fs.readFileSync(__dirname + '/Criipto.Signatures/Operations.cs').toString();
operations = operationsSupressions.map(s => `#pragma warning disable ${s}`).join('\n') + '\n' + operations;
fs.writeFileSync(__dirname + '/Criipto.Signatures/Operations.cs', operations);