folder-generator 1.3.0 → 1.3.2

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
@@ -1,33 +1,44 @@
1
+ #!/usr/bin/env node
2
+
1
3
  const fs = require('fs');
2
4
  const path = require('path');
3
5
  const chalk = require('chalk');
4
- const { parseStructure, createFiles } = require('../lib/generator');
6
+ const { parseStructure, createFiles } = require('./generator');
5
7
 
6
- // Normalize input across operating systems
7
8
  function normalizeInput(input) {
8
9
  return input
9
- .replace(/\r\n/g, '\n') // Convert Windows line endings
10
+ .replace(/\r\n/g, '\n') // Normalize line endings
10
11
  .replace(/\\/g, '/') // Normalize path separators
11
- .replace(/\/\//g, '/'); // Remove duplicate slashes
12
+ .replace(/\t/g, ' '); // Convert tabs to spaces
12
13
  }
13
14
 
14
- const input = process.argv.slice(2).join(' ');
15
+ function main() {
16
+ let input = process.argv.slice(2).join(' ');
15
17
 
16
- if (!input) {
17
- console.error(chalk.red('Please provide a folder structure as text.'));
18
- process.exit(1);
19
- }
18
+ // If no arguments, check if piped input exists
19
+ if (!input && !process.stdin.isTTY) {
20
+ input = fs.readFileSync(0, 'utf-8');
21
+ }
20
22
 
21
- try {
22
- const normalized = normalizeInput(input);
23
- const structure = parseStructure(normalized);
24
-
25
- // Debug: Uncomment to see parsed structure
26
- // console.log('Parsed structure:', JSON.stringify(structure, null, 2));
27
-
28
- createFiles(structure);
29
- console.log(chalk.bold.green('\nFolder structure created successfully!'));
30
- } catch (error) {
31
- console.error(chalk.red('\nError creating folder structure:'), error);
32
- process.exit(1);
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
+ }
29
+
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
+ }
33
42
  }
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.0",
3
+ "version": "1.3.2",
4
4
  "description": "CLI tool to generate folder structures from text input",
5
5
  "bin": {
6
6
  "foldgen": "./bin/cli.js"