MCPcopy Index your code
hub / github.com/reactide/reactide / getTree

Function getTree

lib/file-tree.js:101–229  ·  view source on GitHub ↗
(rootDirPath, callback)

Source from the content-addressed store, hash-verified

99//getTree is a function expression that takes in 2x arguments
100//A 'rootDirPath' and a callback
101const getTree = (rootDirPath, callback) => {
102 //init resets the values in the projInfo object back to their default values
103 init();
104 //rootPath property inside of projInfo object is reassigned to the inputted rootDirPath
105 projInfo.rootPath = rootDirPath;
106 //fileTree is assigned the object returned from invoking Director constructor
107 //Object returned has the following properties:
108 //path, type, name, opened, files, subdirectories, & id
109 //The path.basename() methods returns the last portion of a path, in this instance, the folder name
110 let fileTree = new Directory(rootDirPath, path.basename(rootDirPath), true);
111
112 //I'm not sure what the purpose of pending is
113 let pending = 1;
114
115 //recurseThroughFileTree is called and passed the object that exists at fileTree
116 function recurseThroughFileTree(directory) {
117 //Loop through files and fill files property array
118 //fs.readdir reads the contents of a directory
119 //entire directory exists at directory.path
120 fs.readdir(directory.path, (err, files) => {
121
122 //I'm not sure what the purpose of pending is
123 pending += files.length;
124
125 //this conditional is assessing pending after it's decremented to see if it's a false/falsy value
126 //should only be true after being decremented several times
127 if (!--pending) {
128 //if it is, we call writeFileSync and pass the file stored at projInfoPath
129 //we write the stringified value of projInfo to whatever file exists at projInfoPath
130 fs.writeFileSync(projInfoPath, JSON.stringify(projInfo));
131 //inputted callback to getTree is then fired and passed the fileTree object
132 callback(fileTree);
133 }
134 //files is an array of files that exist in the directory passed to the fs.readdir method
135 //forEach is used to iterate over that array
136 files.forEach((file) => {
137 //filePath uses path.join() to join the path and file names together
138 const filePath = path.join(directory.path, file);
139 //fs.stat is a method that checks for file details associated with the inputted file
140 //if status is good, it's details are available at the 'stats' variable
141 fs.stat(filePath, (err, stats) => {
142 if (err) {
143 //this conditional is assessing pending after it's decremented to see if it's a false/falsy value
144 //should only be true after being decremented several times
145 if (!--pending) {
146 //if it is, we call writeFileSync and pass the file stored at projInfoPath
147 //we write the stringified value of projInfo to whatever file exists at projInfoPath
148 fs.writeFileSync(projInfoPath, JSON.stringify(projInfo));
149 //inputted callback to getTree is then fired and passed the fileTree object
150 callback(fileTree);
151 }
152 }
153 //checks if stats is true or truthy & stats.isFile() resolves to true
154 if (stats && stats.isFile()) {
155 //insertSorted pushes an object into the directory.files array
156 //object pushed into the array has the following parameters:
157 //path, type, name, ext, id
158 insertSorted(new File(filePath, file, getFileExt), directory.files);

Callers 1

setFileTreeMethod · 0.50

Calls 2

initFunction · 0.70
recurseThroughFileTreeFunction · 0.70

Tested by

no test coverage detected