papagaio 0.7.1 → 0.7.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/README.md CHANGED
@@ -9,7 +9,7 @@ Minimal yet powerful text preprocessor.
9
9
 
10
10
  ## Installation
11
11
  ```javascript
12
- import { Papagaio } from './src/papagaio.js';
12
+ import { Papagaio } from './papagaio.js';
13
13
  const papagaio = new Papagaio();
14
14
  const result = papagaio.process(input);
15
15
  ```
package/bin/cli.mjs CHANGED
@@ -7,147 +7,131 @@ const isNode = typeof process !== 'undefined' && process.versions && process.ver
7
7
  // MAIN FUNCTION
8
8
  // ============================================================================
9
9
  async function main() {
10
- // ============================================================================
11
- // IMPORTS - Branch por runtime
12
- // ============================================================================
13
- let Papagaio, std, os, fs, pkg;
14
-
15
- if (isQuickJS) {
16
- // QuickJS imports
17
- const stdModule = await import("std");
18
- const osModule = await import("os");
19
- std = stdModule;
20
- os = osModule;
21
- const { Papagaio: P } = await import("../src/papagaio.js");
22
- Papagaio = P;
23
- } else {
24
- // Node.js imports
25
- const fsModule = await import("fs");
26
- fs = fsModule.default;
27
-
28
- // Load package.json usando fs ao invés de require
29
- const pkgPath = new URL("../package.json", import.meta.url);
30
- const pkgContent = fs.readFileSync(pkgPath, "utf8");
31
- pkg = JSON.parse(pkgContent);
32
-
33
- const { Papagaio: P } = await import("../src/papagaio.js");
34
- Papagaio = P;
35
- }
36
-
37
- // ============================================================================
38
- // ABSTRAÇÃO DE CONSOLE/STD
39
- // ============================================================================
40
- const output = {
41
- log: isQuickJS ? (msg) => std.out.puts(msg + "\n") : console.log,
42
- error: isQuickJS ? (msg) => std.err.puts(msg + "\n") : console.error,
43
- exit: isQuickJS ? std.exit : process.exit
44
- };
45
-
46
- // ============================================================================
47
- // PARSE ARGUMENTS
48
- // ============================================================================
49
- const args = isQuickJS ? scriptArgs.slice(1) : process.argv.slice(2);
50
- const VERSION = isQuickJS ? "0.6.0" : pkg.version;
51
-
52
- // Help & Version
53
- if (args.includes("-v") || args.includes("--version")) {
54
- output.log(VERSION);
55
- output.exit(0);
56
- }
57
-
58
- if (args.includes("-h") || args.includes("--help")) {
59
- output.log(`Usage: papagaio [options] <file1> [file2] [...]
10
+ // ============================================================================
11
+ // IMPORTS - Branch por runtime
12
+ // ============================================================================
13
+ let Papagaio, std, os, fs, pkg;
14
+
15
+ if (isQuickJS) {
16
+ // QuickJS imports
17
+ const stdModule = await import("std");
18
+ const osModule = await import("os");
19
+ std = stdModule;
20
+ os = osModule;
21
+ const { Papagaio: P } = await import("../papagaio.js");
22
+ Papagaio = P;
23
+ } else {
24
+ // Node.js imports
25
+ const fsModule = await import("fs");
26
+ fs = fsModule.default;
27
+
28
+ // Load package.json usando fs ao invés de require
29
+ const pkgPath = new URL("../package.json", import.meta.url);
30
+ const pkgContent = fs.readFileSync(pkgPath, "utf8");
31
+ pkg = JSON.parse(pkgContent);
32
+
33
+ const { Papagaio: P } = await import("../papagaio.js");
34
+ Papagaio = P;
35
+ }
36
+
37
+ // ============================================================================
38
+ // ABSTRAÇÃO DE CONSOLE/STD
39
+ // ============================================================================
40
+ const output = {
41
+ log: isQuickJS ? (msg) => std.out.puts(msg + "\n") : console.log,
42
+ error: isQuickJS ? (msg) => std.err.puts(msg + "\n") : console.error,
43
+ exit: isQuickJS ? std.exit : process.exit
44
+ };
45
+
46
+ // ============================================================================
47
+ // PARSE ARGUMENTS
48
+ // ============================================================================
49
+ const args = isQuickJS ? scriptArgs.slice(1) : process.argv.slice(2);
50
+ const VERSION = isQuickJS ? "0.6.0" : pkg.version;
51
+
52
+ // Help & Version
53
+ if (args.includes("-v") || args.includes("--version")) {
54
+ output.log(VERSION);
55
+ output.exit(0);
56
+ }
57
+
58
+ if (args.includes("-h") || args.includes("--help")) {
59
+ output.log(`Usage: papagaio [options] <file1> [file2] [...]
60
60
 
61
61
  Options:
62
62
  -h, --help Show this help message
63
63
  -v, --version Show version number
64
- --sigil <symbol> Set sigil symbol
65
- --open <symbol> Set open symbol
66
- --close <symbol> Set close symbol
67
64
 
68
65
  Examples:
69
66
  papagaio input.txt
70
67
  papagaio file1.txt file2.txt file3.txt
71
- papagaio *.txt
72
- papagaio --sigil @ --open [ --close ] input.txt`);
73
- output.exit(0);
74
- }
75
-
76
- // Parse options
77
- const sigilIndex = args.findIndex(arg => arg === "--sigil");
78
- const openIndex = args.findIndex(arg => arg === "--open");
79
- const closeIndex = args.findIndex(arg => arg === "--close");
80
-
81
- const sigil = sigilIndex !== -1 ? args[sigilIndex + 1] : undefined;
82
- const open = openIndex !== -1 ? args[openIndex + 1] : undefined;
83
- const close = closeIndex !== -1 ? args[closeIndex + 1] : undefined;
84
-
85
- // Get input files
86
- const files = args.filter((arg, i) => {
87
- if (arg.startsWith("-")) return false;
88
- if (i > 0 && (args[i - 1] === "--sigil" || args[i - 1] === "--open" || args[i - 1] === "--close")) return false;
89
- return true;
90
- });
91
-
92
- if (files.length === 0) {
93
- output.error("Error: no input file specified.\nUse --help for usage.");
94
- output.exit(1);
95
- }
96
-
97
- // ============================================================================
98
- // FILE READING ABSTRACTION
99
- // ============================================================================
100
- function readFile(filepath) {
101
- if (isQuickJS) {
102
- const f = std.open(filepath, "r");
103
- if (!f) {
104
- throw new Error(`cannot open file '${filepath}'`);
105
- }
106
- const content = f.readAsString();
107
- f.close();
108
- return content;
109
- } else {
110
- if (!fs.existsSync(filepath)) {
111
- throw new Error(`file not found: ${filepath}`);
112
- }
113
- return fs.readFileSync(filepath, "utf8");
68
+ papagaio *.txt`);
69
+ output.exit(0);
70
+ }
71
+
72
+ // Get input files
73
+ const files = args.filter((arg, i) => {
74
+ if (arg.startsWith("-")) return false;
75
+ return true;
76
+ });
77
+
78
+ if (files.length === 0) {
79
+ output.error("Error: no input file specified.\nUse --help for usage.");
80
+ output.exit(1);
81
+ }
82
+
83
+ // ============================================================================
84
+ // FILE READING ABSTRACTION
85
+ // ============================================================================
86
+ function readFile(filepath) {
87
+ if (isQuickJS) {
88
+ const f = std.open(filepath, "r");
89
+ if (!f) {
90
+ throw new Error(`cannot open file '${filepath}'`);
91
+ }
92
+ const content = f.readAsString();
93
+ f.close();
94
+ return content;
95
+ } else {
96
+ if (!fs.existsSync(filepath)) {
97
+ throw new Error(`file not found: ${filepath}`);
98
+ }
99
+ return fs.readFileSync(filepath, "utf8");
100
+ }
114
101
  }
115
- }
116
-
117
- // ============================================================================
118
- // READ AND CONCATENATE FILES
119
- // ============================================================================
120
- let concatenatedSrc = "";
121
- let hasErrors = false;
122
-
123
- for (const file of files) {
124
- try {
125
- const src = readFile(file);
126
- concatenatedSrc += src;
127
- } catch (error) {
128
- output.error(`Error reading ${file}: ${error.message || error}`);
129
- hasErrors = true;
102
+
103
+ // ============================================================================
104
+ // READ AND CONCATENATE FILES
105
+ // ============================================================================
106
+ let concatenatedSrc = "";
107
+ let hasErrors = false;
108
+
109
+ for (const file of files) {
110
+ try {
111
+ const src = readFile(file);
112
+ concatenatedSrc += src;
113
+ } catch (error) {
114
+ output.error(`Error reading ${file}: ${error.message || error}`);
115
+ hasErrors = true;
116
+ }
130
117
  }
131
- }
132
-
133
- if (hasErrors) {
134
- output.exit(1);
135
- }
136
-
137
- // ============================================================================
138
- // PROCESS CONCATENATED INPUT
139
- // ============================================================================
140
- const p = new Papagaio(sigil, open, close);
141
- const out = p.process(concatenatedSrc);
142
- output.log(out);
118
+
119
+ if (hasErrors) {
120
+ output.exit(1);
121
+ }
122
+
123
+ // PROCESS CONCATENATED INPUT
124
+ const p = new Papagaio();
125
+ const out = p.process(concatenatedSrc);
126
+ output.log(out);
143
127
  }
144
128
 
145
- // Executa main
129
+ // main
146
130
  main().catch(err => {
147
- const output = isQuickJS
148
- ? (msg) => std.err.puts(msg + "\n")
149
- : console.error;
150
- output("Fatal error: " + (err.message || err));
151
- const exit = isQuickJS ? std.exit : process.exit;
152
- exit(1);
131
+ const output = isQuickJS
132
+ ? (msg) => std.err.puts(msg + "\n")
133
+ : console.error;
134
+ output("Fatal error: " + (err.message || err));
135
+ const exit = isQuickJS ? std.exit : process.exit;
136
+ exit(1);
153
137
  });