-
Notifications
You must be signed in to change notification settings - Fork 0
/
compileValidSQL.tsx
149 lines (102 loc) · 3.58 KB
/
compileValidSQL.tsx
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
#!/usr/bin/env node
const c = require('ansi-colors');
// Create an object to store the parsed arguments
let args = {
inputDir: './logs/rest/',
outputDir: './logs/rest/',
verbose: false,
};
// parse command line arguments
for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i];
// noinspection FallThroughInSwitchStatementJS
switch (arg) {
case '--input':
case '-i':
args.inputDir = process.argv[++i];
break;
case '--output':
case '-o':
args.outputDir = process.argv[++i];
break;
case '--verbose':
case '-v':
args.verbose = true;
break;
case '--help':
case '-h':
console.log(c.cyan('Usage:'));
console.log(c.cyan('--input or -i: Input directory'));
console.log(c.cyan('--output or -o: Output directory'));
console.log(c.cyan('--verbose or -v: Enable verbose mode'));
process.exit(0);
default:
console.error(`Unknown argument: ${arg}`);
process.exit(1);
}
}
// print parsed arguments if verbose mode is enabled
if (args.verbose) {
console.log(c.cyan('Parsed arguments:'));
console.log(c.cyan(JSON.stringify(args, null, 4)));
}
interface iApiResponseLine {
"method": "GET" | "PUT" | "POST" | "DELETE",
"table": string,
"CarbonPHP\\Restful\\RestSettings::$externalRestfulRequestsAPI": boolean,
"argv": [
any[],
any,
any
],
"stmt": {
// ['stmt']['sql']
sql: string, // sql
injections: { [injectionNumber: string]: string },
debugDumpParams: string[]
}
}
interface iRestfulTestFile {
[testName: string]: iApiResponseLine[]
}
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const fs = require('fs');
(async () => {
let jsonValidSqlFiles: iRestfulTestFile[] = [],
validExternalRequests: iApiResponseLine[] = [];
const ManifestFile = 'validSQL.json';
fs.readdirSync(args.inputDir).forEach((file) => {
if (file === ManifestFile) {
return;
}
const {readFileSync} = require("fs");
const resource = readFileSync(`${args.inputDir}${file}`, 'utf8');
const json = JSON.parse(resource)
if (json === undefined) {
console.error(`Failed to parse ${file} as JSON`);
return;
}
jsonValidSqlFiles.push(json);
});
jsonValidSqlFiles.forEach(apiRequest => {
Object.keys(apiRequest).forEach(testName => {
Object.keys(apiRequest[testName]).forEach(restCallInfo => {
apiRequest[testName][restCallInfo].forEach(singleSqlInfoObject => {
if (true === singleSqlInfoObject['CarbonPHP\\Restful\\RestSettings::$externalRestfulRequestsAPI']) {
singleSqlInfoObject.stmt.debugDumpParams = singleSqlInfoObject.stmt.debugDumpParams.slice(0,2)
// change this to only the sql statement
validExternalRequests.push(singleSqlInfoObject.stmt)
}
})
})
});
});
// @link https://stackoverflow.com/questions/12941083/execute-and-get-the-output-of-a-shell-command-in-node-js
const {stdout} = await exec('git rev-parse HEAD')
const revision = stdout.trim();
fs.writeFileSync(args.outputDir + ManifestFile, JSON.stringify({
"revision": revision,
"validSQL": validExternalRequests
}, undefined, 4));
})()