-
Notifications
You must be signed in to change notification settings - Fork 73
/
fn.js
250 lines (203 loc) · 6.87 KB
/
fn.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Find index at end of param string (not at start)
String.prototype.indexOfEnd = function(string) {
var io = this.indexOf(string);
return io == -1 ? -1 : io + string.length;
}
var
db = require('mime-db'),
path = require('path'),
fs = require('fs');
var functions = {
generateDataOosUtilValues: function(){
console.log('*** generateDataOosUtilValues ***');
var
sqlStmt = '',
temp,
extensions = {}
;
var deleteElements = function(pSearch, pArray){
//Make sure that at least one element exist for each ext
for (var i = 0; i < pArray.length && pArray.length > 1; i++){
// Remove application specific mime types
if (pArray[i].indexOf(pSearch) >= 0){
pArray.splice(i, 1);
i += -1; //Since splice removes element from array need to adjust pointer
}
}
}//deleteElements
// db has the mime-type with an array of extensions
// Reverse this to have extensions with array of mime-types
console.log('Reversing mime-types and extensions');
for (var key in db){
if (db[key].extensions){
var exts = db[key].extensions;
for(var i = 0; i < exts.length; i++){
//Check if it array already exists and if it's unique
if(extensions[exts[i]] && extensions[exts[i]].indexOf(key) < 0){
extensions[exts[i]].push(key);
}
else{
extensions[exts[i]] = [key];
}
}//for i
}
}//for key
// Some extensions will have multiple mime-types.
// Adjust down to one per extension
console.log('Reducing to one mime-type per extension');
for (var ext in extensions){
//Take several passes to get down to one mime-type per extension
var j = 0;
while (extensions[ext].length > 1){
switch(j){
case 0:
//First pass, remove non-standard mime types
deleteElements('/x-', extensions[ext]);
break;
case 1:
deleteElements('application/', extensions[ext]);
case 2:
deleteElements('audio/wave', extensions[ext]);
case 3:
deleteElements('image/', extensions[ext]);
case 4:
deleteElements('audio/', extensions[ext]);
}//switch
j++;
// Error out if we can't filter down
if (j > 10){
console.log('Can\'t filter down mime-type');
console.log(ext, ':', extensions[ext]);
process.exit(1);
}
}//while
}//for duplicates
console.log('Creating insert statement');
//Create insert statement
sqlStmt = '-- This file is generated, do not modify.\n\n';
sqlStmt += 'begin\n delete oos_util_values;\n';
sqlStmt += ' insert all\n';
for(var ext in extensions){
temp = " into oos_util_values(cat, name, value) values('mime-type', '%name%','%value%')\n";
temp = temp.replace(/\%name\%/g, ext);
temp = temp.replace(/\%value\%/g, extensions[ext][0]);
sqlStmt += temp;
}
sqlStmt += ' select 1 from dual;\n';
sqlStmt += 'end;\n'
sqlStmt += '/\n';
sqlStmt += 'commit;\n';
fs.writeFileSync(path.resolve(__dirname,'../data/oos_util_values.sql'), sqlStmt);
},//generateDataOosUtilValues
appendFile : function (pFile, pContent){
fs.appendFileSync(path.resolve(__dirname,pFile), pContent + '\n');
},// appendFile
readFile : function (pFile){
return fs.readFileSync(path.resolve(__dirname,pFile),'utf8');
},// readFile
writeFile : function (pFile, pContent){
fs.writeFileSync(path.resolve(__dirname,pFile), pContent);
},// writeFile
/**
* Checks that package files all contain a "/" at EOF so it will compile properly in SQL*Plus
*
*
* Related Tickets:
* - #37
*
* @param pPackages JSON object of packages (use config.objects.packages)
*/
validatePackages : function(pPackages){
var
contents,
error = false;
for (var packageName in pPackages){
for (var fileExt in pPackages[packageName]){
contents = this.readFile(pPackages[packageName][fileExt]);
// #37
if (contents.search(/\/(\r?\n)*$/) === -1){
console.log('File missing "/" at end: ' + pPackages[packageName][fileExt]);
error = true;
}
}// fileExt
}//packageName
if (error){
process.exit(1);
}
},//validatePackages
/**
* Creates Synonym script
*
*/
createSynonymScript : function (pConfig){
var template = 'create or replace synonym %OBJECT% for &from_user..%OBJECT%;\n';
var temp = this.readFile(pConfig.files.createSynonyms);
temp = temp.slice(0,temp.indexOfEnd('-- AUTOREPLACE_START'));
temp += '\n\n';
// Don't do tables since tables aren't officially supported or necessary yet
for (var package in pConfig.objects.packages){
temp += template.replace(/%OBJECT%/g, package);
}//package
this.writeFile(pConfig.files.createSynonyms, temp);
},//createSynonymScript
/**
* Creates Grant script
*
*/
createGrantScript : function (pConfig){
var template = {
execute : 'grant execute on %OBJECT% to &to_user;\n'
};
var temp = this.readFile(pConfig.files.createGrants);
temp = temp.slice(0,temp.indexOfEnd('-- AUTOREPLACE_START'));
temp += '\n\n';
// Don't do tables since tables aren't officially supported or necessary yet
for (var package in pConfig.objects.packages){
temp += template.execute.replace(/%OBJECT%/g, package);
}//package
this.writeFile(pConfig.files.createGrants, temp);
}, //createGrantScript
/**
* Returns JSON object of parameters
* Will validate and exit if parameters are Invalid
*
*/
getParameters : function(){
// Parameters
var params = {
help : 'Run: node app <version major.minor.patch>',
// If process.argv.splice(2) is called before it will be null here
valArr : process.argv.splice(2), // Array of variables passed in via command line
values : {
version : ''
},
valid : true, // Update if not valid
errMsg : '' // if not valid, then display this message
}// params
if (params.valArr.length < 1){
params.errMsg = 'Missing parameters.';
}
else{
// Define all parameters
params.values.version = params.valArr[0];
}
// Test params
if (!/\d+\.\d+\.\d+/.test(params.values.version)) {
params.errMsg = 'Version needs to be major.minor.patch'
}
// If any parameters are invalid
if (params.errMsg != ''){
console.log(`Error: ${params.errMsg}\n${params.help}`);
process.exit(1);
}
// No errors sanitize and return parameters
var tmpVersion = params.values.version.split('.');
params.values.version = {
major : tmpVersion[0],
minor : tmpVersion[1],
patch : tmpVersion[2]
};
return params.values;
}// getParameters
};// functions
module.exports = functions;