-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (59 loc) · 2.63 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
#!/usr/bin/env node
const fs = require('fs');
const {setAllFlags} = require('./component/setFlags');
const args = process.argv.slice(2);
//flags for terminal input
let flagsValues = setAllFlags(args)
if('f' in flagsValues) {
const readStream = fs.createReadStream(flagsValues['f']);
fs.stat(flagsValues['f'], (err, stats) => {
if (err) throw err;
console.log(`Size of file: ${stats.size/1000/1000} mb`);
});
readStream.setEncoding('utf-8');
let header = '';
let itera = 0;
readStream.on('data', async(chunk) => {
const chunkSplit = chunk.split('\n')
for(let i=0;i<chunkSplit.length;i++){
if(i==0 && header.length === 0){
header = chunkSplit[i];
header = header.trim();
header = header.replace('\n','')
}
if('s' in flagsValues){
if(chunkSplit[i].match(flagsValues['s'],'i'))
console.log('\x1b[32m%s\x1b[0m',chunkSplit[i])
// else
// console.log('\x1b[31m%s\x1b[0m',chunkSplit[i])
}
else if('g' in flagsValues && !('o' in flagsValues) && i>0){
console.log(`INSERT INTO ${flagsValues['f']} (${header.replaceAll('\n','')}) VALUES (${chunkSplit[i]})`)
}
else if('g' in flagsValues && ('o' in flagsValues && flagsValues['g'] === 'sql') && i>0){
chunkSplit[i] = chunkSplit[i].replace('\n\r','')
chunkSplit[i]= chunkSplit[i].trim();
const getSQl = `INSERT INTO ${flagsValues['f']} (${header}) VALUES (${chunkSplit[i]})\n`;
if(chunkSplit[i].length>0){
fs.appendFile(flagsValues['o'],getSQl, (err) => {
if (err) throw err;
});
}
}
else if('g' in flagsValues && ('o' in flagsValues && flagsValues['o'] === 'json') && i>0){
chunkSplit[i] = chunkSplit[i].replace('\n\r','')
chunkSplit[i]= chunkSplit[i].trim();
const getSQl = `INSERT INTO DIMONLOOKUPSEX8277 (${header}) VALUES (${chunkSplit[i]})`;
if(chunkSplit[i].length>0){
fs.appendFile(flagsValues['o'],getSQl, (err) => {
if (err) throw err;
});
}
}
}
});
}
else if ('v' in flagsValues)
console.log(flagsValues['v'])
else if('h' in flagsValues)
console.log(fs.readFileSync(`${__dirname}/cmd/help.txt`, 'utf8'));