folder-generator 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/generator.js +25 -17
- package/package.json +1 -1
package/lib/generator.js
CHANGED
|
@@ -10,43 +10,51 @@ function parseStructure(text) {
|
|
|
10
10
|
for (const line of lines) {
|
|
11
11
|
if (!line.trim()) continue;
|
|
12
12
|
|
|
13
|
-
//
|
|
14
|
-
const
|
|
15
|
-
const name = line.replace(/[├└──│ ]/g, '').trim();
|
|
13
|
+
// Normalize line for Windows (replace \ with /)
|
|
14
|
+
const normalizedLine = line.replace(/\\/g, '/');
|
|
16
15
|
|
|
17
|
-
//
|
|
16
|
+
// Determine depth - count leading spaces before tree characters
|
|
17
|
+
const indentMatch = normalizedLine.match(/^ */);
|
|
18
|
+
const indent = indentMatch ? indentMatch[0].length : 0;
|
|
19
|
+
const depth = Math.floor(indent / 4);
|
|
20
|
+
|
|
21
|
+
// Extract name (remove tree characters)
|
|
22
|
+
const name = normalizedLine.replace(/^[ │├└─]*/, '').trim();
|
|
23
|
+
|
|
24
|
+
if (!name) continue;
|
|
25
|
+
|
|
26
|
+
// Navigate to correct parent
|
|
18
27
|
currentPath = currentPath.slice(0, depth + 1);
|
|
19
28
|
|
|
20
29
|
// Create new node
|
|
21
30
|
const newNode = { name, children: [] };
|
|
22
31
|
currentPath[currentPath.length - 1].children.push(newNode);
|
|
23
32
|
|
|
24
|
-
// Add to path for
|
|
33
|
+
// Add to path for children
|
|
25
34
|
currentPath.push(newNode);
|
|
26
35
|
}
|
|
27
36
|
|
|
28
37
|
return root;
|
|
29
38
|
}
|
|
30
39
|
|
|
31
|
-
function createFiles(node, currentPath = ''
|
|
40
|
+
function createFiles(node, currentPath = '.') {
|
|
32
41
|
const fullPath = path.join(currentPath, node.name);
|
|
33
42
|
|
|
34
43
|
if (node.name.includes('.')) {
|
|
35
44
|
// It's a file
|
|
45
|
+
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
36
46
|
fs.writeFileSync(fullPath, '');
|
|
37
|
-
console.log(chalk.green(`Created file: ${
|
|
38
|
-
} else {
|
|
47
|
+
console.log(chalk.green(`Created file: ${fullPath}`));
|
|
48
|
+
} else if (node.name) {
|
|
39
49
|
// It's a directory
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
console.log(chalk.blue(`Created directory: ${path.relative(rootPath || currentPath, fullPath)}`));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Create children
|
|
46
|
-
for (const child of node.children) {
|
|
47
|
-
createFiles(child, fullPath, rootPath || fullPath);
|
|
48
|
-
}
|
|
50
|
+
fs.mkdirSync(fullPath, { recursive: true });
|
|
51
|
+
console.log(chalk.blue(`Created directory: ${fullPath}`));
|
|
49
52
|
}
|
|
53
|
+
|
|
54
|
+
// Create children
|
|
55
|
+
node.children.forEach(child => {
|
|
56
|
+
createFiles(child, node.name ? fullPath : currentPath);
|
|
57
|
+
});
|
|
50
58
|
}
|
|
51
59
|
|
|
52
60
|
module.exports = { parseStructure, createFiles };
|