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 +38 -21
- package/lib/generator.js +5 -3
- package/package.json +1 -1
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
|
|
9
|
-
|
|
10
|
-
.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
19
|
-
if (!input && !process.stdin.isTTY) {
|
|
20
|
-
input = fs.readFileSync(0, 'utf-8');
|
|
21
|
-
}
|
|
25
|
+
let input = '';
|
|
22
26
|
|
|
23
|
-
if (
|
|
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:
|
|
26
|
-
console.log(chalk.yellow('
|
|
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
|
|
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
|
-
|
|
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
|
|
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}`));
|