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 +1 -2
- package/python-setup.js +30 -20
- package/utils/pythonFuncs.js +20 -21
package/package.json
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applay-utils",
|
|
3
|
-
"version": "1.8.
|
|
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
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
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
|
|
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
|
-
//
|
|
41
|
-
|
|
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}`);
|
package/utils/pythonFuncs.js
CHANGED
|
@@ -1,38 +1,37 @@
|
|
|
1
|
+
const path = require("path");
|
|
1
2
|
const { spawn } = require("child_process");
|
|
2
3
|
|
|
3
|
-
function
|
|
4
|
+
function aggregateAsync(db, collection, pipe, options = {}) {
|
|
4
5
|
return new Promise((resolve, reject) => {
|
|
5
6
|
|
|
6
|
-
const python =
|
|
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,
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
|
|
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
|
-
|
|
34
|
-
|
|
32
|
+
proc.stdin.write(payload);
|
|
33
|
+
proc.stdin.end();
|
|
35
34
|
});
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
module.exports = {
|
|
37
|
+
module.exports = { aggregateAsync };
|