git-cli-yt 1.1.9 → 2.0.0

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,67 +1,105 @@
1
- #!/usr/bin/env node
2
-
3
- const { execSync, spawn } = require("child_process");
4
- const path = require("path");
5
- const fs = require("fs");
6
- const os = require("os");
7
-
8
- // Carrega o arquivo .env da pasta atual se existir
9
- require("dotenv").config({ path: path.join(process.cwd(), ".env") });
10
-
11
- const packageDir = path.join(__dirname, "..");
12
- const isWindows = process.platform === "win32";
13
-
14
- // Ambiente virtual isolado no Temp do usuário
15
- const venvDir = path.join(os.tmpdir(), "cli-yt-venv");
16
-
17
- const pythonExecutable = isWindows
18
- ? path.join(venvDir, "Scripts", "python.exe")
19
- : path.join(venvDir, "bin", "python");
20
-
21
- const pipExecutable = isWindows
22
- ? path.join(venvDir, "Scripts", "pip.exe")
23
- : path.join(venvDir, "bin", "pip");
24
-
25
- function checkPython() {
26
- try {
27
- const cmd = isWindows ? "where python" : "which python3 || which python";
28
- execSync(cmd, { stdio: "ignore" });
29
- } catch (e) {
30
- console.error("❌ Erro: Python 3 não foi encontrado no sistema.");
31
- process.exit(1);
32
- }
33
- }
34
-
35
- function setupVenv() {
36
- const pythonCmd = isWindows ? "python" : "python3";
37
-
38
- if (!fs.existsSync(pythonExecutable)) {
39
- console.log("⚙️ Criando ambiente virtual Python isolado...");
40
- execSync(`${pythonCmd} -m venv "${venvDir}"`, { stdio: "inherit" });
41
- }
42
-
43
- const reqFile = path.join(packageDir, "requirements.txt");
44
- if (fs.existsSync(reqFile)) {
45
- console.log("📦 Verificando/Instalando dependências...");
46
- execSync(`"${pipExecutable}" install -q -r "${reqFile}"`, { stdio: "inherit" });
47
- }
48
- }
49
-
50
- function runPlayer() {
51
- const mainPy = path.join(packageDir, "main.py");
52
-
53
- // Passa o process.env original sem criar chaves vazias
54
- const child = spawn(pythonExecutable, [mainPy], {
55
- stdio: "inherit",
56
- cwd: process.cwd(),
57
- env: process.env,
58
- });
59
-
60
- child.on("exit", (code) => {
61
- process.exit(code || 0);
62
- });
63
- }
64
-
65
- checkPython();
66
- setupVenv();
67
- runPlayer();
1
+ #!/usr/bin/env node
2
+
3
+ const crypto = require("crypto");
4
+ const fs = require("fs");
5
+ const os = require("os");
6
+ const path = require("path");
7
+ const { execFileSync, spawn } = require("child_process");
8
+
9
+ const packageDir = path.join(__dirname, "..");
10
+ const isWindows = process.platform === "win32";
11
+ const venvDir = path.join(os.tmpdir(), "cli-yt-v2-venv");
12
+ const pythonExecutable = isWindows
13
+ ? path.join(venvDir, "Scripts", "python.exe")
14
+ : path.join(venvDir, "bin", "python");
15
+ const mainPy = path.join(packageDir, "main.py");
16
+ const requirementsFile = path.join(packageDir, "requirements.txt");
17
+
18
+ function findPython() {
19
+ const candidates = isWindows
20
+ ? [
21
+ ["python", []],
22
+ ["py", ["-3"]],
23
+ ]
24
+ : [
25
+ ["python3", []],
26
+ ["python", []],
27
+ ];
28
+ const check = [
29
+ "-c",
30
+ "import sys; raise SystemExit(0 if sys.version_info >= (3, 9) else 1)",
31
+ ];
32
+
33
+ for (const [command, prefix] of candidates) {
34
+ try {
35
+ execFileSync(command, [...prefix, ...check], { stdio: "ignore" });
36
+ return { command, prefix };
37
+ } catch (_) {
38
+ // Próximo candidato.
39
+ }
40
+ }
41
+ throw new Error("Python 3.9 ou superior não foi encontrado.");
42
+ }
43
+
44
+ function setupVenv(python) {
45
+ if (!fs.existsSync(pythonExecutable)) {
46
+ console.log("⚙️ Criando ambiente virtual Python isolado...");
47
+ execFileSync(
48
+ python.command,
49
+ [...python.prefix, "-m", "venv", venvDir],
50
+ { stdio: "inherit", windowsHide: true },
51
+ );
52
+ }
53
+
54
+ if (!fs.existsSync(requirementsFile)) {
55
+ return;
56
+ }
57
+
58
+ const requirements = fs.readFileSync(requirementsFile);
59
+ const digest = crypto.createHash("sha256").update(requirements).digest("hex");
60
+ const stampFile = path.join(venvDir, `.requirements-${digest}`);
61
+ if (fs.existsSync(stampFile)) {
62
+ return;
63
+ }
64
+
65
+ console.log("📦 Instalando dependências atualizadas...");
66
+ execFileSync(
67
+ pythonExecutable,
68
+ [
69
+ "-m",
70
+ "pip",
71
+ "install",
72
+ "--disable-pip-version-check",
73
+ "-r",
74
+ requirementsFile,
75
+ ],
76
+ { stdio: "inherit", windowsHide: true },
77
+ );
78
+ fs.writeFileSync(stampFile, digest, "utf8");
79
+ }
80
+
81
+ function runPlayer() {
82
+ const child = spawn(pythonExecutable, [mainPy], {
83
+ stdio: "inherit",
84
+ cwd: process.cwd(),
85
+ env: process.env,
86
+ windowsHide: true,
87
+ });
88
+
89
+ child.on("error", (error) => {
90
+ console.error(`❌ Não foi possível iniciar o player: ${error.message}`);
91
+ process.exit(1);
92
+ });
93
+ child.on("exit", (code, signal) => {
94
+ process.exit(code === null ? (signal ? 1 : 0) : code);
95
+ });
96
+ }
97
+
98
+ try {
99
+ const python = findPython();
100
+ setupVenv(python);
101
+ runPlayer();
102
+ } catch (error) {
103
+ console.error(`❌ Erro: ${error.message}`);
104
+ process.exit(1);
105
+ }