-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprintDirectoryStructure.js
54 lines (42 loc) · 1.66 KB
/
printDirectoryStructure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const fs = require('fs')
const path = require('path')
function isInBlacklist(file) {
const list = ['node_modules', 'dist', 'build', '.git', '.idea']
return list.includes(file)
}
function printDirectoryStructure(directoryPath, indent = 0, fileDescriptor) {
const files = fs.readdirSync(directoryPath)
files.forEach(file => {
const filePath = path.join(directoryPath, file)
// Если это папка "node_modules", пропускаем ее
if (isInBlacklist(file)) {
return
}
// Создаем строку с именем файла или папки и отступом, соответствующим уровню вложенности
const line = `${'-'.repeat(indent)}- ${file}\n`
// Записываем эту строку в файл с помощью метода fs.write
fs.write(fileDescriptor, line, err => {
if (err) {
throw err
}
})
const fileStats = fs.statSync(filePath)
// Если это папка, вызываем эту же функцию для вывода ее содержимого
if (fileStats.isDirectory()) {
printDirectoryStructure(filePath, indent + 2, fileDescriptor)
}
})
}
// Создаем файл с именем "анализ.txt" и запускаем функцию для записи в него структуры файлов
fs.open('folderStructure.txt', 'w', (err, fileDescriptor) => {
if (err) {
throw err
}
printDirectoryStructure('./packages/create-by-template', 0, fileDescriptor)
// Закрываем файл после записи
fs.close(fileDescriptor, err => {
if (err) {
throw err
}
})
})