forked from espruino/Espruino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefactor_json_comments.js
More file actions
executable file
·63 lines (57 loc) · 1.75 KB
/
refactor_json_comments.js
File metadata and controls
executable file
·63 lines (57 loc) · 1.75 KB
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/node
/* This was designed to be run once over Espruino
* ... hence it has probably already been run.
*
* It scans over all old-style JSON elements, reads them in,
* formats them, and then spits them out with the description after.
*
* This will hopefully encourage more verbose descriptions!
*/
var fs = require("fs");
var common = require("./common.js");
function formatJSON(json) {
var s = "{\n";
var first = true;
for (var key in json) {
if (!first) s += ",\n";
var val = json[key];
s += " \""+key+"\" : ";
if (key=="return") {
s += JSON.stringify(val);
} else if (key=="params") {
s += "[\n ";
val.forEach(function(param) {
if (param != val[0]) s+=",\n ";
s += JSON.stringify(param);
});
s += "\n ]";
} else {
s += JSON.stringify(val,null," ");
}
first = false;
}
s += "\n}";
return s;
}
common.getWrapperFiles(function(files) {
files.forEach(function(filename) {
console.log("Refactoring "+filename);
var contents = fs.readFileSync(filename).toString();
var jsonBlocks = contents.match( /\/\*JSON(?:(?!\*\/).|[\n\r])*\*\//g );
if (jsonBlocks) jsonBlocks.forEach(function(jsonBlock) {
// parse it
var json = JSON.parse(jsonBlock.trim().slice(6,-2));
var description = json["description"];
delete json["description"];
if (Array.isArray(description))
description = description.join("\n\n");
// now reconstruct
var newBlock = "/*JSON" + formatJSON(json);
if (description!==undefined)
newBlock += "\n" + description + "\n";
newBlock += "*/";
contents = contents.replace(jsonBlock, newBlock);
});
fs.writeFileSync(filename, contents);
});
});