papagaio 0.7.7 → 0.7.8
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 +25 -96
- package/package.json +1 -1
package/bin/cli.mjs
CHANGED
|
@@ -1,62 +1,22 @@
|
|
|
1
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
2
|
|
|
6
|
-
|
|
7
|
-
|
|
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("../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);
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import Papagaio from "../papagaio.js";
|
|
32
5
|
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
};
|
|
6
|
+
async function main() {
|
|
7
|
+
const args = process.argv.slice(2);
|
|
45
8
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const args = isQuickJS ? scriptArgs.slice(1) : process.argv.slice(2);
|
|
50
|
-
const VERSION = isQuickJS ? "0.6.0" : pkg.version;
|
|
9
|
+
const pkgPath = new URL("../package.json", import.meta.url);
|
|
10
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
11
|
+
const VERSION = pkg.version;
|
|
51
12
|
|
|
52
|
-
// Help & Version
|
|
53
13
|
if (args.includes("-v") || args.includes("--version")) {
|
|
54
|
-
|
|
55
|
-
|
|
14
|
+
console.log(VERSION);
|
|
15
|
+
process.exit(0);
|
|
56
16
|
}
|
|
57
17
|
|
|
58
18
|
if (args.includes("-h") || args.includes("--help")) {
|
|
59
|
-
|
|
19
|
+
console.log(`Usage: papagaio [options] <file1> [file2] [...]
|
|
60
20
|
|
|
61
21
|
Options:
|
|
62
22
|
-h, --help Show this help message
|
|
@@ -66,72 +26,41 @@ Examples:
|
|
|
66
26
|
papagaio input.txt
|
|
67
27
|
papagaio file1.txt file2.txt file3.txt
|
|
68
28
|
papagaio *.txt`);
|
|
69
|
-
|
|
29
|
+
process.exit(0);
|
|
70
30
|
}
|
|
71
31
|
|
|
72
|
-
|
|
73
|
-
const files = args.filter((arg, i) => {
|
|
74
|
-
if (arg.startsWith("-")) return false;
|
|
75
|
-
return true;
|
|
76
|
-
});
|
|
32
|
+
const files = args.filter(arg => !arg.startsWith("-"));
|
|
77
33
|
|
|
78
34
|
if (files.length === 0) {
|
|
79
|
-
|
|
80
|
-
|
|
35
|
+
console.error("Error: no input file specified.\nUse --help for usage.");
|
|
36
|
+
process.exit(1);
|
|
81
37
|
}
|
|
82
38
|
|
|
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
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// ============================================================================
|
|
104
|
-
// READ AND CONCATENATE FILES
|
|
105
|
-
// ============================================================================
|
|
106
39
|
let concatenatedSrc = "";
|
|
107
40
|
let hasErrors = false;
|
|
108
41
|
|
|
109
42
|
for (const file of files) {
|
|
110
43
|
try {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
44
|
+
if (!fs.existsSync(file)) {
|
|
45
|
+
throw new Error(`file not found: ${file}`);
|
|
46
|
+
}
|
|
47
|
+
concatenatedSrc += fs.readFileSync(file, "utf8");
|
|
48
|
+
} catch (err) {
|
|
49
|
+
console.error(`Error reading ${file}: ${err.message || err}`);
|
|
115
50
|
hasErrors = true;
|
|
116
51
|
}
|
|
117
52
|
}
|
|
118
53
|
|
|
119
54
|
if (hasErrors) {
|
|
120
|
-
|
|
55
|
+
process.exit(1);
|
|
121
56
|
}
|
|
122
57
|
|
|
123
|
-
// PROCESS CONCATENATED INPUT
|
|
124
58
|
const p = new Papagaio();
|
|
125
59
|
const out = p.process(concatenatedSrc);
|
|
126
|
-
|
|
60
|
+
console.log(out);
|
|
127
61
|
}
|
|
128
62
|
|
|
129
|
-
// main
|
|
130
63
|
main().catch(err => {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
output("Fatal error: " + (err.message || err));
|
|
135
|
-
const exit = isQuickJS ? std.exit : process.exit;
|
|
136
|
-
exit(1);
|
|
137
|
-
});
|
|
64
|
+
console.error("Fatal error:", err.message || err);
|
|
65
|
+
process.exit(1);
|
|
66
|
+
});
|