-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect-assets.js
40 lines (32 loc) · 1 KB
/
collect-assets.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
/*
Collect static files tree skeleton as js object
Usage: nodejs collect-assets.js path/to/project
*/
var dirs = ['images'];
var outFile = 'assets.json';
var path = require('path');
var fs = require('fs');
var util = require('util');
var workPath = (process.argv.length > 2 && fs.existsSync(process.argv[2])) ? process.argv[2] : __dirname;
var col = {};
function collectDir(dirNode, dirPath, dir) {
dirNode['@'] = [];
var dirUriPath = path.join(dirPath, dir);
var files = fs.readdirSync(dirUriPath);
files.forEach(function (file) {
var dirFilePath = path.join(dirUriPath, file);
var stats = fs.statSync(dirFilePath);
if (stats.isDirectory()) {
dirNode[file] = {};
collectDir(dirNode[file], dirUriPath, file);
} else {
//TODO: match the extensions patterns (.png, .jpg) and skip @2x, 3x, .ios, etc...
dirNode['@'].push(file);
}
});
}
dirs.forEach(function(k) {
col[k] = {};
collectDir(col[k], workPath, k);
});
fs.writeFileSync(path.join(workPath, outFile), JSON.stringify(col, null, 2));