applay-utils 1.8.19 → 1.8.21

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/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "applay-utils",
3
- "version": "1.8.19",
3
+ "version": "1.8.21",
4
4
  "description": "Utilitary tools for Applay applications",
5
5
  "scripts": {
6
- "postinstall": "node python-setup.js",
7
6
  "build": "npm version patch",
8
7
  "test": "echo \"Error: no test specified\" && exit 1"
9
8
  },
package/python-setup.js CHANGED
@@ -1,4 +1,6 @@
1
1
  const { spawnSync } = require("child_process");
2
+ const fs = require("fs");
3
+ const path = require("path");
2
4
 
3
5
  function findPython() {
4
6
  for (const cmd of ["python3", "python", "py"]) {
@@ -8,34 +10,42 @@ function findPython() {
8
10
  return null;
9
11
  }
10
12
 
11
- function ensurePyMongo(python) {
12
- const check = spawnSync(python, ["-c", "import pymongo"], { encoding: "utf8" });
13
-
14
- if (check.status === 0) {
15
- console.log("✔ PyMongo já instalado");
16
- return;
17
- }
18
-
19
- console.log("📦 Instalando PyMongo...");
20
- const install = spawnSync(python, ["-m", "pip", "install", "pymongo"], { stdio: "inherit" });
21
-
22
- if (install.status !== 0) {
23
- console.error("❌ Falha ao instalar PyMongo.");
13
+ function run(cmd, args, opts = {}) {
14
+ const out = spawnSync(cmd, args, { stdio: "inherit", ...opts });
15
+ if (out.status !== 0) {
16
+ console.error(`❌ Falha ao rodar: ${cmd} ${args.join(" ")}`);
24
17
  process.exit(1);
25
18
  }
26
-
27
- console.log("✔ PyMongo instalado com sucesso");
28
19
  }
29
20
 
30
21
  const python = findPython();
31
-
32
22
  if (!python) {
33
- console.error("❌ Python não encontrado. Instale Python 3.");
23
+ console.error("❌ Nenhum Python encontrado!");
34
24
  process.exit(1);
35
25
  }
36
26
 
37
27
  console.log("✔ Python encontrado em:", python);
38
- ensurePyMongo(python);
39
28
 
40
- // Export to package.json
41
- console.log(`PYTHON=${python}`);
29
+ // Caminho da venv
30
+ const venvPath = path.join(__dirname, ".python-env");
31
+
32
+ // Criar venv se não existir
33
+ if (!fs.existsSync(venvPath)) {
34
+ console.log("📦 Criando ambiente virtual Python...");
35
+ run(python, ["-m", "venv", venvPath]);
36
+ }
37
+
38
+ const pyBin = path.join(venvPath, "bin", "python");
39
+ const pipBin = path.join(venvPath, "bin", "pip");
40
+
41
+ // Verifica PyMongo dentro da venv
42
+ const check = spawnSync(pyBin, ["-c", "import pymongo"]);
43
+ if (check.status !== 0) {
44
+ console.log("📦 Instalando PyMongo dentro da venv...");
45
+ run(pyBin, ["-m", "pip", "install", "pymongo"]);
46
+ } else {
47
+ console.log("✔ PyMongo já instalado na venv.");
48
+ }
49
+
50
+ console.log("✔ Ambiente Python pronto.");
51
+ console.log(`PYTHON=${pyBin}`);
@@ -1,38 +1,37 @@
1
+ const path = require("path");
1
2
  const { spawn } = require("child_process");
2
3
 
3
- function aggregatePython(db, collection, pipe, options = {}) {
4
+ function aggregateAsync(db, collection, pipe, options = {}) {
4
5
  return new Promise((resolve, reject) => {
5
6
 
6
- const python = spawn("python3", ["aggregate.py"]);
7
+ const python = path.join(__dirname, ".python-env/bin/python");
8
+ const script = path.join(__dirname, "aggregate.py");
7
9
 
8
10
  const payload = JSON.stringify({
9
- mongoUrl: db.client.s.url, // usa a URL do próprio MongoClient do Node
11
+ mongoUrl: db.client.s.url,
10
12
  db: db.databaseName,
11
13
  collection,
12
14
  pipeline: pipe,
13
15
  options
14
16
  });
15
17
 
16
- let output = "";
17
- let errorOut = "";
18
-
19
- python.stdout.on("data", data => output += data.toString());
20
- python.stderr.on("data", data => errorOut += data.toString());
21
-
22
- python.on("close", code => {
23
- if (code !== 0) {
24
- return reject(new Error("Python error:\n" + errorOut));
25
- }
26
- try {
27
- resolve(JSON.parse(output));
28
- } catch (e) {
29
- reject(e);
30
- }
18
+ const proc = spawn(python, [script]);
19
+
20
+ let out = "";
21
+ let err = "";
22
+
23
+ proc.stdout.on("data", d => out += d.toString());
24
+ proc.stderr.on("data", d => err += d.toString());
25
+
26
+ proc.on("close", code => {
27
+ if (code !== 0) return reject(new Error(err));
28
+ try { resolve(JSON.parse(out)); }
29
+ catch (e) { reject(e); }
31
30
  });
32
31
 
33
- python.stdin.write(payload);
34
- python.stdin.end();
32
+ proc.stdin.write(payload);
33
+ proc.stdin.end();
35
34
  });
36
35
  }
37
36
 
38
- module.exports = { aggregatePython };
37
+ module.exports = { aggregateAsync };