orc-scripts 4.0.3 → 4.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/scripts/generateApi.js +66 -39
- package/src/scripts/start.js +48 -3
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ const lodash = require("lodash");
|
|
|
5
5
|
const occUrl = process.env.OccUrl;
|
|
6
6
|
const occToken = process.env.OccToken;
|
|
7
7
|
let outputFile = "";
|
|
8
|
+
let requestsFile = "";
|
|
8
9
|
|
|
9
10
|
if (process.argv.includes("--outputFile")) {
|
|
10
11
|
outputFile = process.argv[process.argv.indexOf("--outputFile") + 1];
|
|
@@ -14,6 +15,10 @@ if (!outputFile) {
|
|
|
14
15
|
throw new Error("Missing --outputFile 'file' argument.");
|
|
15
16
|
}
|
|
16
17
|
|
|
18
|
+
if (process.argv.includes("--requestsFile")) {
|
|
19
|
+
requestsFile = process.argv[process.argv.indexOf("--requestsFile") + 1];
|
|
20
|
+
}
|
|
21
|
+
|
|
17
22
|
if (!occToken) {
|
|
18
23
|
throw new Error(
|
|
19
24
|
"Missing OccToken environment variable. This environment variable needs to contains the authentication token used to access the OCC platform specified in OccUrl.",
|
|
@@ -40,7 +45,7 @@ function isVerbAllowed(verb) {
|
|
|
40
45
|
return lowerCaseVerb === "get" || lowerCaseVerb === "post" || lowerCaseVerb === "put" || lowerCaseVerb === "delete";
|
|
41
46
|
}
|
|
42
47
|
|
|
43
|
-
function generateOperationsFromPath(url, pathData) {
|
|
48
|
+
function generateOperationsFromPath(url, pathData, desiredRequests) {
|
|
44
49
|
const operations = [];
|
|
45
50
|
|
|
46
51
|
for (const verb of Object.getOwnPropertyNames(pathData)) {
|
|
@@ -50,8 +55,15 @@ function generateOperationsFromPath(url, pathData) {
|
|
|
50
55
|
|
|
51
56
|
const operation = pathData[verb];
|
|
52
57
|
|
|
58
|
+
const operationName = extractRequestNameFromOperation(operation);
|
|
59
|
+
|
|
60
|
+
const shouldAddOperation = desiredRequests == null || desiredRequests.includes(operationName.toLowerCase());
|
|
61
|
+
if( !shouldAddOperation ){
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
53
65
|
operations.push({
|
|
54
|
-
name:
|
|
66
|
+
name: operationName,
|
|
55
67
|
url: url,
|
|
56
68
|
verb: verb.toUpperCase(),
|
|
57
69
|
hasQueryString: operation.parameters.filter(p => p.in === "query").length > 0,
|
|
@@ -102,51 +114,66 @@ function generateImports() {
|
|
|
102
114
|
return 'import { buildUrl } from "../utils/buildUrl";\n\n';
|
|
103
115
|
}
|
|
104
116
|
|
|
105
|
-
|
|
106
|
-
.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (resp.statusCode !== 200) {
|
|
111
|
-
error = new Error(`Request Failed. Status Code: ${resp.statusCode}`);
|
|
112
|
-
}
|
|
113
|
-
if (error) {
|
|
114
|
-
console.error(error.message);
|
|
115
|
-
// Consume response data to free up memory
|
|
116
|
-
resp.resume();
|
|
117
|
-
return;
|
|
117
|
+
if (requestsFile) {
|
|
118
|
+
fs.readFile(requestsFile, 'utf-8', (err, data) => {
|
|
119
|
+
if (err) {
|
|
120
|
+
return console.error(err);
|
|
118
121
|
}
|
|
122
|
+
const desiredRequests = data.split("\n").map(d => d.trim().toLowerCase());
|
|
123
|
+
generate(desiredRequests);
|
|
124
|
+
})
|
|
125
|
+
}else {
|
|
126
|
+
generate(null);
|
|
127
|
+
}
|
|
119
128
|
|
|
120
|
-
// A chunk of data has been received.
|
|
121
|
-
resp.on("data", chunk => {
|
|
122
|
-
data += chunk;
|
|
123
|
-
});
|
|
124
129
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
let
|
|
130
|
+
function generate(desiredRequests) {
|
|
131
|
+
https
|
|
132
|
+
.get(occUrl, { headers: { "X-AUTH": occToken } }, resp => {
|
|
133
|
+
let data = "";
|
|
134
|
+
let error = "";
|
|
130
135
|
|
|
131
|
-
|
|
132
|
-
|
|
136
|
+
if (resp.statusCode !== 200) {
|
|
137
|
+
error = new Error(`Request Failed. Status Code: ${resp.statusCode}`);
|
|
138
|
+
}
|
|
139
|
+
if (error) {
|
|
140
|
+
console.error(error.message);
|
|
141
|
+
// Consume response data to free up memory
|
|
142
|
+
resp.resume();
|
|
143
|
+
return;
|
|
133
144
|
}
|
|
134
145
|
|
|
135
|
-
|
|
136
|
-
|
|
146
|
+
// A chunk of data has been received.
|
|
147
|
+
resp.on("data", chunk => {
|
|
148
|
+
data += chunk;
|
|
149
|
+
});
|
|
137
150
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
151
|
+
// The whole response has been received. Print out the result.
|
|
152
|
+
resp.on("end", () => {
|
|
153
|
+
const swaggerMetaData = JSON.parse(data);
|
|
154
|
+
const paths = swaggerMetaData.paths;
|
|
155
|
+
let operations = [];
|
|
156
|
+
|
|
157
|
+
for (const url of Object.getOwnPropertyNames(paths)) {
|
|
158
|
+
operations = operations.concat(generateOperationsFromPath(url, paths[url], desiredRequests));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
let helperData = "/* istanbul ignore file */\n\n";
|
|
162
|
+
helperData += generateImports();
|
|
141
163
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return console.error(err);
|
|
164
|
+
for (const op of lodash.sortBy(operations, [o => o.name])) {
|
|
165
|
+
helperData += generateOperation(op);
|
|
145
166
|
}
|
|
146
|
-
|
|
167
|
+
|
|
168
|
+
fs.writeFile(outputFile, helperData, function (err) {
|
|
169
|
+
if (err) {
|
|
170
|
+
return console.error(err);
|
|
171
|
+
}
|
|
172
|
+
console.log(`File '${outputFile}' has been created`);
|
|
173
|
+
});
|
|
147
174
|
});
|
|
175
|
+
})
|
|
176
|
+
.on("error", err => {
|
|
177
|
+
console.log("Error: " + err.message);
|
|
148
178
|
});
|
|
149
|
-
|
|
150
|
-
.on("error", err => {
|
|
151
|
-
console.log("Error: " + err.message);
|
|
152
|
-
});
|
|
179
|
+
}
|
package/src/scripts/start.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const DevServer = require("webpack-dev-server");
|
|
2
2
|
const webpack = require("webpack");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const fs = require("fs");
|
|
3
5
|
|
|
4
6
|
const HOST = process.env.HOSTNAME || "localhost";
|
|
5
7
|
|
|
@@ -7,6 +9,42 @@ const args = process.argv.slice(2);
|
|
|
7
9
|
const argPort = args.indexOf("--port") !== -1 ? args[args.indexOf("--port") + 1] : null;
|
|
8
10
|
const PORT = argPort || process.env.PORT || 5000;
|
|
9
11
|
|
|
12
|
+
const findSslPasswordFromParametersFile = (certFile) => {
|
|
13
|
+
// reference: https://hals.app/blog/recursively-read-parent-folder-nodejs/
|
|
14
|
+
|
|
15
|
+
let parentDirectory = path.dirname(certFile);
|
|
16
|
+
while (fs.existsSync(parentDirectory)) {
|
|
17
|
+
console.log("Looking for certificate password in: " + parentDirectory)
|
|
18
|
+
const fileToFindPath = path.join(parentDirectory, "parameters.dev.xml");
|
|
19
|
+
if (fs.existsSync(fileToFindPath)) {
|
|
20
|
+
const fileContent = fs.readFileSync(fileToFindPath, "utf8");
|
|
21
|
+
|
|
22
|
+
// Using regex to parse the XML to avoid adding a dependency on an XML package
|
|
23
|
+
|
|
24
|
+
const matchResult = /<param\s+name="SSL_CertificatePfxPassword"\s+value="(?<Value>[^"]+)"\s*\/>/.exec(fileContent);
|
|
25
|
+
if (matchResult && matchResult.groups && matchResult.groups['Value']) {
|
|
26
|
+
console.log("Certificate password found");
|
|
27
|
+
return matchResult.groups['Value'];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
console.warn("Could not find SSL_CertificatePfxPassword in file " + fileToFindPath)
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The trick is here:
|
|
35
|
+
// Using path.dirname() of a directory returns the parent directory!
|
|
36
|
+
const parentDirname = path.dirname(parentDirectory);
|
|
37
|
+
// But only to a certain point (file system root) (So to avoid infinite loops lets stop here)
|
|
38
|
+
if (parentDirectory === parentDirname) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
parentDirectory = parentDirname;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
10
48
|
const config = require("../config/webpack.config.js");
|
|
11
49
|
const options = {
|
|
12
50
|
historyApiFallback: true,
|
|
@@ -19,13 +57,20 @@ const options = {
|
|
|
19
57
|
},
|
|
20
58
|
};
|
|
21
59
|
|
|
22
|
-
if (HOST !== "localhost") {
|
|
23
|
-
options.public = HOST;
|
|
24
|
-
}
|
|
25
60
|
if (HOST !== "localhost" || args.indexOf("--https") !== -1 || process.env.HTTPS) {
|
|
26
61
|
options.server = 'https';
|
|
27
62
|
}
|
|
28
63
|
|
|
64
|
+
if (options.server === 'https' && process.env.SSL_CERT_PATH) {
|
|
65
|
+
options.server = {
|
|
66
|
+
type: 'https',
|
|
67
|
+
options: {
|
|
68
|
+
pfx: process.env.SSL_CERT_PATH,
|
|
69
|
+
passphrase: process.env.SSL_CERT_PASSWORD || findSslPasswordFromParametersFile(process.env.SSL_CERT_PATH),
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
29
74
|
const compiler = webpack(config);
|
|
30
75
|
|
|
31
76
|
const server = new DevServer(options, compiler);
|