-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (100 loc) · 2.52 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
const
dirTree = require('directory-tree'),
yaml = require('js-yaml'),
fs = require('fs-extra'),
{ join, normalize, basename } = require('path');
const yamlConfig = {
schema: yaml.JSON_SCHEMA
}
const VNJSON = {};
function getFileName(scene){
return basename(scene.name, scene.extension)
}
function scenesToJSON(src, dist, callback, assetsURL='scenes'){
/**
* remove prev build
*/
fs.emptyDirSync(dist);
/**
* walk dir /src
*/
let errSceneName;
let errLabelName;
try {
const _scenes = dirTree(src).children.map(scene=>{
errSceneName = scene.name;
if(scene.type==='directory'){
//create scene body
const sceneBody = new Object();
sceneBody.assets = new Array();
//walk scene dir
scene.children.map(label=>{
/**
* { label }.yaml
*/
errLabelName = label.name;
if(/ya?ml|yaml/i.test(label.extension)){
let yamlBody = fs.readFileSync( normalize(label.path), 'utf8');
sceneBody[getFileName(label)] = yaml.load(yamlBody, yamlConfig);
}
/**
* Обработка директорий внутри сцены
*/
if(label.type==='directory'){
/**
* walk dir /assets
*/
label.children.forEach(asset=>{
/**
* ASSETS
*/
if(label.name==="assets"){
try{
fs.copySync(asset.path, join(dist, "/assets/", asset.name));
const assetBody = {
name: basename(asset.name, asset.extension),
url: `${assetsURL}/assets/${asset.name}`
};
sceneBody.assets.push(assetBody);
}
catch (err){
console.error('Asset file error', asset.name);
console.error(err);
}
}
/**
* DATA
*/
if( label.name==="data"&&label.type==="directory" ){
try{
sceneBody.data = sceneBody.data||{};
const fileBody = fs.readFileSync(asset.path, "utf-8");
const b64Data = Buffer.from( encodeURI(fileBody) ).toString('base64');
sceneBody.data[asset.name] = b64Data;
}
catch(err){
console.error('Data file error', asset.name);
console.error(err);
}
}
})
}
});//sceneItem
/**
* add current scene to TREE
*/
VNJSON[scene.name] = sceneBody;
}
});
/**
* write VNJSON to vn.json
*/
const pathToFile = join(dist, 'vn.json');
fs.writeFileSync(pathToFile, JSON.stringify(VNJSON, null, 2));
callback();
}
catch (err){
callback(err, errSceneName, errLabelName);
}
}
module.exports = scenesToJSON;