-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (69 loc) · 2.34 KB
/
app.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require('express')
const fs = require('fs');
const path = require('path');
const app = express()
app.use(express.json());
// route
app.post('/createFolderStructure', createFolderStructure)
app.use((req, res)=>{
res.status(404).send('Route not found')
})
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log('app is listening on port:', port);
})
app.on('error', (error) => {
console.error(error);
})
// controller
async function createFolderStructure(req, res) {
try {
const body = req.body
if (!body.basePath) {
return res.status(400).send('basePath is required in body')
}
if (!body.structure) {
return res.status(400).send('structure is required in body')
}
let logs = createDirectoriesRecursively(body.basePath, body.structure)
return res.status(200).send({ logs })
} catch (error) {
console.error(error)
res.status(500).send('OOps, Something went wrong !!!')
}
}
// service
function createDirectoriesRecursively(basePath, obj) {
let logs = []
// Ensure the base path exists
if (!fs.existsSync(basePath)) {
fs.mkdirSync(basePath);
}
// Iterate through the keys of the object
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const currentPath = path.join(basePath, key);
// If the value is an object, create a directory and recurse
if (typeof obj[key] === 'object' && obj[key] !== null) {
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath);
logs.push('FOLDER CREATED :' + currentPath);
}
else {
logs.push('FOLDER EXISTS :' + currentPath);
}
const innerlogs = createDirectoriesRecursively(currentPath, obj[key]);
logs = [...logs, ...innerlogs]
} else {
// If the value is not an object, create a file with its content
if (!fs.existsSync(currentPath)) {
logs.push('FILE CREATED :' + currentPath);
fs.writeFileSync(currentPath, obj[key]);
} else {
logs.push('FILE EXISTS :' + currentPath);
}
}
}
}
return logs
}