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 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') // Convert Windows line endings
10
+ .replace(/\r\n/g, '\n') // Normalize line endings
12
11
  .replace(/\\/g, '/') // Normalize path separators
13
- .replace(/\/\//g, '/'); // Remove duplicate slashes
12
+ .replace(/\t/g, ' '); // Convert tabs to spaces
14
13
  }
15
14
 
16
- const input = process.argv.slice(2).join(' ');
15
+ function main() {
16
+ let input = process.argv.slice(2).join(' ');
17
17
 
18
- if (!input) {
19
- console.error(chalk.red('Please provide a folder structure as text.'));
20
- process.exit(1);
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
- 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);
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 whitespace and tree characters
12
- const depth = Math.max(0, (line.match(/^[ │├└─]*/)[0].length / 4));
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, basePath = '.') {
32
- const fullPath = path.join(basePath, node.name);
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
- // Ensure parent directory exists
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(`Created file: ${fullPath}`));
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(`Created directory: ${fullPath}`));
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 : basePath);
47
+ createFiles(child, node.name ? fullPath : currentPath);
46
48
  });
47
49
  }
48
50
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "folder-generator",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "CLI tool to generate folder structures from text input",
5
5
  "bin": {
6
6
  "foldgen": "./bin/cli.js"