-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadme
166 lines (124 loc) · 3.51 KB
/
readme
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Treeify
=======
A node module that simply does one thing => to make the tree structure for the modules present
in a dir.
Example:
Consider a dir temp,
that contains files like => module1.js, module2.js, temp1.js, temp2.js
temp/
|
---- module1.js
---- module2.js
---- temp1.js
---- temp2.js
var treeObj = new treeify.Treeify('path to /temp dir'); // dir name can be absolute or relative path
now if module1.js file =>
module.exports = {
data1: 'something',
data2: 'something',
}
treeify on the temp dir will give the tempObj object :
{
module1,
module2,
temp1,
temp2,
}
so, you can now simply acccess the content from the treeObj like:
treeObj.module1 or treeObj.module2 or treeObj.temp1 etc.
or
if you have a dir inside dir like :
temp/
|
----demo/
|
----demo1.js
----demo2.js
----anotherDir/
|
----anotherMod.js
|
----static/
|
----static1.js
----static2.js
----mod: function () {}
where :
demo1.js =>
module.exports = {
cont1: 'something1',
cont2: 'something2',
}
demo2.js =>
module.exports = {
cont3: 'something1',
cont4: 'something2',
}
anotherMod.js =>
module.exports = {
another: ''
}
static =>
module.exports = {
static1: '',
static2: '',
mod: function() {}
}
treeify on the "temp" dir will give :
{
demo: {
cont1: '',
cont2: '',
cont3: '',
cont4: '',
anotherDir: {
another: '',
}
},
static: {
static1: '',
static2: '',
mod:function() {}
}
}
so, you can now access using :
treeObj.demo.cont1
treeObj.demo.cont2
treeObj.demo.cont3
treeObj.demo.cont4
treeObj.demo.anotherDir.another
treeObj.static.static1
treeObj.static.static2
treeObj.static.mod
Similarly , you can use it for the nested dirs or modules.
In a nutshell, for a particular dir, it will get all the exports (in the module) inside it and group together as per dir.
How to Run and Build
=====================
Clone the repo and run npm install
Now run "npm run babel" to compile code to es5
This will create a index.js file inside 'build' dir
require this 'index.js' file like :
eg : var treeify = require('../build/index.js'); // set the path as per yours
Now simply specify the path of the dir you want to treeify :
let obj = new treeify.Treeify('path to your dir comes here');
Now, we have two options to treeify the dir : promise and without promise
like :
let tree = obj.treeify(); // will return the created tree
or
let tree = obj.treeifyPromise()
.then((d) => {
// here you get the generated tree
})
.catch((err) => {
});
Interesting thing i learnt:
The most obvious thing we do in Node.js is 'require' the 'module', Right ?????
and this is the most common thing we neglect, i,e the 'module' object provided by Node.JS which is fascinating :)
and has a lot of things if we dig into it.
One such thing is module.parent, using which you can find the parent for the required module, i.e parent of the module where it is required.
Do dig into the 'module' object in Node.JS, it's cool !
Note:
=====
It still needs a lot of work :P
I will be improving it like adding features for adding filters for the options we need in the generated object and etc.
Feel free to contribute :)