forked from macacajs/macaca-datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
122 lines (96 loc) · 3.16 KB
/
app.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
'use strict';
const fs = require('fs');
const _ = require('xutil');
const path = require('path');
const socket = require('./app/socket');
const chalk = _.chalk;
const detectPort = _.detectPort;
module.exports = app => {
app.logger.info(`${chalk.cyan('launch datahub at:')} ${app.config.sequelize.storage}`);
app.beforeStart(async () => {
await app.model.sync();
const ctx = app.createAnonymousContext();
try {
await ctx.model.Data.findAll({
raw: true,
});
} catch (e) {
if (e.parent && e.parent.code === 'SQLITE_ERROR') {
console.log(_.chalk.red('\n****************** IMPORTANT!!! ******************'));
console.log(_.chalk.red(`\n please remove: ${path.resolve(app.config.sequelize.storage, '..')}`));
console.log(_.chalk.red(`\n $ rm -r ${path.resolve(app.config.sequelize.storage, '..')}`));
console.log(_.chalk.red('\n****************** IMPORTANT!!! ******************\n'));
process.exit(0);
}
}
if (app.config.dataHubStoreDir) {
app.logger.info(`${chalk.cyan('launch datahub store at:')} ${app.config.dataHubStoreDir}`);
const hubFile = path.resolve(app.config.dataHubStoreDir, 'hub.data');
if (_.isExistedFile(hubFile)) {
app.logger.info(`${chalk.cyan('import datahub from:')} ${hubFile}`);
const content = fs.readFileSync(hubFile, 'utf8');
try {
const list = JSON.parse(content);
app.whiteList = list.map(item => {
return {
identifer: item.identifer,
};
});
for (let i = 0; i < list.length; i++) {
const data = list[i];
const {
identifer,
} = data;
await ctx.model.Project.upsert({
...data,
}, {
where: {
identifer,
},
});
}
} catch (e) {
app.logger.warn(e.message);
}
}
const archiveFile = path.resolve(app.config.dataHubStoreDir, 'archive.data');
if (_.isExistedFile(archiveFile)) {
app.logger.info(`${chalk.cyan('import datahub from:')} ${archiveFile}`);
const content = fs.readFileSync(archiveFile, 'utf8');
try {
const list = JSON.parse(content);
const tables = Object.keys(_.groupBy(list, 'identifer'));
for (let i = 0; i < tables.length; i++) {
await ctx.model.Data.destroy({
where: {
identifer: tables[i],
},
});
}
for (let i = 0; i < list.length; i++) {
const data = list[i];
const {
identifer,
pathname,
} = data;
await ctx.model.Data.upsert({
...data,
}, {
where: {
identifer,
pathname,
},
});
}
} catch (e) {
app.logger.warn(e.message);
}
}
}
const socketPort = await detectPort(app.config.dataHubSocket.port);
app.config.socket = {
port: socketPort,
};
socket.listen(socketPort);
});
};