This repository has been archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (135 loc) · 4.18 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
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
const of = require('await-of').default;
const fs = require('fs');
const faker = require('faker');
const NEW_LINE = '\n';
const DEFAULT_FILE_ENCODING = 'utf8';
const NULL_VALUE = 'NULL';
const INSERT_START_LINE = 'INSERT INTO';
const INSERT_REGEX = /INSERT INTO (.*?) \((.*?)\) VALUES \((.*?)\);/i;
const VALUE_TYPES = {
empty: 'empty',
null: 'NULL',
value: 'value',
};
function getMatches(line = '', regex) {
return regex.exec(line) || [];
}
function processValue(value = '', config = {}) {
const { type, value: configValue, excludeValues = [], isString = false } = config;
let newValue;
if (excludeValues.includes(removeQuotesAndTrim(value))) {
return value;
}
if (type.includes('faker')) {
const execPath = type.split('.');
if (execPath.length !== 3) {
return '';
}
const subType = faker[execPath[1]] || {};
const func = subType[execPath[2]];
if (!func) {
return '';
}
try {
newValue = func();
return isString ? `'${newValue}'` : newValue;
} catch (error) {
console.error(error);
return '';
}
}
const valueMap = {
[VALUE_TYPES.empty]: '',
[VALUE_TYPES.null]: NULL_VALUE,
[VALUE_TYPES.value]: configValue,
};
newValue = valueMap[type] || '';
newValue = isString ? `'${newValue}'` : newValue;
return newValue || value;
}
function removeQuotesAndTrim(value = '') {
return value.startsWith("'") ? value.substring(1).slice(0, -1) : value;
}
function processContent(content = '', config = {}) {
const lines = content.split(NEW_LINE);
let modifiedResult = [];
for (let line of lines) {
if (!line.startsWith(INSERT_START_LINE)) {
modifiedResult.push(line);
continue;
}
const matches = getMatches(line, INSERT_REGEX);
if (matches.length !== 4) {
modifiedResult.push(line);
continue;
}
const [, tableName, columnsStr, valuesStr] = matches;
const columns = columnsStr.split(',').map(value => value.trim());
const values = valuesStr.split(',').map(value => value.trim());
const tableSettings = config[tableName];
if (!tableSettings) {
modifiedResult.push(line);
continue;
}
let newValues = [];
for (let i = 0, len = columns.length; i < len; i++) {
const columnSetting = tableSettings[columns[i]];
if (!columnSetting) {
newValues.push(values[i]);
continue;
}
newValues.push(processValue(values[i], columnSetting));
}
const newLine = `INSERT INTO ${tableName} (${columnsStr}) VALUES (${newValues.join(', ')})`;
modifiedResult.push(newLine);
}
return modifiedResult.join(NEW_LINE);
}
async function parseConfig(configFilePath) {
const [content, error] = await of(readFile(configFilePath));
if (error) {
throw error;
}
return JSON.parse(content);
}
function readFile(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, DEFAULT_FILE_ENCODING, (error, content) => {
if (error) {
return reject(error);
}
resolve(content);
});
});
}
function writeFile(filePath, content) {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, content, error => {
if (error) {
return reject(error);
}
resolve();
});
});
}
async function processFile(sqlDumpFile, options = {}) {
let error;
let content;
let config;
[config, error] = await of(parseConfig(options.config));
if (error) {
throw error;
}
[content, error] = await of(readFile(sqlDumpFile));
if (error) {
throw error;
}
const newContent = processContent(content, config);
const outputFileName = options.output || sqlDumpFile;
[, error] = await of(writeFile(outputFileName, newContent));
if (error) {
throw error;
}
return outputFileName;
}
module.exports = processFile;