folder-generator 1.3.1 → 1.3.3
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/bin/cli.js +28 -20
- package/lib/generator.js +12 -10
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -5,32 +5,40 @@ const path = require('path');
|
|
|
5
5
|
const chalk = require('chalk');
|
|
6
6
|
const { parseStructure, createFiles } = require('../lib/generator');
|
|
7
7
|
|
|
8
|
-
// Normalize input across operating systems
|
|
9
8
|
function normalizeInput(input) {
|
|
10
9
|
return input
|
|
11
|
-
.replace(/\r\n/g, '\n') //
|
|
10
|
+
.replace(/\r\n/g, '\n') // Normalize line endings
|
|
12
11
|
.replace(/\\/g, '/') // Normalize path separators
|
|
13
|
-
.replace(
|
|
12
|
+
.replace(/\t/g, ' '); // Convert tabs to spaces
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
function main() {
|
|
16
|
+
let input = process.argv.slice(2).join(' ');
|
|
17
17
|
|
|
18
|
-
if
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
18
|
+
// If no arguments, check if piped input exists
|
|
19
|
+
if (!input && !process.stdin.isTTY) {
|
|
20
|
+
input = fs.readFileSync(0, 'utf-8');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!input) {
|
|
24
|
+
console.error(chalk.red('Error: No input provided'));
|
|
25
|
+
console.log(chalk.yellow('Usage: foldgen "your/structure"'));
|
|
26
|
+
console.log(chalk.yellow(' or: echo "structure" | foldgen'));
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
22
29
|
|
|
23
|
-
try {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
try {
|
|
31
|
+
const normalized = normalizeInput(input);
|
|
32
|
+
console.log(chalk.yellow('\nParsing structure...'));
|
|
33
|
+
|
|
34
|
+
const structure = parseStructure(normalized);
|
|
35
|
+
createFiles(structure);
|
|
36
|
+
|
|
37
|
+
console.log(chalk.bold.green('\n✔ Folder structure created successfully!'));
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error(chalk.red('\n✖ Error creating structure:'), error.message);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
35
42
|
}
|
|
36
43
|
|
|
44
|
+
main();
|
package/lib/generator.js
CHANGED
|
@@ -8,12 +8,10 @@ function parseStructure(text) {
|
|
|
8
8
|
let stack = [{ node: root, depth: -1 }];
|
|
9
9
|
|
|
10
10
|
for (const line of lines) {
|
|
11
|
-
// Calculate depth based on leading
|
|
12
|
-
const depth =
|
|
11
|
+
// Calculate depth based on leading characters
|
|
12
|
+
const depth = (line.match(/^[ │├└─]*/)[0].length) / 4;
|
|
13
13
|
const name = line.replace(/^[ │├└─]*/, '').trim();
|
|
14
14
|
|
|
15
|
-
if (!name) continue;
|
|
16
|
-
|
|
17
15
|
// Find the correct parent
|
|
18
16
|
while (depth <= stack[stack.length - 1].depth) {
|
|
19
17
|
stack.pop();
|
|
@@ -28,21 +26,25 @@ function parseStructure(text) {
|
|
|
28
26
|
return root;
|
|
29
27
|
}
|
|
30
28
|
|
|
31
|
-
function createFiles(node,
|
|
32
|
-
|
|
29
|
+
function createFiles(node, currentPath = '.') {
|
|
30
|
+
if (!node) return;
|
|
31
|
+
|
|
32
|
+
const fullPath = path.join(currentPath, node.name);
|
|
33
33
|
|
|
34
34
|
if (node.name.includes('.')) {
|
|
35
|
-
//
|
|
35
|
+
// It's a file - ensure directory exists first
|
|
36
36
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
37
37
|
fs.writeFileSync(fullPath, '');
|
|
38
|
-
console.log(chalk.green(
|
|
38
|
+
console.log(chalk.green(`✓ Created file: ${fullPath}`));
|
|
39
39
|
} else if (node.name) {
|
|
40
|
+
// It's a directory
|
|
40
41
|
fs.mkdirSync(fullPath, { recursive: true });
|
|
41
|
-
console.log(chalk.blue(
|
|
42
|
+
console.log(chalk.blue(`✓ Created directory: ${fullPath}`));
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
// Process children
|
|
44
46
|
node.children.forEach(child => {
|
|
45
|
-
createFiles(child, node.name ? fullPath :
|
|
47
|
+
createFiles(child, node.name ? fullPath : currentPath);
|
|
46
48
|
});
|
|
47
49
|
}
|
|
48
50
|
|