folder-generator 1.0.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,50 +3,47 @@ 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
- // Determine the depth based on indentation
14
- const depth = (line.match(/[├└──]/)?.index || 0) / 4;
15
- const name = line.replace(/[├└──│ ]/g, '').trim();
16
-
17
- // Navigate to the correct parent
18
- currentPath = currentPath.slice(0, depth + 1);
19
-
20
- // Create new node
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
+
15
+ if (!name) continue;
16
+
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;
21
23
  const newNode = { name, children: [] };
22
- currentPath[currentPath.length - 1].children.push(newNode);
23
-
24
- // Add to path for potential children
25
- currentPath.push(newNode);
24
+ parent.children.push(newNode);
25
+ stack.push({ node: newNode, depth });
26
26
  }
27
-
27
+
28
28
  return root;
29
29
  }
30
30
 
31
- function createFiles(node, currentPath = '', rootPath = '') {
32
- const fullPath = path.join(currentPath, node.name);
33
-
31
+ function createFiles(node, basePath = '.') {
32
+ const fullPath = path.join(basePath, node.name);
33
+
34
34
  if (node.name.includes('.')) {
35
- // It's a file
35
+ // Ensure parent directory exists
36
+ fs.mkdirSync(path.dirname(fullPath), { recursive: true });
36
37
  fs.writeFileSync(fullPath, '');
37
- console.log(chalk.green(`Created file: ${path.relative(rootPath, fullPath)}`));
38
- } else {
39
- // It's a directory
40
- if (node.name) { // Skip empty root name
41
- fs.mkdirSync(fullPath, { recursive: true });
42
- console.log(chalk.blue(`Created directory: ${path.relative(rootPath || currentPath, fullPath)}`));
43
- }
44
-
45
- // Create children
46
- for (const child of node.children) {
47
- createFiles(child, fullPath, rootPath || fullPath);
48
- }
38
+ console.log(chalk.green(`Created file: ${fullPath}`));
39
+ } else if (node.name) {
40
+ fs.mkdirSync(fullPath, { recursive: true });
41
+ console.log(chalk.blue(`Created directory: ${fullPath}`));
49
42
  }
43
+
44
+ node.children.forEach(child => {
45
+ createFiles(child, node.name ? fullPath : basePath);
46
+ });
50
47
  }
51
48
 
52
49
  module.exports = { parseStructure, createFiles };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "folder-generator",
3
- "version": "1.0.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"