forked from svenheden/csharp-models-to-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·83 lines (64 loc) · 2.28 KB
/
index.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
79
80
81
82
83
#!/usr/bin/env node
const fs = require('fs');
const process = require('process');
const path = require('path');
const { spawn } = require('child_process');
const createConverter = require('./converter');
const configArg = process.argv.find(x => x.startsWith('--config='));
if (!configArg) {
return console.error('No configuration file for `csharp-models-to-typescript` provided.');
}
const configPath = configArg.substr('--config='.length);
let config;
try {
unparsedConfig = fs.readFileSync(configPath, 'utf8');
} catch (error) {
return console.error(`Configuration file "${configPath}" not found.`);
}
try {
config = JSON.parse(unparsedConfig);
} catch (error) {
return console.error(`Configuration file "${configPath}" contains invalid JSON.`);
}
const output = config.output || 'types.d.ts';
const converter = createConverter({
customTypeTranslations: config.customTypeTranslations || {},
namespace: config.namespace,
camelCase: config.camelCase || false,
camelCaseOptions: config.camelCaseOptions || {},
camelCaseEnums: config.camelCaseEnums || false,
numericEnums: config.numericEnums || false,
omitFilePathComment: config.omitFilePathComment || false,
omitSemicolon: config.omitSemicolon || false,
stringLiteralTypesInsteadOfEnums: config.stringLiteralTypesInsteadOfEnums || false
});
let timer = process.hrtime();
const dotnetProject = path.join(__dirname, 'lib/csharp-models-to-json');
const dotnetProcess = spawn('dotnet', ['run', `--project "${dotnetProject}"`, `"${path.resolve(configPath)}"`], { shell: true });
let stdout = '';
dotnetProcess.stdout.on('data', data => {
stdout += data;
});
dotnetProcess.stderr.on('data', err => {
console.error(err.toString());
});
dotnetProcess.stdout.on('end', () => {
let json;
try {
json = JSON.parse(stdout);
} catch (error) {
return console.error([
'The output from `csharp-models-to-json` contains invalid JSON.',
error.message,
stdout
].join('\n\n'));
}
const types = converter(json);
fs.writeFile(output, types, err => {
if (err) {
return console.error(err);
}
timer = process.hrtime(timer);
console.log('Done in %d.%d seconds.', timer[0], timer[1]);
});
});