UNPKG

als-file-list

Version:

Generates filtered and sorted list of all file paths in a directory and its subdirectories.

40 lines (32 loc) 1.63 kB
const fs = require('fs'); const path = require('path'); function createTestDirectory(root, levels = 4) { if (!fs.existsSync(root)) fs.mkdirSync(root); function createStructure(currentPath, currentLevel) { if (currentLevel > levels) return; for (let i = 0; i < 2; i++) { // Создаем две папки на каждом уровне const dirPath = path.join(currentPath, `folder_${currentLevel}_${i}`); if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath); } for (let j = 0; j < 2; j++) { // Создаем два файла в каждой папке const filePath = path.join(dirPath, `file_${currentLevel}_${j}.txt`); fs.writeFileSync(filePath, `Test file at level ${currentLevel} ${'content'.repeat(currentLevel)}`); } createStructure(dirPath, currentLevel + 1); // Рекурсивный вызов для следующего уровня } } createStructure(root, 1); } function deleteTestDirectory(directoryPath) { if (fs.existsSync(directoryPath)) { fs.readdirSync(directoryPath).forEach(file => { const currentPath = path.join(directoryPath, file); if (fs.lstatSync(currentPath).isDirectory()) deleteTestDirectory(currentPath); else fs.unlinkSync(currentPath); }); // Удаление текущей директории после очистки fs.rmdirSync(directoryPath); } } module.exports = {createTestDirectory, deleteTestDirectory}