-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (46 loc) · 1.46 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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
process.stdin.setEncoding('utf8');
var dashboardJson = '';
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null)
dashboardJson += chunk;
});
process.stdin.on('end', function() {
//deserialize dashboard json
var dashboard = JSON.parse(dashboardJson);
//load package.json
var packageJson = fs.readFileSync('./package.json', { encoding: 'utf8' });
var widgetId = JSON.parse(packageJson).name;
//find widget based on id
var widget = findWidget(dashboard, widgetId);
if (widget) {
//read model.js
var model = fs.readFileSync('./model.js', { encoding: 'utf8' });
//read view.html
var view = fs.readFileSync('./view.html', { encoding: 'utf8' });
//read helpers.js
var helpers = fs.readFileSync('./helpers.js', { encoding: 'utf8' });
//update dashboard
widget.settings.model = model;
widget.settings.view = view;
widget.settings.helpers = helpers;
//serialize and write to stdout
var newDashboardJson = JSON.stringify(dashboard, null, 2);
console.log(newDashboardJson);
}
else
throw 'Could not find widget in dashboard: ' + widgetId;
});
function findWidget(dashboard, widgetId) {
for (var i in dashboard.panes) {
var pane = dashboard.panes[i];
for (var j in pane.widgets) {
var widget = pane.widgets[j];
if (widget.title && widget.title === widgetId)
return widget;
}
}
}