{"size": 1378, "ext": "js", "max_stars_repo_path": "src/helpers/fs-helper.js", "max_stars_repo_name": "petrmiko/konteiner", "max_stars_repo_stars_event_min_datetime": "2019-07-12T02:49:52.000Z", "max_stars_repo_stars_event_max_datetime": "2019-07-12T02:49:52.000Z", "max_issues_repo_path": "src/helpers/fs-helper.js", "max_issues_repo_name": "petrmiko/konteiner", "max_issues_repo_issues_event_min_datetime": "2022-03-18T15:11:46.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-18T15:12:33.000Z", "max_forks_repo_path": "src/helpers/fs-helper.js", "max_forks_repo_name": "petrmiko/konteiner", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 25.5185185185, "max_line_length": 89, "alphanum_fraction": 0.6785195936}
const fs = require('fs')
const path = require('path')

/**
 * @module FsHelper
 */

/**
  * @typedef FsListConfig
  * @property {number=} searchDepth default -1
  * @property {Array<string>=} supportedExtensions
  */

/**
 * @param {string} startPath
 * @param {FsListConfig=} config
 * @param {Array<string>=} acc
 * @param {number} currentDepth
 * @returns {Array<string>}
 */
const getFileListSync = (startPath, config = {}, acc = [], currentDepth = 1) => {
	const {supportedExtensions = [], searchDepth = -1} = config
	let saneStartPath = startPath
	if (!fs.existsSync(startPath)) {
		supportedExtensions.forEach((supportedAppendSuffix) => {
			const tmpPath =  startPath + supportedAppendSuffix
			if(fs.existsSync(tmpPath)) {
				saneStartPath = tmpPath
				return
			}
		})
		// no adjustment made -> path really does not exist
		if (saneStartPath === startPath) return acc
	}

	if (!fs.statSync(saneStartPath).isDirectory()) {
		const extension = path.parse(saneStartPath).ext
		if(!supportedExtensions.includes(extension)) {
			return acc
		}
		return [].concat(acc, saneStartPath)
	} else {
		if (searchDepth > 0 && searchDepth < currentDepth) return acc
		return fs.readdirSync(saneStartPath)
			.reduce((acc, file) => {
				return getFileListSync(path.join(saneStartPath, file), config, acc, currentDepth + 1)
			}, acc)
	}
}

module.exports = {
	getFileListSync,
}
