forked from AnatolyUss/nmig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmig.js
More file actions
89 lines (81 loc) · 2.94 KB
/
nmig.js
File metadata and controls
89 lines (81 loc) · 2.94 KB
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
/*
* This file is a part of "NMIG" - the database migration tool.
*
* Copyright (C) 2016 - present, Anatoly Khaytovich <anatolyuss@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (please see the "LICENSE.md" file).
* If not, see <http://www.gnu.org/licenses/gpl.txt>.
*
* @author Anatoly Khaytovich <anatolyuss@gmail.com>
*/
'use strict';
const fs = require('fs');
const path = require('path');
const main = require('./migration/fmtp/Main');
/**
* Read the configuration file.
*
* @returns {Promise}
*/
const readConfig = () => {
return new Promise((resolve, reject) => {
const strPathToConfig = path.join(__dirname, 'config.json');
fs.readFile(strPathToConfig, (error, data) => {
if (error) {
reject('\n\t--Cannot run migration\nCannot read configuration info from ' + strPathToConfig);
} else {
try {
const config = JSON.parse(data.toString());
config.tempDirPath = path.join(__dirname, 'temporary_directory');
config.logsDirPath = path.join(__dirname, 'logs_directory');
config.dataTypesMapAddr = path.join(__dirname, 'DataTypesMap.json');
resolve(config);
} catch (err) {
reject('\n\t--Cannot parse JSON from ' + strPathToConfig);
}
}
});
});
}
/**
* Read the extra configuration file, if necessary.
*
* @param {Object} config
*
* @returns {Promise}
*/
const readExtraConfig = config => {
return new Promise((resolve, reject) => {
if (config.enable_extra_config !== true) {
config.extraConfig = null;
return resolve(config);
}
const strPathToExtraConfig = path.join(__dirname, 'extra_config.json');
fs.readFile(strPathToExtraConfig, (error, data) => {
if (error) {
reject('\n\t--Cannot run migration\nCannot read configuration info from ' + strPathToExtraConfig);
} else {
try {
config.extraConfig = JSON.parse(data.toString());
resolve(config);
} catch (err) {
reject('\n\t--Cannot parse JSON from ' + strPathToExtraConfig);
}
}
});
});
}
readConfig()
.then(readExtraConfig)
.then(config => main(config))
.catch(error => console.log(error));