import fs from 'fs'; import path from 'path'; import { getSubFolder, isDirectory } from '@utils/file'; import { getPathOnCWD, removeCWDPath } from '@utils/path'; import { getTargetFolder } from './getTargetFolder'; export async function getTargetFolderRecursive( targetFolder: string, ): Promise { const files = fs.readdirSync(targetFolder, { encoding: 'utf-8', withFileTypes: true, }); const getTargetPathByFileName = (file: fs.Dirent) => { return path.resolve(targetFolder, file.name); }; const dirs = files .filter(isDirectory) .map(getTargetPathByFileName) .map(removeCWDPath) .concat([removeCWDPath(targetFolder)]) .reverse(); const target = await getTargetFolder(dirs); const targetStaticPath = getPathOnCWD(target); const targetSubFolders = getSubFolder(targetStaticPath); const isSelectCurrentFolder = targetStaticPath === targetFolder; const hasSubFolder = !!targetSubFolders.length; const isRecursive = !isSelectCurrentFolder && hasSubFolder; if (isRecursive) return await getTargetFolderRecursive(targetStaticPath); return targetStaticPath; }