/
projectFiles.js
59 lines (52 loc) · 1.39 KB
/
projectFiles.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
// Load project files in project data format
const shell = require('shelljs');
const path = require('path');
const paths = require('../core/paths');
// const vio = require('../core/vio');
function sortElements(elements, elementById) {
elements.sort((a, b) => {
a = elementById[a];
b = elementById[b];
if (!a || !b) console.log(elements, elementById);
if (a.children && !b.children) return -1;
else if (!a.children && b.children) return 1;
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
});
return elements;
}
function readDir(dir) {
const prjRoot = paths.getProjectRoot();
dir = dir || paths.map('src');
const elementById = {};
const elements = [];
shell.ls(dir).forEach(file => {
file = paths.join(dir, file);
const rFile = file.replace(prjRoot, '');
elements.push(rFile);
if (shell.test('-d', file)) {
const res = readDir(file);
Object.assign(elementById, res.elementById);
elementById[rFile] = {
name: path.basename(file),
type: 'folder',
id: rFile,
children: res.elements,
};
} else {
elementById[rFile] = {
name: path.basename(file),
type: 'file',
ext: path.extname(file).replace('.', ''),
id: rFile,
};
}
});
sortElements(elements, elementById);
return {
elementById,
elements,
};
}
module.exports = {
readDir,
};