folder-generator 1.3.9 → 2.0.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 +31 -19
- package/package.json +1 -1
package/lib/generator.js
CHANGED
|
@@ -4,49 +4,61 @@ const chalk = require('chalk');
|
|
|
4
4
|
|
|
5
5
|
function parseStructure(text) {
|
|
6
6
|
const lines = text.split('\n').filter(line => line.trim());
|
|
7
|
-
const root = { name: '', children: [] };
|
|
7
|
+
const root = { name: '', children: [], isDirectory: true }; // Root is always a directory
|
|
8
8
|
let stack = [{ node: root, depth: -1 }];
|
|
9
9
|
|
|
10
10
|
for (const line of lines) {
|
|
11
|
-
// Calculate depth based on leading
|
|
11
|
+
// Calculate depth based on leading tree characters
|
|
12
12
|
const depth = (line.match(/^[ │├└─]*/)[0].length) / 4;
|
|
13
|
-
const name = line.replace(/^[ │├└─]*/, '').trim();
|
|
14
13
|
|
|
14
|
+
// Extract the name, remove comments (using //), and trim whitespace
|
|
15
|
+
let name = line.replace(/^[ │├└─]*/, '').split('//')[0].trim();
|
|
15
16
|
if (!name) continue;
|
|
16
17
|
|
|
17
|
-
//
|
|
18
|
+
// A name ending with '/' is a directory
|
|
19
|
+
const isDirectory = name.endsWith('/');
|
|
20
|
+
if (isDirectory) {
|
|
21
|
+
name = name.slice(0, -1); // Remove the trailing '/' from the name
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Find the correct parent in the stack based on depth
|
|
18
25
|
while (depth <= stack[stack.length - 1].depth) {
|
|
19
26
|
stack.pop();
|
|
20
27
|
}
|
|
21
28
|
|
|
22
29
|
const parent = stack[stack.length - 1].node;
|
|
23
|
-
const newNode = { name, children: [] };
|
|
30
|
+
const newNode = { name, children: [], isDirectory };
|
|
24
31
|
parent.children.push(newNode);
|
|
25
32
|
stack.push({ node: newNode, depth });
|
|
26
33
|
}
|
|
27
|
-
|
|
28
34
|
return root;
|
|
29
35
|
}
|
|
30
36
|
|
|
37
|
+
|
|
31
38
|
function createFiles(node, currentPath = '.') {
|
|
32
39
|
if (!node) return;
|
|
33
40
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
// The root node has an empty name and represents the starting path.
|
|
42
|
+
const fullPath = node.name ? path.join(currentPath, node.name) : currentPath;
|
|
43
|
+
|
|
44
|
+
// Process only nodes with names (skips the initial root container)
|
|
45
|
+
if (node.name) {
|
|
46
|
+
if (node.isDirectory) {
|
|
47
|
+
// It's a directory
|
|
48
|
+
fs.mkdirSync(fullPath, { recursive: true });
|
|
49
|
+
console.log(chalk.blue(`✓ Created directory: ${fullPath}`));
|
|
50
|
+
} else {
|
|
51
|
+
// It's a file
|
|
52
|
+
// Ensure parent directory exists before creating the file.
|
|
53
|
+
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
54
|
+
fs.writeFileSync(fullPath, '');
|
|
55
|
+
console.log(chalk.green(`✓ Created file: ${fullPath}`));
|
|
56
|
+
}
|
|
45
57
|
}
|
|
46
58
|
|
|
47
|
-
// Process children
|
|
59
|
+
// Process children recursively, passing the new parent path
|
|
48
60
|
node.children.forEach(child => {
|
|
49
|
-
createFiles(child,
|
|
61
|
+
createFiles(child, fullPath);
|
|
50
62
|
});
|
|
51
63
|
}
|
|
52
64
|
|