Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs');
- var path = require('path');
- function isDirectory(dirPath) {
- return fs.statSync(dirPath).isDirectory();
- }
- function endsWithExtension(file, extension) {
- return file.endsWith(extension);
- }
- class FileSearch {
- constructor(fileExtension, skipDirectories = null) {
- this.fileExtension = fileExtension || '';
- if (!fileExtension.includes('.')) {
- throw new Error('File extension should contain a dot (.)');
- }
- this.skipDirectories = skipDirectories || ['.vscode', '.git', 'node_modules'];
- }
- /* Function for borrow (namespace.js)*/
- /**
- *
- * @param {string} item - full path to the directory or a file
- */
- skipCurrentDirectory(item) {
- return this.skipDirectories.includes(item);
- }
- getFiles(dirPath, files) {
- files = files || [];
- var items = fs.readdirSync(dirPath);
- items.forEach(item => {
- if (this.skipCurrentDirectory(item)) {
- return;
- }
- var fullPath = path.join(dirPath, item);
- if (isDirectory(fullPath)) {
- files = this.getFiles(fullPath, files);
- } else if (endsWithExtension(item, this.fileExtension)) {
- files.push(fullPath);
- }
- });
- return files;
- }
- }
- module.exports = FileSearch;
Add Comment
Please, Sign In to add comment