-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-config-generator.js
117 lines (83 loc) · 2.76 KB
/
node-config-generator.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
var fs = require('fs');
var path = require('path');
var builder = require('xmlbuilder');
var imgExts = ['.png', '.jpg', '.gif'];
var sndExts = ['.mp3', '.ogg', '.wav'];
var movExts = ['.mov', '.mp4', '.m4v'];
// Find all directories that look like they'll need a config file.
function findConfigWorthyDirectories(dir) {
var directoryStats = fs.lstatSync(dir);
if (directoryStats.isDirectory()) {
// Directory ...
var files = fs.readdirSync(dir);
for (var i = 0; i < files.length; i++) {
var fileDir = dir + '/' + files[i];
// If we find an image file, assume it is a configurable directory.
if (imgExts.indexOf(path.extname(fileDir)) > -1) {
makeConfigForDirectory(dir);
break;
}
findConfigWorthyDirectories(fileDir);
}
}
}
function makeConfigForDirectory(dir) {
console.log('Targeting:', dir);
// Create base xml structure
var xml = builder.create('config')
.ele('component', {'assets': dir})
.ele('slides');
// Make slide nodes for each asset.
var firstSlide;
var files = fs.readdirSync(dir);
for (var i = 0; i < files.length; i++) {
var fileDir = dir + '/' + files[i];
var classStr = '';
var ext = path.extname(fileDir);
// If we find an image file, assume it is a configurable directory.
if (imgExts.indexOf(ext) > -1) {
classStr += 'image';
} else if (sndExts.indexOf(ext) > -1) {
classStr = 'image sound';
} else if (movExts.indexOf(ext) > -1) {
classStr = 'movie';
} else {
// Didn't recognize extension. Skip.
}
if (classStr != '' && fileDir.indexOf('FR_') < 0) {
var enFile = fileDir;
var frFile = enFile.replace('EN', 'FR');
//Sound exception. Attribute to first slide.
if (classStr.indexOf('sound') > -1) {
firstSlide.att('class', classStr);
firstSlide.att('enSnd', enFile);
firstSlide.att('frSnd', frFile);
} else {
// Create new slide.
var item = xml.ele('slide');
item.att('id', 'slide_' + i);
item.att('class', classStr);
item.att('enSrc', enFile);
item.att('frSrc', frFile);
// Remember first node
if (i === 1) {
// console.log('FIRST', i, enFile);
firstSlide = item;
}
}
}
}
// Pretty Stringify xml.
var xmlStr = xml.end({ pretty: true});
// Write to XML file.
var saveDir = 'configs/';
var parentFolder = path.basename(dir);
var configTitle = parentFolder + '.xml';
fs.writeFile(saveDir + configTitle, xmlStr, function(err) {
if (err) throw err;
console.log('Saved--> ' + saveDir + configTitle + ' ');
});
}
// Kick things off using parent directory...
var targetDir = process.argv[2] || '.';
findConfigWorthyDirectories(targetDir);