-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-data.js
126 lines (105 loc) · 2.82 KB
/
build-data.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
/**
Structure:
type OneObject =
{
id: string,
text: string,
x?: number,
y?: number,
angle?: number,
distance?: number,
isGroup?: boolean,
scale?: number,
children?: OneObject[],
}
id - path to the element
text - human-readable name
x - X relative to previous element
y - Y relative to previous element
isGroup - is element clickable with some info in the README.md (default false)
scale - scale of the element (default 20)
angle - 0-360 - deviation angle from the previous element
distance - distance from the previous element
*/
const fs = require('fs');
const path = require('path');
const util = require('util');
const input = `${__dirname}/data/`;
const readDir = util.promisify(fs.readdir);
const readFile = util.promisify(fs.readFile);
const isDir = path => fs.statSync(path).isDirectory();
const getJson = async (path) => {
if(!fs.existsSync(path)) {
throw `JSON config file doesn't exist on the path: ${path}`;
}
const file = await readFile(path, 'utf8');
if(!file.length) {
throw `JSON config file is empty on the path: ${path}`;
}
try {
return JSON.parse(file);
} catch(e) {
throw `Invalid JSON config file on the path: ${path}`;
}
};
const getCoords = ({x, y, angle, distance = 2, scale = 1}, path) => {
const defaultRadius = 20;
if(x !== undefined && y !== undefined) {
return {x, y};
}
if(angle !== undefined) {
const radius = defaultRadius * (scale + 1) * distance;
angle = angle * Math.PI / -180;
x = Math.cos(angle) * radius;
y = Math.sin(angle) * radius;
return {
x: Math.round(x),
y: Math.round(y),
};
}
throw `Couldn't find {x, y}, not enough information on path: ${path}`;
};
async function walkTree(currentPath, id = '/') {
let result = [];
const insides = await readDir(currentPath);
for(const name of insides) {
const absolutePath = currentPath+name+'/';
if(!isDir(absolutePath)) {
continue;
}
// Fill the object
const json = await getJson(`${absolutePath}${name}.json`);
const o = {
id: id+name,
text: json.text,
};
const {x, y} = getCoords(json, absolutePath);
o.x = x;
o.y = y;
/**
* Optional parameters
*/
// Group
if(!fs.existsSync(`${absolutePath}README.md`)) {
o.isGroup = true;
}
// Scale
json.scale && (o.scale = json.scale);
// Children
const children = await walkTree(absolutePath, id+name+'/');
children.length && (o.children = children);
// Push
result.push(o);
}
return result;
}
(async () => {
try {
const result = await walkTree(input);
const output = path.join(input, '/index.js');
const data = `const data = ${JSON.stringify(result, null, '\t')};\n\nexport default data;\n`;
fs.writeFile(output, data, (e) => e ? console.error(e) : console.log('data/index.js saved'));
} catch(e) {
console.error(e);
}
})();