-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·178 lines (164 loc) · 4.88 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
176
177
178
const fs = require('fs');
const FormData = require('form-data');
const axios = require('axios');
const Readable = require('stream').Readable;
const command = process.argv[2];
const spaceId = process.argv[3];
const watch = process.argv[4];
const baseUrl = process.env.URL_KIBANA || 'http://elastic:changeme@localhost:5601';
const soTypes = ['index-pattern', 'dashboard', 'lens', 'map', 'search', 'query', 'visualization'];
function debounce(func, timeout = 300) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, timeout);
};
}
function unpackJSONPropsInPlace(object) {
Object.keys(object).forEach((key) => {
if (typeof object[key] === 'string' && (key.endsWith('JSON') || key == 'visState')) {
object[key] = JSON.parse(object[key]);
} else if (typeof object[key] === 'object' && object[key] !== null) {
unpackJSONPropsInPlace(object[key]);
}
});
}
// like unpackJSONPropsInPlace, but the other way around - if the key ends with JSON, stringify the property
function packJSONPropsInPlace(object) {
Object.keys(object).forEach((key) => {
if (typeof object[key] !== 'object' || object[key] === null) {
return;
}
if (key.endsWith('JSON') || key == 'visState') {
object[key] = JSON.stringify(object[key]);
return;
}
packJSONPropsInPlace(object[key]);
});
}
function objectFilename(object) {
return `${object.type}-${object.id}.json`;
}
async function push() {
const objects = [];
fs.readdirSync('.', { encoding: 'utf8' }).forEach((file) => {
if (soTypes.some((type) => file.startsWith(type)) && file.endsWith('.json')) {
const object = JSON.parse(fs.readFileSync(file, { encoding: 'utf8' }));
packJSONPropsInPlace(object);
objects.push(JSON.stringify(object));
}
});
const formData = new FormData();
var file = new Readable();
file.push(objects.join('\n'));
file.push(null);
formData.append('file', file, 'file.ndjson');
await axios.post(`${baseUrl}/s/${spaceId}/api/saved_objects/_import?overwrite=true`, formData, {
headers: {
...formData.getHeaders(),
'kbn-xsrf': 'abc',
},
});
console.log('Pushed objects to Kibana');
}
async function pull() {
const response = await axios.post(
`${baseUrl}/s/${spaceId}/api/saved_objects/_export`,
{
type: soTypes,
excludeExportDetails: true,
includeReferencesDeep: true,
},
{
headers: {
'kbn-xsrf': 'abc',
},
}
);
const objects = response.data.split('\n').map(JSON.parse);
objects.forEach(unpackJSONPropsInPlace);
objects.forEach((object) => {
fs.writeFileSync(objectFilename(object), JSON.stringify(object, null, 2));
});
}
function pack() {
const objects = [];
fs.readdirSync('.', { encoding: 'utf8' }).forEach((file) => {
if (soTypes.some((type) => file.startsWith(type)) && file.endsWith('.json')) {
const object = JSON.parse(fs.readFileSync(file, { encoding: 'utf8' }));
packJSONPropsInPlace(object);
objects.push(JSON.stringify(object));
}
});
return objects.join('\n');
}
function unpack() {
const objects = fs
.readFileSync(spaceId || './export.ndjson', { encoding: 'utf8' })
.split('\n')
.map(JSON.parse)
.filter((o) => !o.exportedCount);
objects.forEach(unpackJSONPropsInPlace);
objects.forEach((object) => {
fs.writeFileSync(objectFilename(object), JSON.stringify(object, null, 2));
});
}
function block() {
let unblock;
const promise = new Promise((res) => {
unblock = res;
});
return {
wait: () => promise,
unblock
};
}
function unblocked() {
const promise = Promise.resolve(true);
return () => promise;
}
module.exports = async function () {
try {
if (command === 'push') {
if (watch === '--watch') {
push();
fs.watch('.', { encoding: 'utf8' }, debounce(push));
} else {
push();
}
} else if (command === 'pull') {
pull();
} else if (command === 'sync') {
let blocked = unblocked();
await pull();
fs.watch('.', { encoding: 'utf8' }, debounce(async () => {
await blocked();
const { unblock, wait } = block();
blocked = wait;
await push();
unblock();
}));
const pollInterval = Number(watch) || 5000;
async function waitAndPull() {
await blocked();
const { unblock, wait } = block();
blocked = wait;
await pull();
unblock();
setTimeout(waitAndPull, pollInterval);
}
setTimeout(waitAndPull, pollInterval);
} else if (command === 'pack') {
fs.writeFileSync(spaceId || './export.ndjson', pack());
} else if (command === 'data-url') {
console.log(`data:text;base64,${Buffer.from(pack(), 'utf8').toString('base64')}`);
pack();
} else if (command === 'unpack') {
unpack();
}
} catch (e) {
console.log(e);
}
};