-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathng-ts-codegen.js
167 lines (147 loc) · 5 KB
/
ng-ts-codegen.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
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
#!/usr/bin/env node
/*
* Copyright(c) 1995 - 2018 T-Systems Multimedia Solutions GmbH
* Riesaer Str. 5, 01129 Dresden
* All rights reserved.
*/
const argv = require('yargs').argv;
const fse = require('fs-extra');
const { exec } = require('child_process');
const { resolve } = require('path');
const url = require('url');
const openApiVersion = '4.1.3';
const jarFileName = `openapi-generator-cli-${openApiVersion}.jar`;
const dockerImageName = `openapitools/openapi-generator-cli:v${openApiVersion}`;
/**
* Converts proxy settings into java runtime parameters.
*/
function getProxyArgsForJava() {
let proxyArgs = '';
if (process.env.HTTP_PROXY) {
const parsedUrl = url.parse(process.env.HTTP_PROXY);
proxyArgs = proxyArgs.concat(` -Dhttp.proxyHost=${parsedUrl.hostname} -Dhttp.proxyPort=${parsedUrl.port}`);
}
if (process.env.HTTPS_PROXY) {
const parsedUrl = url.parse(process.env.HTTPS_PROXY);
proxyArgs = proxyArgs.concat(` -Dhttps.proxyHost=${parsedUrl.hostname} -Dhttps.proxyPort=${parsedUrl.port}`);
}
if (process.env.NO_PROXY) {
const noProxyValue = process.env.NO_PROXY.split(',').join('|');
proxyArgs = proxyArgs.concat(` -Dhttp.nonProxyHosts="${noProxyValue}" -Dhttps.nonProxyHosts="${noProxyValue}"`);
}
return proxyArgs;
}
/**
* Converts proxy settings into docker run parameters.
*/
function getProxyArgsForDocker() {
let proxyArgs = '';
if (process.env.HTTP_PROXY) {
proxyArgs = proxyArgs.concat(` --env HTTP_PROXY="${process.env.HTTP_PROXY}"`);
}
if (process.env.HTTPS_PROXY) {
proxyArgs = proxyArgs.concat(` --env HTTPS_PROXY="${process.env.HTTPS_PROXY}"`);
}
if (process.env.NO_PROXY) {
proxyArgs = proxyArgs.concat(` --env NO_PROXY="${process.env.NO_PROXY}"`);
}
return proxyArgs;
}
/**
* Adds the new given additional property to the given map of additional properties.
* @param {Object} additionalProperties map of additional properties
* @param {string} arg additional property to add
*/
function addAdditionalProperty(additionalProperties, arg) {
if (typeof arg !== 'string') {
return;
}
const addArg = arg.split('=', 2);
additionalProperties[addArg[0]] = addArg[1];
}
// Usage
if (argv.help || argv.h) {
console.log('[Usage]');
console.log(
'openapi-typescript-angular-generator -i <openapi-spec> -o <output-destination> [-e <java|docker>] [-m <docker-mount>] [-a <authorization>] [--additional-properties <additional properties>...]'
);
process.exit(0);
}
// input and output must be defined
if (!argv.i) {
console.log('Please provide openapi-file: -i <location>');
process.exit(1);
}
if (!argv.o) {
console.log('Please provide output-directory: -o <location>');
process.exit(1);
}
// define the actual command
let command;
let isDocker = false;
if (argv.e === 'docker') {
// transfer post processing env-variable to docker if it exists
let envArgs = process.env.TS_POST_PROCESS_FILE ? ` --env TS_POST_PROCESS_FILE="${process.env.TS_POST_PROCESS_FILE}"` : ''
// add proxy args
envArgs += getProxyArgsForDocker();
const volume = argv.m || process.env.PWD;
command = `docker run${envArgs} --rm -v ${volume}:/local ${dockerImageName}`;
isDocker = true;
} else {
// default to java
const proxyArgs = getProxyArgsForJava();
command = `java${proxyArgs} -jar ${resolve(__dirname, jarFileName)}`;
}
// join parameters to the command
const isUrlInput = argv.i.startsWith('http://') || argv.i.startsWith('https://');
const args = [
'generate',
`-i ${isDocker && !isUrlInput ? `/local/${argv.i}` : argv.i}`,
`-o ${isDocker ? `/local/${argv.o}` : argv.o}`,
'-g=typescript-angular',
`-t=${
isDocker
? '/local/node_modules/openapi-typescript-angular-generator/src/mustache'
: resolve(__dirname, '../src/mustache')
}`,
];
// enable post processing if environment variable TS_POST_PROCESS_FILE is set
if (process.env.TS_POST_PROCESS_FILE) {
args.push('--enable-post-process-file',)
}
// add auth headers
if (argv.a) {
args.push(`-a ${argv.a}`);
}
// additional properties
const additionalProperties = {
supportsES6: 'true',
ngVersion: '7.0.0',
modelPropertyNaming: 'original',
};
if (argv['additional-properties']) {
if (Array.isArray(argv['additional-properties'])) {
argv['additional-properties'].forEach(arg => {
addAdditionalProperty(additionalProperties, arg);
});
} else {
addAdditionalProperty(additionalProperties, argv['additional-properties']);
}
}
for (const key in additionalProperties) {
if (additionalProperties.hasOwnProperty(key)) {
const element = additionalProperties[key];
args.push(`--additional-properties="${key}=${element}"`);
}
}
// build command
command += ` ${args.join(' ')}`;
// execute
console.log('Executing following command to generate the code:\n', command);
const cmd = exec(command, () => {
// clean up
const files = ['.openapi-generator', '.gitignore', '.openapi-generator-ignore', 'git_push.sh', 'README.md'];
files.forEach(f => fse.remove(resolve(process.cwd(), argv.o, f)));
});
cmd.stdout.pipe(process.stdout);
cmd.stderr.pipe(process.stderr);