folder-generator 1.3.3 → 1.3.5

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,39 +1,56 @@
1
1
  #!/usr/bin/env node
2
-
3
2
  const fs = require('fs');
4
3
  const path = require('path');
5
4
  const chalk = require('chalk');
6
5
  const { parseStructure, createFiles } = require('../lib/generator');
6
+ const yargs = require('yargs/yargs');
7
+ const { hideBin } = require('yargs/helpers');
7
8
 
8
- function normalizeInput(input) {
9
- return input
10
- .replace(/\r\n/g, '\n') // Normalize line endings
11
- .replace(/\\/g, '/') // Normalize path separators
12
- .replace(/\t/g, ' '); // Convert tabs to spaces
13
- }
14
-
15
- function main() {
16
- let input = process.argv.slice(2).join(' ');
9
+ async function main() {
10
+ const argv = yargs(hideBin(process.argv))
11
+ .option('input', {
12
+ alias: 'i',
13
+ type: 'string',
14
+ description: 'Folder structure text'
15
+ })
16
+ .option('file', {
17
+ alias: 'f',
18
+ type: 'string',
19
+ description: 'Path to file containing folder structure'
20
+ })
21
+ .help()
22
+ .alias('help', 'h')
23
+ .argv;
17
24
 
18
- // If no arguments, check if piped input exists
19
- if (!input && !process.stdin.isTTY) {
20
- input = fs.readFileSync(0, 'utf-8');
21
- }
25
+ let input = '';
22
26
 
23
- if (!input) {
27
+ if (argv.file) {
28
+ try {
29
+ input = fs.readFileSync(argv.file, 'utf8');
30
+ } catch (err) {
31
+ console.error(chalk.red(`Error reading file: ${argv.file}`));
32
+ process.exit(1);
33
+ }
34
+ } else if (argv.input) {
35
+ input = argv.input;
36
+ } else if (!process.stdin.isTTY) {
37
+ // Read from stdin if piped
38
+ const chunks = [];
39
+ for await (const chunk of process.stdin) chunks.push(chunk);
40
+ input = Buffer.concat(chunks).toString('utf8');
41
+ } else {
24
42
  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'));
43
+ console.log(chalk.yellow('Usage:'));
44
+ console.log(chalk.yellow(' foldgen --input "your/structure"'));
45
+ console.log(chalk.yellow(' foldgen --file structure.txt'));
46
+ console.log(chalk.yellow(' echo "structure" | foldgen'));
27
47
  process.exit(1);
28
48
  }
29
49
 
30
50
  try {
31
- const normalized = normalizeInput(input);
32
51
  console.log(chalk.yellow('\nParsing structure...'));
33
-
34
- const structure = parseStructure(normalized);
52
+ const structure = parseStructure(input);
35
53
  createFiles(structure);
36
-
37
54
  console.log(chalk.bold.green('\n✔ Folder structure created successfully!'));
38
55
  } catch (error) {
39
56
  console.error(chalk.red('\n✖ Error creating structure:'), error.message);
package/lib/generator.js CHANGED
@@ -8,11 +8,13 @@ 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 characters
11
+ // Calculate depth based on leading whitespace
12
12
  const depth = (line.match(/^[ │├└─]*/)[0].length) / 4;
13
13
  const name = line.replace(/^[ │├└─]*/, '').trim();
14
14
 
15
- // Find the correct parent
15
+ if (!name) continue;
16
+
17
+ // Find correct parent
16
18
  while (depth <= stack[stack.length - 1].depth) {
17
19
  stack.pop();
18
20
  }
@@ -32,7 +34,7 @@ function createFiles(node, currentPath = '.') {
32
34
  const fullPath = path.join(currentPath, node.name);
33
35
 
34
36
  if (node.name.includes('.')) {
35
- // It's a file - ensure directory exists first
37
+ // It's a file
36
38
  fs.mkdirSync(path.dirname(fullPath), { recursive: true });
37
39
  fs.writeFileSync(fullPath, '');
38
40
  console.log(chalk.green(`✓ Created file: ${fullPath}`));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "folder-generator",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "description": "CLI tool to generate folder structures from text input",
5
5
  "bin": {
6
6
  "foldgen": "./bin/cli.js"