Skip to content

Commit f15f758

Browse files
committed
♻️ Added function to create file object
1 parent 7669cd6 commit f15f758

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

lib/modclean.js

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -181,37 +181,9 @@ class ModClean extends ModClean_Utils {
181181
if(err) throw this.error(err, '_find');
182182

183183
let [e, files] = await on(
184-
Promise.all(results.map(async file => {
185-
let obj = {
186-
path: file,
187-
fullPath: path.join(this.options.cwd, file),
188-
dir: path.join(this.options.cwd, path.parse(file).dir),
189-
name: path.basename(file),
190-
isModule: false,
191-
isDirectory: null
192-
};
193-
194-
try {
195-
let stats = await stat(obj.fullPath),
196-
isDir = stats.isDirectory();
197-
198-
obj.isDirectory = isDir;
199-
200-
if(isDir) {
201-
let list = await readdir(obj.fullPath),
202-
parent = path.basename(path.join(obj.fullPath, '../'));
203-
204-
if(list.indexOf('package.json') !== -1 && parent === this.options.modulesDir) obj.isModule = true;
205-
}
206-
207-
obj.stat = stats;
208-
} catch(error) {
209-
this.error(error, '_find');
210-
return null;
211-
}
212-
213-
return obj;
214-
}))
184+
Promise.all(
185+
results.map(async file => await this._buildFileObject(file))
186+
)
215187
);
216188

217189
if(e) throw e;
@@ -221,6 +193,45 @@ class ModClean extends ModClean_Utils {
221193
this.emit('file:list', files);
222194
return files;
223195
}
196+
197+
/**
198+
* Creates file object for the specified `file` path
199+
* @async
200+
* @private
201+
* @param {String} file File path to create object for
202+
* @return {Object} Object with all file properties
203+
*/
204+
async _buildFileObject(file) {
205+
let obj = {
206+
path: file,
207+
fullPath: path.join(this.options.cwd, file),
208+
dir: path.join(this.options.cwd, path.parse(file).dir),
209+
name: path.basename(file),
210+
isModule: false,
211+
isDirectory: null
212+
};
213+
214+
try {
215+
let stats = await stat(obj.fullPath),
216+
isDir = stats.isDirectory();
217+
218+
obj.isDirectory = isDir;
219+
220+
if(isDir) {
221+
let list = await readdir(obj.fullPath),
222+
parent = path.basename(path.join(obj.fullPath, '../'));
223+
224+
if(list.indexOf('package.json') !== -1 && parent === (this.options.modulesDir || 'node_modules')) obj.isModule = true;
225+
}
226+
227+
obj.stat = stats;
228+
} catch(error) {
229+
this.error(error, '_buildFileObject');
230+
return null;
231+
}
232+
233+
return obj;
234+
}
224235

225236
/**
226237
* Processes the found files and deletes them in parallel.

0 commit comments

Comments
 (0)