papagaio 0.6.1 → 0.6.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.mjs ADDED
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env node
2
+ // Detecta o runtime
3
+ const isQuickJS = typeof scriptArgs !== 'undefined';
4
+ const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
5
+
6
+ // ============================================================================
7
+ // MAIN FUNCTION
8
+ // ============================================================================
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] [...]
60
+
61
+ Options:
62
+ -h, --help Show this help message
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
+
68
+ Examples:
69
+ papagaio input.txt
70
+ 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");
114
+ }
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;
130
+ }
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);
143
+ }
144
+
145
+ // Executa main
146
+ 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);
153
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "papagaio",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "easy yet powerful preprocessor",
5
5
  "main": "src/papagaio.js",
6
6
  "type": "module",
@@ -28,6 +28,6 @@
28
28
  },
29
29
  "homepage": "https://github.com/jardimdanificado/papagaio#readme",
30
30
  "bin": {
31
- "papagaio": "bin/cli.js"
31
+ "papagaio": "bin/cli.mjs"
32
32
  }
33
33
  }
package/bin/cli.js DELETED
@@ -1,34 +0,0 @@
1
- #!/usr/bin/env node
2
- import { Papagaio } from "../src/papagaio.js";
3
- import fs from "fs";
4
- import { createRequire } from "module";
5
- const require = createRequire(import.meta.url);
6
- const pkg = require("../package.json");
7
-
8
-
9
- // Help & Version
10
- const args = process.argv.slice(2);
11
- if (args.includes("-v") || args.includes("--version")) {
12
- console.log(pkg.version);
13
- process.exit(0);
14
- }
15
- if (args.includes("-h") || args.includes("--help")) {
16
- console.log(`Usage: papagaio [options] <file>
17
-
18
- Options:
19
- -h, --help Show this help message
20
- -v, --version Show version number`);
21
- process.exit(0);
22
- }
23
-
24
- // File input
25
- const file = args.find(arg => !arg.startsWith("-"));
26
- if (!file) {
27
- console.error("Error: no input file specified.\nUse --help for usage.");
28
- process.exit(1);
29
- }
30
-
31
- const src = fs.readFileSync(file, "utf8");
32
- const p = new Papagaio();
33
- const out = p.process(src);
34
- console.log(out);
package/bin/cli.qjs DELETED
@@ -1,57 +0,0 @@
1
- #!/usr/bin/env qjs
2
- import * as std from "std";
3
- import * as os from "os";
4
-
5
- // Import Papagaio class - ajuste o caminho conforme necessário
6
- // Para QuickJS, você pode incluir o arquivo diretamente ou usar import
7
- import { Papagaio } from "../src/papagaio.js";
8
-
9
- // Version (você pode hardcoded ou ler de um arquivo JSON se necessário)
10
- const VERSION = "0.6.0";
11
-
12
- // Parse command line arguments
13
- const args = scriptArgs.slice(1); // QuickJS usa scriptArgs ao invés de process.argv
14
-
15
- // Help & Version
16
- if (args.includes("-v") || args.includes("--version")) {
17
- std.out.puts(VERSION + "\n");
18
- std.exit(0);
19
- }
20
-
21
- if (args.includes("-h") || args.includes("--help")) {
22
- std.out.puts(`Usage: papagaio [options] <file>
23
- Options:
24
- -h, --help Show this help message
25
- -v, --version Show version number
26
- `);
27
- std.exit(0);
28
- }
29
-
30
- // File input
31
- const file = args.find(arg => !arg.startsWith("-"));
32
- if (!file) {
33
- std.err.puts("Error: no input file specified.\nUse --help for usage.\n");
34
- std.exit(1);
35
- }
36
-
37
- // Read file
38
- let src;
39
- try {
40
- const f = std.open(file, "r");
41
- if (!f) {
42
- std.err.puts(`Error: cannot open file '${file}'\n`);
43
- std.exit(1);
44
- }
45
- src = f.readAsString();
46
- f.close();
47
- } catch (e) {
48
- std.err.puts(`Error reading file: ${e}\n`);
49
- std.exit(1);
50
- }
51
-
52
- // Process with Papagaio
53
- const p = new Papagaio();
54
- const out = p.process(src);
55
-
56
- // Output result
57
- std.out.puts(out);