-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
40 lines (32 loc) · 1004 Bytes
/
test.js
File metadata and controls
40 lines (32 loc) · 1004 Bytes
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
const fs = require('fs').promises
const path = require('path')
const readFolder = async (dir) => {
const result = {}
const entries = await fs.readdir(dir, { withFileTypes: true })
for (const entry of entries) {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
result[entry.name] = { directory: await readFolder(fullPath) }
} else {
const contents = await fs.readFile(fullPath, 'utf8')
result[entry.name] = { file: { contents } }
}
}
return result
}
const main = async () => {
const folderPath = process.argv[2] // Pass folder path as an argument
const outputPath = process.argv[3] || 'output.json'
if (!folderPath) {
console.error('Please provide a folder path')
process.exit(1)
}
try {
const folderStructure = await readFolder(folderPath)
await fs.writeFile(outputPath, JSON.stringify(folderStructure, null, 2))
console.log(`Output saved to ${outputPath}`)
} catch (error) {
console.error('Error reading folder:', error)
}
}
main()