forked from blynkkk/blynk-sketch-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (112 loc) · 3.24 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
'use strict';
var _ = require("underscore");
function add_names(d) {
_.keys(d).forEach((k) => {
if (!_.has(d[k], "name")) {
d[k].name = k;
}
});
}
function run_inherit(d) {
var result = {};
_.keys(d).forEach((k) => {
var i = d[k];
var chain = [i];
while (_.has(i, "inherit")) {
var tgt = i.inherit;
if (typeof tgt === 'string') {
if (_.has(d,tgt))
tgt = d[tgt];
else
console.error(tgt, "not found");
}
chain.unshift(tgt);
i = tgt;
}
//console.log(chain.map((i) => i.name));
result[k] = Object.assign({}, ...chain);
delete result[k].inherit;
});
return result;
}
function trim_obj(obj) {
if (!Array.isArray(obj) && typeof obj != 'object') return obj;
return Object.keys(obj).reduce(function(acc, key) {
acc[key] = typeof obj[key] == 'string'? obj[key].trim() : trim_obj(obj[key]);
return acc;
}, Array.isArray(obj)? []:{});
}
var boards = require("./data_boards.js");
var shields = require("./data_shields.js");
var examples = require("./data_examples.js");
add_names(examples);
add_names(boards);
add_names(shields);
boards = run_inherit(boards);
shields = run_inherit(shields);
function generate(board, shield, example, auth_token) {
if (boards[board] === undefined) return "No such board";
if (shields[shield] === undefined) return "No such shield";
if (examples[example] === undefined) return "No such example";
var data = {
auth: auth_token || "YourAuthToken",
defines: "\b",
board : {
comment: "\b",
defs: "\b",
inc: "\b",
glob: "\b",
init: "\b",
loop: "\b"
},
example: {
comment: "\b",
defs: "\b",
inc: "\b",
glob: "\b",
init: "\b",
loop: "\b"
}
};
_.extend(data, boards[board]);
_.extend(data.board, shields[shield]);
_.extend(data.example, examples[example]);
data = trim_obj(data);
//return JSON.stringify(data);
var t = _.template(data.template);
var code = t(data);
code = code.replace(/\r\n/g, "\n");
code = code.replace(/\n[\s]*[\b]\n/g, "\n");
code = code.replace(/[\b]/g,"");
return code;
}
function getBoardShields(board) {
board = boards[board];
var result = [];
if (_.has(board, "builtin")) {
result.push("--- Built In");
Array.prototype.push.apply(result, board.builtin);
}
var all_shields = _.keys(shields);
if (_.has(board, "exclude")) {
var regexs = board.exclude;
all_shields = all_shields.filter(function (text) {
if (text.startsWith("---")) return true;
return !regexs.some(function (regex) {
return regex.test(text);
});
});
}
all_shields = all_shields.filter((k) => (shields[k].embedded !== true));
Array.prototype.push.apply(result, all_shields);
return result;
}
/*************************************************/
/*module.exports.boards = boards
module.exports.shields = shields
module.exports.examples = examples*/
module.exports.listBoards = function() { return Object.keys(boards) };
module.exports.listShields = function() { return Object.keys(shields) };
module.exports.listExamples = function() { return Object.keys(examples) };
module.exports.getBoardShields = getBoardShields;
module.exports.generate = generate;