This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
135 lines (115 loc) · 3.19 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
/* eslint no-console:0 */
const WebSocket = require('ws');
const enigma = require('enigma.js');
const schema = require('enigma.js/schemas/3.2.json');
const Table = require('easy-table');
const fs = require('fs');
const path = require('path');
const host = process.env.TEST_HOST || 'localhost';
async function openSessionApp() {
const session = enigma.create({
schema,
url: `ws://${host}:9076/app/engineData`,
createSocket: (url) => new WebSocket(url),
});
const qix = await session.open();
const app = await qix.createSessionApp();
await qix.configureReload(true, true, false);
console.log('Session opened.\n');
return {
session,
qix,
app,
};
}
async function readScript(scriptPath) {
return new Promise((resolve, reject) => {
fs.readFile(scriptPath, 'utf8', async (err, script) => {
if (err) reject(err);
resolve(script);
});
});
}
async function createConnection(app, name, connectionString, type) {
await app.createConnection({
qName: name,
qConnectionString: connectionString,
qType: type,
});
}
async function setScriptAndDoReload(qix, app, script) {
await app.setScript(script);
await app.doReload();
const progress = await qix.getProgress(0);
if (progress.qErrorData.length !== 0) {
const result = await app.checkScriptSyntax();
if (result.length !== 0) {
console.log(result[0]);
}
console.log(progress.qErrorData[0]);
}
return progress.qErrorData.length === 0;
}
async function getTables(app) {
const tablesAndKeys = await app.getTablesAndKeys({
qcx: 1000,
qcy: 1000,
}, {
qcx: 0,
qcy: 0,
},
30,
true,
false);
return tablesAndKeys.qtr;
}
async function printTable(app, table) {
const easyTable = new Table();
console.log('==========');
console.log(table.qName);
console.log('----------');
const data = await app.getTableData(0, 100, true, table.qName);
for (let index = 0; index < data.length; index += 1) {
const row = data[index];
table.qFields.forEach((field, idx) => {
easyTable.cell(field.qName, row.qValue[idx].qText);
});
easyTable.newRow();
}
console.log(easyTable.toString());
}
async function printTables(app) {
const tables = await getTables(app);
const results = [];
tables.forEach((table) => {
results.push(printTable(app, table));
});
await Promise.all(results);
}
async function closeSession(session) {
await session.close();
console.log('Session closed.');
}
async function setupAndReload(scriptPath, printOutput) {
const script = await readScript(scriptPath);
const {
session,
qix,
app,
} = await openSessionApp(scriptPath);
await createConnection(app, 'data', '/data/', 'folder');
await createConnection(app, 'webdata', 'https://raw.githubusercontent.com/qlik-oss/core-data-loading/master/data/airports.csv', 'internet');
const reloadOK = await setScriptAndDoReload(qix, app, script);
if (printOutput) {
await printTables(app);
}
await closeSession(session);
return reloadOK;
}
if (process.argv[2]) {
(async () => {
const scriptPath = `${path.dirname(__filename)}/scripts/${process.argv[2]}`;
await setupAndReload(scriptPath, true);
})();
}
module.exports = setupAndReload;