folder-generator 1.1.0 → 1.2.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/bin/cli.js CHANGED
@@ -3,75 +3,34 @@
3
3
  const fs = require('fs');
4
4
  const path = require('path');
5
5
  const chalk = require('chalk');
6
- const figlet = require('figlet');
7
- const yargs = require('yargs/yargs');
8
- const { hideBin } = require('yargs/helpers');
9
6
  const { parseStructure, createFiles } = require('../lib/generator');
10
7
 
11
- // Parse command line arguments
12
- const argv = yargs(hideBin(process.argv))
13
- .usage('Usage: $0 [options]')
14
- .option('input', {
15
- alias: 'i',
16
- type: 'string',
17
- description: 'Folder structure text'
18
- })
19
- .option('file', {
20
- alias: 'f',
21
- type: 'string',
22
- description: 'Path to file containing folder structure'
23
- })
24
- .option('output', {
25
- alias: 'o',
26
- type: 'string',
27
- description: 'Output directory (default: current directory)',
28
- default: process.cwd()
29
- })
30
- .help()
31
- .alias('help', 'h')
32
- .argv;
33
-
34
- async function main() {
35
- // Display banner
36
- console.log(
37
- chalk.blue(
38
- figlet.textSync('FolderGen', { horizontalLayout: 'full' })
39
- )
40
- );
41
-
42
- let input = argv.input;
43
-
44
- // Read from file if specified
45
- if (argv.file) {
46
- try {
47
- input = fs.readFileSync(argv.file, 'utf8');
48
- } catch (err) {
49
- console.error(chalk.red(`Error reading file: ${argv.file}`));
50
- process.exit(1);
51
- }
52
- }
8
+ // Normalize input across operating systems
9
+ function normalizeInput(input) {
10
+ return input
11
+ .replace(/\r\n/g, '\n') // Convert Windows line endings
12
+ .replace(/\\/g, '/') // Normalize path separators
13
+ .replace(/\/\//g, '/'); // Remove duplicate slashes
14
+ }
53
15
 
54
- // Get input from stdin if no other source
55
- if (!input && !process.stdin.isTTY) {
56
- const chunks = [];
57
- for await (const chunk of process.stdin) chunks.push(chunk);
58
- input = Buffer.concat(chunks).toString('utf8');
59
- }
16
+ const input = process.argv.slice(2).join(' ');
60
17
 
61
- if (!input) {
62
- console.log(chalk.red('No input provided.'));
63
- console.log(chalk.yellow('Provide input via --input, --file, or stdin'));
64
- process.exit(1);
65
- }
18
+ if (!input) {
19
+ console.error(chalk.red('Please provide a folder structure as text.'));
20
+ process.exit(1);
21
+ }
66
22
 
67
- try {
68
- const structure = parseStructure(input);
69
- createFiles(structure, argv.output);
70
- console.log(chalk.bold.green('\nFolder structure created successfully!'));
71
- } catch (error) {
72
- console.error(chalk.red('\nError creating folder structure:'), error.message);
73
- process.exit(1);
74
- }
23
+ try {
24
+ const normalized = normalizeInput(input);
25
+ const structure = parseStructure(normalized);
26
+
27
+ // Debug: Uncomment to see parsed structure
28
+ // console.log('Parsed structure:', JSON.stringify(structure, null, 2));
29
+
30
+ createFiles(structure);
31
+ console.log(chalk.bold.green('\nFolder structure created successfully!'));
32
+ } catch (error) {
33
+ console.error(chalk.red('\nError creating folder structure:'), error);
34
+ process.exit(1);
75
35
  }
76
36
 
77
- main();
package/lib/generator.js CHANGED
@@ -3,57 +3,46 @@ const path = require('path');
3
3
  const chalk = require('chalk');
4
4
 
5
5
  function parseStructure(text) {
6
- const lines = text.split('\n');
6
+ const lines = text.split('\n').filter(line => line.trim());
7
7
  const root = { name: '', children: [] };
8
- let currentPath = [root];
9
-
8
+ let stack = [{ node: root, depth: -1 }];
9
+
10
10
  for (const line of lines) {
11
- if (!line.trim()) continue;
12
-
13
- // Normalize line for Windows (replace \ with /)
14
- const normalizedLine = line.replace(/\\/g, '/');
15
-
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
-
11
+ // Calculate depth based on leading whitespace and tree characters
12
+ const depth = Math.max(0, (line.match(/^[ │├└─]*/)[0].length / 4));
13
+ const name = line.replace(/^[ │├└─]*/, '').trim();
14
+
24
15
  if (!name) continue;
25
16
 
26
- // Navigate to correct parent
27
- currentPath = currentPath.slice(0, depth + 1);
28
-
29
- // Create new node
17
+ // Find the correct parent
18
+ while (depth <= stack[stack.length - 1].depth) {
19
+ stack.pop();
20
+ }
21
+
22
+ const parent = stack[stack.length - 1].node;
30
23
  const newNode = { name, children: [] };
31
- currentPath[currentPath.length - 1].children.push(newNode);
32
-
33
- // Add to path for children
34
- currentPath.push(newNode);
24
+ parent.children.push(newNode);
25
+ stack.push({ node: newNode, depth });
35
26
  }
36
-
27
+
37
28
  return root;
38
29
  }
39
30
 
40
- function createFiles(node, currentPath = '.') {
41
- const fullPath = path.join(currentPath, node.name);
42
-
31
+ function createFiles(node, basePath = '.') {
32
+ const fullPath = path.join(basePath, node.name);
33
+
43
34
  if (node.name.includes('.')) {
44
- // It's a file
35
+ // Ensure parent directory exists
45
36
  fs.mkdirSync(path.dirname(fullPath), { recursive: true });
46
37
  fs.writeFileSync(fullPath, '');
47
38
  console.log(chalk.green(`Created file: ${fullPath}`));
48
39
  } else if (node.name) {
49
- // It's a directory
50
40
  fs.mkdirSync(fullPath, { recursive: true });
51
41
  console.log(chalk.blue(`Created directory: ${fullPath}`));
52
42
  }
53
-
54
- // Create children
43
+
55
44
  node.children.forEach(child => {
56
- createFiles(child, node.name ? fullPath : currentPath);
45
+ createFiles(child, node.name ? fullPath : basePath);
57
46
  });
58
47
  }
59
48
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "folder-generator",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "CLI tool to generate folder structures from text input",
5
5
  "bin": {
6
6
  "foldgen": "./bin/cli.js"