@intecoag/inteco-cli 0.5.1 → 1.0.0

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.
@@ -1,154 +1,154 @@
1
- import fs from "fs";
2
- import nReadlines from 'n-readlines';
3
- import prompts from "prompts";
4
- import path from "path";
5
- import ora from "ora";
6
- import Seven from 'node-7z'
7
- import sevenBin from '7zip-bin'
8
-
9
-
10
- export default async function dumpTableToCSV() {
11
- console.log()
12
-
13
- let success = true;
14
-
15
- const results = await prompts([
16
- {
17
- // Tabelle die exportiert werden soll
18
- type: 'text',
19
- name: 'table',
20
- message: 'Tabellen-Name?'
21
- }
22
- ], {
23
- onCancel: () => {
24
- console.log()
25
- console.log(chalk.red("Cancelled Dump!"))
26
- console.log()
27
- success = false
28
- }
29
- })
30
-
31
-
32
- if (success) {
33
- console.log()
34
-
35
- // Unpack archives
36
- let archives = fs.readdirSync(process.cwd(), { withFileTypes: true }).filter(dirent => dirent.isFile() && (dirent.name.split(".")[dirent.name.split(".").length-1] == "gz") || (dirent.name.split(".")[dirent.name.split(".").length-1] == "7z")).map(dirent => { return dirent.name });
37
- for (const archive of archives){
38
- await extractDumpsFromArchive(archive)
39
- }
40
-
41
- console.log()
42
-
43
- // Read Files and create csv
44
- let files = fs.readdirSync(process.cwd(), { withFileTypes: true }).filter(dirent => dirent.isFile() && dirent.name.split(".")[dirent.name.split(".").length-1] == "sql").map(dirent => { return dirent.name });
45
-
46
- await Promise.all(files.map(async (file) => {
47
- await createCSVDump(file, results.table)
48
- }));
49
-
50
- console.log()
51
- }
52
- }
53
-
54
- function getPromiseFromEvent(item, event) {
55
- return new Promise((resolve) => {
56
- const listener = (data) => {
57
- resolve(data);
58
- }
59
- item.on(event, listener);
60
- })
61
- }
62
-
63
- async function extractDumpsFromArchive(archive){
64
- const spinnerZIP = ora('Unpacking Archive: '+archive).start();
65
-
66
- const list = Seven.list(process.cwd() + path.sep + archive, {
67
- $bin: sevenBin.path7za
68
- })
69
-
70
- const data = await getPromiseFromEvent(list, "data")
71
-
72
- let file = data.file
73
-
74
- if(file.split(".").length == 1){
75
- const rename = Seven.rename(process.cwd() + path.sep + archive,[[file, file+".sql"]], {
76
- $bin: sevenBin.path7za
77
- })
78
-
79
- await getPromiseFromEvent(rename, "end")
80
- }
81
-
82
- const unpack = Seven.extract(process.cwd() + path.sep + archive,"."+path.sep, {
83
- $bin: sevenBin.path7za
84
- })
85
-
86
- await getPromiseFromEvent(unpack, "end")
87
-
88
- spinnerZIP.succeed("Archive unpacked: "+archive)
89
-
90
- }
91
-
92
- async function createCSVDump(file, table) {
93
- let count = 0;
94
- const spinner = ora('Reading file (' + file +"): "+count).start();
95
- const readFile = new nReadlines(process.cwd() + path.sep + file);
96
-
97
- let line = "";
98
- let data = "";
99
- let readHeader = false;
100
- let headerData = "";
101
-
102
- while ((line = readFile.next())) {
103
- line = line.toString('utf-8').trim()
104
-
105
- if (line.includes("INSERT INTO `" + table + "` VALUES")) {
106
- data = data + line;
107
- }
108
-
109
- if (line.includes("CREATE TABLE `" + table + "`")) {
110
- readHeader = true;
111
- }
112
-
113
- if (readHeader) {
114
- if (line.includes("PRIMARY KEY")) {
115
- readHeader = false;
116
- } else {
117
- headerData = headerData.concat(line);
118
- }
119
-
120
- }
121
-
122
- count++;
123
- spinner.text = 'Reading file (' + file +"): "+count
124
- spinner.render()
125
- }
126
- spinner.text = 'Writing file (' + file +")"
127
- spinner.render()
128
-
129
- let headers = []
130
- let matchHeader = new RegExp(/`(.*?)`/g);
131
- var found;
132
- while (found = matchHeader.exec(headerData)) {
133
- if (found[0] != "`" + table + "`") {
134
- headers.push(found[0]);
135
- }
136
-
137
- };
138
-
139
- let records = [];
140
- var reBrackets = /\((.*?)\)/g;
141
- var found;
142
- while (found = reBrackets.exec(data)) {
143
- records.push(found[1]);
144
- };
145
-
146
- let recordsString = records.join("\n").replaceAll(",", ";");
147
- let headersString = headers.join(";")
148
-
149
- if (!fs.existsSync(process.cwd() + path.sep + "csv")) {
150
- fs.mkdirSync(process.cwd() + path.sep + "csv")
151
- }
152
- fs.writeFileSync(process.cwd() + path.sep + "csv" + path.sep + file.split(".")[0] + ".csv", headersString + "\n" + recordsString)
153
- spinner.succeed("CSV created: "+file.split(".")[0] + ".csv")
1
+ import fs from "fs";
2
+ import nReadlines from 'n-readlines';
3
+ import prompts from "prompts";
4
+ import path from "path";
5
+ import ora from "ora";
6
+ import Seven from 'node-7z'
7
+ import sevenBin from '7zip-bin'
8
+
9
+
10
+ export default async function dumpTableToCSV() {
11
+ console.log()
12
+
13
+ let success = true;
14
+
15
+ const results = await prompts([
16
+ {
17
+ // Tabelle die exportiert werden soll
18
+ type: 'text',
19
+ name: 'table',
20
+ message: 'Tabellen-Name?'
21
+ }
22
+ ], {
23
+ onCancel: () => {
24
+ console.log()
25
+ console.log(chalk.red("Cancelled Dump!"))
26
+ console.log()
27
+ success = false
28
+ }
29
+ })
30
+
31
+
32
+ if (success) {
33
+ console.log()
34
+
35
+ // Unpack archives
36
+ let archives = fs.readdirSync(process.cwd(), { withFileTypes: true }).filter(dirent => dirent.isFile() && (dirent.name.split(".")[dirent.name.split(".").length-1] == "gz") || (dirent.name.split(".")[dirent.name.split(".").length-1] == "7z")).map(dirent => { return dirent.name });
37
+ for (const archive of archives){
38
+ await extractDumpsFromArchive(archive)
39
+ }
40
+
41
+ console.log()
42
+
43
+ // Read Files and create csv
44
+ let files = fs.readdirSync(process.cwd(), { withFileTypes: true }).filter(dirent => dirent.isFile() && dirent.name.split(".")[dirent.name.split(".").length-1] == "sql").map(dirent => { return dirent.name });
45
+
46
+ await Promise.all(files.map(async (file) => {
47
+ await createCSVDump(file, results.table)
48
+ }));
49
+
50
+ console.log()
51
+ }
52
+ }
53
+
54
+ function getPromiseFromEvent(item, event) {
55
+ return new Promise((resolve) => {
56
+ const listener = (data) => {
57
+ resolve(data);
58
+ }
59
+ item.on(event, listener);
60
+ })
61
+ }
62
+
63
+ async function extractDumpsFromArchive(archive){
64
+ const spinnerZIP = ora('Unpacking Archive: '+archive).start();
65
+
66
+ const list = Seven.list(process.cwd() + path.sep + archive, {
67
+ $bin: sevenBin.path7za
68
+ })
69
+
70
+ const data = await getPromiseFromEvent(list, "data")
71
+
72
+ let file = data.file
73
+
74
+ if(file.split(".").length == 1){
75
+ const rename = Seven.rename(process.cwd() + path.sep + archive,[[file, file+".sql"]], {
76
+ $bin: sevenBin.path7za
77
+ })
78
+
79
+ await getPromiseFromEvent(rename, "end")
80
+ }
81
+
82
+ const unpack = Seven.extract(process.cwd() + path.sep + archive,"."+path.sep, {
83
+ $bin: sevenBin.path7za
84
+ })
85
+
86
+ await getPromiseFromEvent(unpack, "end")
87
+
88
+ spinnerZIP.succeed("Archive unpacked: "+archive)
89
+
90
+ }
91
+
92
+ async function createCSVDump(file, table) {
93
+ let count = 0;
94
+ const spinner = ora('Reading file (' + file +"): "+count).start();
95
+ const readFile = new nReadlines(process.cwd() + path.sep + file);
96
+
97
+ let line = "";
98
+ let data = "";
99
+ let readHeader = false;
100
+ let headerData = "";
101
+
102
+ while ((line = readFile.next())) {
103
+ line = line.toString('utf-8').trim()
104
+
105
+ if (line.includes("INSERT INTO `" + table + "` VALUES")) {
106
+ data = data + line;
107
+ }
108
+
109
+ if (line.includes("CREATE TABLE `" + table + "`")) {
110
+ readHeader = true;
111
+ }
112
+
113
+ if (readHeader) {
114
+ if (line.includes("PRIMARY KEY")) {
115
+ readHeader = false;
116
+ } else {
117
+ headerData = headerData.concat(line);
118
+ }
119
+
120
+ }
121
+
122
+ count++;
123
+ spinner.text = 'Reading file (' + file +"): "+count
124
+ spinner.render()
125
+ }
126
+ spinner.text = 'Writing file (' + file +")"
127
+ spinner.render()
128
+
129
+ let headers = []
130
+ let matchHeader = new RegExp(/`(.*?)`/g);
131
+ var found;
132
+ while (found = matchHeader.exec(headerData)) {
133
+ if (found[0] != "`" + table + "`") {
134
+ headers.push(found[0]);
135
+ }
136
+
137
+ };
138
+
139
+ let records = [];
140
+ var reBrackets = /\((.*?)\)/g;
141
+ var found;
142
+ while (found = reBrackets.exec(data)) {
143
+ records.push(found[1]);
144
+ };
145
+
146
+ let recordsString = records.join("\n").replaceAll(",", ";");
147
+ let headersString = headers.join(";")
148
+
149
+ if (!fs.existsSync(process.cwd() + path.sep + "csv")) {
150
+ fs.mkdirSync(process.cwd() + path.sep + "csv")
151
+ }
152
+ fs.writeFileSync(process.cwd() + path.sep + "csv" + path.sep + file.split(".")[0] + ".csv", headersString + "\n" + recordsString)
153
+ spinner.succeed("CSV created: "+file.split(".")[0] + ".csv")
154
154
  }