donbot-cli 1.0.1
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/cli/access.js +108 -0
- package/cli/deploy.js +153 -0
- package/cli/filer.js +43 -0
- package/cli/flows.js +187 -0
- package/cli/routes.js +47 -0
- package/donbot.js +96 -0
- package/package.json +23 -0
package/cli/access.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
require('dotenv').config();
|
|
3
|
+
var jwt = require('jsonwebtoken');
|
|
4
|
+
const bcrypt = require("bcryptjs");
|
|
5
|
+
var MongoClient = require('mongodb').MongoClient;
|
|
6
|
+
|
|
7
|
+
const registry = async (mail, first_name, last_name, password) => {
|
|
8
|
+
|
|
9
|
+
console.log(`
|
|
10
|
+
_____ _______ __ _ ______ _______ _______
|
|
11
|
+
| | | || | | || _ | | || |
|
|
12
|
+
| _ | | _ || || || | | | | _ ||_ _|
|
|
13
|
+
| | | || | | || || | | | | | | |
|
|
14
|
+
| |_| | | |_| || _ || _ | | |_| | | |
|
|
15
|
+
| | | || | | || |_| | | | | |
|
|
16
|
+
|____| |_______||_| |___||_____| |_______| |___|
|
|
17
|
+
|
|
18
|
+
DonBot CLI - Herramienta para operaciones automatizadas.
|
|
19
|
+
`);
|
|
20
|
+
|
|
21
|
+
if (!mail || !first_name || !last_name || !password) {
|
|
22
|
+
console.error('Verifique que los siguientes valores estén definidos: mail, first_name, last_name, password.');
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
MongoClient.connect('mongodb://'+process.env.DB_USER+':'+process.env.DB_PASS+'@'+process.env.DB_HOST+':'+process.env.DB_PORT+'/'+process.env.DB_NAME, async function(err, db) {
|
|
27
|
+
if (err) throw err;
|
|
28
|
+
var dbo = db.db(process.env.DB_NAME);
|
|
29
|
+
|
|
30
|
+
//Obtenemos el salt
|
|
31
|
+
let salt = await bcrypt.genSalt(10).then();
|
|
32
|
+
let hash = await bcrypt.hash(password, salt).then();
|
|
33
|
+
|
|
34
|
+
const users = await dbo.collection("users").find({ mail: mail }).toArray();
|
|
35
|
+
if (!users) {
|
|
36
|
+
|
|
37
|
+
var user_new = {
|
|
38
|
+
first_name: first_name,
|
|
39
|
+
last_name: last_name,
|
|
40
|
+
mail: mail,
|
|
41
|
+
password: hash
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
await dbo.collection("users").insertOne(user_new);
|
|
45
|
+
console.log("Usuario creado.");
|
|
46
|
+
process.exit();
|
|
47
|
+
|
|
48
|
+
}else {
|
|
49
|
+
|
|
50
|
+
console.error("El usuario ya esta registrado.");
|
|
51
|
+
process.exit();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const createKey = (user, description, expiration) => {
|
|
59
|
+
|
|
60
|
+
MongoClient.connect('mongodb://'+process.env.DB_USER+':'+process.env.DB_PASS+'@'+process.env.DB_HOST+':'+process.env.DB_PORT+'/'+process.env.DB_NAME, async function(err, db) {
|
|
61
|
+
if (err) throw err;
|
|
62
|
+
var dbo = db.db("donbot");
|
|
63
|
+
|
|
64
|
+
const genToken = () => {
|
|
65
|
+
return [...Array(128)]
|
|
66
|
+
.map((e) => ((Math.random() * 36) | 0).toString(36))
|
|
67
|
+
.join('');
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
let today = new Date().toISOString();
|
|
71
|
+
|
|
72
|
+
const payload = {
|
|
73
|
+
user: user,
|
|
74
|
+
creationDate: today,
|
|
75
|
+
expiresIn: expiration
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
let expiresobj;
|
|
79
|
+
let expiresday;
|
|
80
|
+
if(expiration == "noexpire"){
|
|
81
|
+
expiresobj = "";
|
|
82
|
+
expiresday = expiration;
|
|
83
|
+
}else{
|
|
84
|
+
expiresobj = {
|
|
85
|
+
expiresIn: expiration
|
|
86
|
+
};
|
|
87
|
+
expiresday = expiration;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const access_token = jwt.sign(payload, process.env.KEY, expiresobj);
|
|
91
|
+
|
|
92
|
+
//Generamos la llave y la almacenamos la mostramos al usuario
|
|
93
|
+
var key = {
|
|
94
|
+
user: user,
|
|
95
|
+
description: description,
|
|
96
|
+
key: access_token,
|
|
97
|
+
creationDate: today,
|
|
98
|
+
expirationDate: expiresday,
|
|
99
|
+
active: true
|
|
100
|
+
};
|
|
101
|
+
await dbo.collection("accesstokens").insertOne(key);
|
|
102
|
+
process.exit();
|
|
103
|
+
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
module.exports = { createKey, registry };
|
package/cli/deploy.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
const { Client } = require("ssh2");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { exec } = require("child_process");
|
|
4
|
+
const readline = require('readline');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
let config;
|
|
7
|
+
|
|
8
|
+
function getGitBranch() {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
exec('git rev-parse --abbrev-ref HEAD', (error, stdout, stderr) => {
|
|
11
|
+
if (error) {
|
|
12
|
+
reject(`Error: ${stderr}`);
|
|
13
|
+
} else {
|
|
14
|
+
resolve(stdout.trim()); // La salida de git es la rama actual
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const askPassphrase = () => {
|
|
21
|
+
return new Promise((resolve) => {
|
|
22
|
+
const rl = readline.createInterface({
|
|
23
|
+
input: process.stdin,
|
|
24
|
+
output: process.stdout,
|
|
25
|
+
terminal: true,
|
|
26
|
+
});
|
|
27
|
+
rl.question('Introduce la passphrase para la clave SSH: ', (passphrase) => {
|
|
28
|
+
rl.close();
|
|
29
|
+
resolve(passphrase);
|
|
30
|
+
});
|
|
31
|
+
rl._writeToOutput = () => {};
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const uploadCodeToServer = async (uploadCodeToServer) => {
|
|
36
|
+
|
|
37
|
+
console.log(`
|
|
38
|
+
_____ _______ __ _ ______ _______ _______
|
|
39
|
+
| | | || | | || _ | | || |
|
|
40
|
+
| _ | | _ || || || | | | | _ ||_ _|
|
|
41
|
+
| | | || | | || || | | | | | | |
|
|
42
|
+
| |_| | | |_| || _ || _ | | |_| | | |
|
|
43
|
+
| | | || | | || |_| | | | | |
|
|
44
|
+
|____| |_______||_| |___||_____| |_______| |___|
|
|
45
|
+
|
|
46
|
+
DonBot CLI - Herramienta para operaciones automatizadas.
|
|
47
|
+
`);
|
|
48
|
+
|
|
49
|
+
let host, user, path_cert, port, path_dest, sourceDir;
|
|
50
|
+
|
|
51
|
+
getGitBranch()
|
|
52
|
+
.then(async (branch) => {
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
config = JSON.parse(fs.readFileSync('config-donbot-cli.json', 'utf8'));
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.error('Error al leer el archivo de configuración:', error.message);
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if(branch === uploadCodeToServer){
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
({ host, user, path_cert, port, path_dest, sourceDir } = config[uploadCodeToServer]);
|
|
65
|
+
|
|
66
|
+
console.log("Actualizando codigo en: " + host)
|
|
67
|
+
|
|
68
|
+
if (!host || !user || !path_cert || !port || !path_dest || !sourceDir) {
|
|
69
|
+
console.error('Faltan valores necesarios en el archivo de configuración.');
|
|
70
|
+
console.error('Verifique que los siguientes valores estén definidos: host, user, path_cert, port, path_dest, sourceDir.');
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
if (!fs.existsSync(path_cert)) {
|
|
76
|
+
console.error(`La clave privada especificada no existe en la ruta: ${path_cert}`);
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.error('Error al verificar la ruta de la clave privada:', error.message);
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let rsyncCommand;
|
|
85
|
+
|
|
86
|
+
let connectionConfig = {
|
|
87
|
+
host,
|
|
88
|
+
port,
|
|
89
|
+
username: user,
|
|
90
|
+
privateKey: fs.readFileSync(path_cert)
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const ssh = new Client();
|
|
94
|
+
|
|
95
|
+
ssh.on("ready", () => {
|
|
96
|
+
console.log("Conectado al servidor");
|
|
97
|
+
|
|
98
|
+
rsyncCommand = `rsync -avzh --progress -e "ssh -i ${path_cert}" --exclude-from="exclude-deploy-donbot-list.txt" "${sourceDir}" ${user}@${host}:${path_dest}`;
|
|
99
|
+
|
|
100
|
+
exec(rsyncCommand, (err, stdout, stderr) => {
|
|
101
|
+
if (err) {
|
|
102
|
+
console.error(`Error al subir los archivos: ${stderr}`);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
console.log("Archivos subidos correctamente:");
|
|
106
|
+
console.log(stdout);
|
|
107
|
+
|
|
108
|
+
// Reiniciar Docker Compose después de la transferencia
|
|
109
|
+
const dockerCommand = `ssh -i ${path_cert} ${user}@${host} "cd && docker compose restart"`;
|
|
110
|
+
exec(dockerCommand, (dockerErr, dockerStdout, dockerStderr) => {
|
|
111
|
+
if (dockerErr) {
|
|
112
|
+
console.error(`Error al reiniciar Docker Compose: ${dockerStderr}`);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
console.log("Docker Compose reiniciado correctamente:");
|
|
116
|
+
console.log(dockerStdout);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
ssh.end();
|
|
122
|
+
})
|
|
123
|
+
.on("error", (err) => {
|
|
124
|
+
console.error("Error de conexión SSH:", err);
|
|
125
|
+
})
|
|
126
|
+
.on('close', () => {
|
|
127
|
+
})
|
|
128
|
+
.on('end', () => {
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
ssh.connect(connectionConfig);
|
|
133
|
+
} catch (err) {
|
|
134
|
+
if (err.message.includes("Encrypted OpenSSH private key detected")) {
|
|
135
|
+
const passphrase = await askPassphrase();
|
|
136
|
+
connectionConfig.passphrase = passphrase;
|
|
137
|
+
ssh.connect(connectionConfig);
|
|
138
|
+
}else{
|
|
139
|
+
console.error("Error inesperado:", err.message);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
}else{
|
|
144
|
+
console.log("No estas en la rama correcta para deploy.")
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
})
|
|
148
|
+
.catch((err) => {
|
|
149
|
+
console.error(err);
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
module.exports = { uploadCodeToServer }
|
package/cli/filer.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
require('dotenv').config();
|
|
3
|
+
const fs = require('fs')
|
|
4
|
+
const readline = require('readline');
|
|
5
|
+
var MongoClient = require('mongodb').MongoClient;
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const readfile = (path) => {
|
|
9
|
+
|
|
10
|
+
//MongoClient.connect('mongodb://'+process.env.DB_USER+':'+process.env.DB_PASS+'@'+process.env.DB_HOST+':'+process.env.DB_PORT+'/'+process.env.DB_NAME, function(err, db) {
|
|
11
|
+
//if (err) throw err;
|
|
12
|
+
//var dbo = db.db("donbot");
|
|
13
|
+
|
|
14
|
+
//Leemos el archivo aprocesar
|
|
15
|
+
const readInterface = readline.createInterface({
|
|
16
|
+
input: fs.createReadStream(path),
|
|
17
|
+
output: process.stdout,
|
|
18
|
+
console: false
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
readInterface.on('line', function(line) {
|
|
22
|
+
var linearray = line.split("|");
|
|
23
|
+
|
|
24
|
+
if(linearray.length != 60){
|
|
25
|
+
process.exit();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/*var myobj = {
|
|
29
|
+
brand_no: linearray[0],
|
|
30
|
+
brand_name: linearray[1],
|
|
31
|
+
private_label: linearray[2],
|
|
32
|
+
deleted_flag: linearray[3]
|
|
33
|
+
};
|
|
34
|
+
dbo.collection("brands").insertOne(myobj);*/
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
//});
|
|
40
|
+
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
module.exports = { readfile };
|
package/cli/flows.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const readline = require('readline');
|
|
5
|
+
|
|
6
|
+
const addRouteToRoutesFile = (routeName) => {
|
|
7
|
+
|
|
8
|
+
const routesFilePath = path.join(__dirname, "../routes/routes.js");
|
|
9
|
+
const routeFilePath = `./${routeName}.routes`;
|
|
10
|
+
|
|
11
|
+
if (!fs.existsSync(routesFilePath)) {
|
|
12
|
+
// Si no existe routes.js, lo creamos
|
|
13
|
+
const initialContent = `"use strict";
|
|
14
|
+
|
|
15
|
+
// Importa todas las rutas aquí
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
module.exports = (app) => {
|
|
19
|
+
// Exporta las rutas para utilizarlas en app.js
|
|
20
|
+
};`;
|
|
21
|
+
fs.writeFileSync(routesFilePath, initialContent);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Leer contenido existente de routes.js
|
|
25
|
+
const existingContent = fs.readFileSync(routesFilePath, "utf-8");
|
|
26
|
+
|
|
27
|
+
// Verificar si ya está registrado el archivo
|
|
28
|
+
if (existingContent.includes(routeFilePath)) {
|
|
29
|
+
console.log(`La ruta ${routeName} ya está registrada en routes.js`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Agregar la nueva ruta al archivo
|
|
34
|
+
const updatedContent = existingContent.replace(
|
|
35
|
+
"// Importa todas las rutas aquí",
|
|
36
|
+
`// Importa todas las rutas aquí\nconst ${routeName}Routes = require("${routeFilePath}");`
|
|
37
|
+
).replace(
|
|
38
|
+
"// Exporta las rutas para utilizarlas en app.js",
|
|
39
|
+
`// Exporta las rutas para utilizarlas en app.js\n app.use("", ${routeName}Routes);`
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
fs.writeFileSync(routesFilePath, updatedContent);
|
|
43
|
+
console.log(`Ruta ${routeName} registrada correctamente en routes.js`);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const askRuteendpoint = () => {
|
|
47
|
+
return new Promise((resolve) => {
|
|
48
|
+
const rl = readline.createInterface({
|
|
49
|
+
input: process.stdin,
|
|
50
|
+
output: process.stdout,
|
|
51
|
+
terminal: false,
|
|
52
|
+
});
|
|
53
|
+
const askForRoute = () => {
|
|
54
|
+
rl.question('Introduce la Ruta para el endpoint "/example": ', (Routeendpoint) => {
|
|
55
|
+
// Validar que la ruta comience con "/"
|
|
56
|
+
if (Routeendpoint && Routeendpoint[0] !== '/') {
|
|
57
|
+
console.error('La ruta debe comenzar con un "/".');
|
|
58
|
+
askForRoute(); // Volver a pedir la ruta sin cerrar la interfaz
|
|
59
|
+
} else {
|
|
60
|
+
rl.close(); // Solo cerramos la interfaz cuando la ruta es válida
|
|
61
|
+
resolve(Routeendpoint);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
askForRoute();
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const newflow = async (name,options) => {
|
|
71
|
+
|
|
72
|
+
console.log(`
|
|
73
|
+
_____ _______ __ _ ______ _______ _______
|
|
74
|
+
| | | || | | || _ | | || |
|
|
75
|
+
| _ | | _ || || || | | | | _ ||_ _|
|
|
76
|
+
| | | || | | || || | | | | | | |
|
|
77
|
+
| |_| | | |_| || _ || _ | | |_| | | |
|
|
78
|
+
| | | || | | || |_| | | | | |
|
|
79
|
+
|____| |_______||_| |___||_____| |_______| |___|
|
|
80
|
+
|
|
81
|
+
DonBot CLI - Herramienta para operaciones automatizadas.
|
|
82
|
+
`);
|
|
83
|
+
|
|
84
|
+
const controllersDir = path.join(__dirname, "../", "controllers");
|
|
85
|
+
const routesDir = path.join(__dirname, "../", "routes");
|
|
86
|
+
|
|
87
|
+
// Crear el controlador
|
|
88
|
+
const controllerFileName = `${name}.controller.js`;
|
|
89
|
+
const controllerPath = path.join(controllersDir, name);
|
|
90
|
+
const controllerFilePath = path.join(controllerPath, controllerFileName);
|
|
91
|
+
|
|
92
|
+
if (!fs.existsSync(controllersDir)) {
|
|
93
|
+
console.error("El directorio 'controllers' no existe.");
|
|
94
|
+
console.log("Creando el directorio 'controllers'...");
|
|
95
|
+
fs.mkdirSync(controllersDir, { recursive: true });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!fs.existsSync(controllerPath)) {
|
|
99
|
+
console.error("El directorio '" + name + "' no existe.");
|
|
100
|
+
console.log("Creando el directorio '" + name + "'...");
|
|
101
|
+
fs.mkdirSync(controllerPath, { recursive: true });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (fs.existsSync(controllerFilePath)) {
|
|
105
|
+
console.error(`El controlador '${controllerFileName}' ya existe.`);
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
let controllerContent;
|
|
111
|
+
|
|
112
|
+
if (options.endpoint) {
|
|
113
|
+
controllerContent = `
|
|
114
|
+
"use strict";
|
|
115
|
+
|
|
116
|
+
class ${name.charAt(0).toUpperCase() + name.slice(1)}Controller {
|
|
117
|
+
static async exampleAction(req, res) {
|
|
118
|
+
try {
|
|
119
|
+
res.status(200).json({ message: "Acción ejecutada correctamente" });
|
|
120
|
+
} catch (error) {
|
|
121
|
+
res.status(500).json({ error: error.message });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
module.exports = ${name.charAt(0).toUpperCase() + name.slice(1)}Controller;
|
|
127
|
+
`;
|
|
128
|
+
|
|
129
|
+
if (!fs.existsSync(routesDir)) {
|
|
130
|
+
console.error("El directorio 'routes' no existe.");
|
|
131
|
+
console.log("Creando el directorio 'routes'...");
|
|
132
|
+
fs.mkdirSync(routesDir, { recursive: true });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
const routeFileName = `${name}.routes.js`;
|
|
137
|
+
const routeFilePath = path.join(routesDir, routeFileName);
|
|
138
|
+
|
|
139
|
+
if (fs.existsSync(routeFilePath)) {
|
|
140
|
+
console.error(`El archivo de rutas '${routeFileName}' ya existe.`);
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const Routeendpoint = await askRuteendpoint();
|
|
145
|
+
|
|
146
|
+
const routeContent = `
|
|
147
|
+
"use strict";
|
|
148
|
+
const express = require("express");
|
|
149
|
+
const ${name.charAt(0).toUpperCase() + name.slice(1)}Controller = require("../controllers/${name}/${name}.controller");
|
|
150
|
+
|
|
151
|
+
const router = express.Router();
|
|
152
|
+
|
|
153
|
+
router.get("${Routeendpoint}", ${name.charAt(0).toUpperCase() + name.slice(1)}Controller.exampleAction);
|
|
154
|
+
|
|
155
|
+
module.exports = router;
|
|
156
|
+
`;
|
|
157
|
+
fs.writeFileSync(routeFilePath, routeContent.trim());
|
|
158
|
+
console.log(`Archivo de rutas '${routeFileName}' creado exitosamente.`);
|
|
159
|
+
|
|
160
|
+
addRouteToRoutesFile(name);
|
|
161
|
+
|
|
162
|
+
}else {
|
|
163
|
+
|
|
164
|
+
controllerContent = `
|
|
165
|
+
"use strict";
|
|
166
|
+
|
|
167
|
+
class ${name.charAt(0).toUpperCase() + name.slice(1)}Controller {
|
|
168
|
+
static async exampleAction() {
|
|
169
|
+
try {
|
|
170
|
+
|
|
171
|
+
} catch (error) {
|
|
172
|
+
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
module.exports = ${name.charAt(0).toUpperCase() + name.slice(1)}Controller;
|
|
178
|
+
`;
|
|
179
|
+
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
fs.writeFileSync(controllerFilePath, controllerContent.trim());
|
|
183
|
+
console.log(`Controlador '${controllerFileName}' creado exitosamente.`);
|
|
184
|
+
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
module.exports = { newflow }
|
package/cli/routes.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
|
|
2
|
+
const listroutes = async () => {
|
|
3
|
+
|
|
4
|
+
console.log(`
|
|
5
|
+
_____ _______ __ _ ______ _______ _______
|
|
6
|
+
| | | || | | || _ | | || |
|
|
7
|
+
| _ | | _ || || || | | | | _ ||_ _|
|
|
8
|
+
| | | || | | || || | | | | | | |
|
|
9
|
+
| |_| | | |_| || _ || _ | | |_| | | |
|
|
10
|
+
| | | || | | || |_| | | | | |
|
|
11
|
+
|____| |_______||_| |___||_____| |_______| |___|
|
|
12
|
+
|
|
13
|
+
DonBot CLI - Herramienta para operaciones automatizadas.
|
|
14
|
+
`);
|
|
15
|
+
|
|
16
|
+
const router = require("../routes/api");
|
|
17
|
+
const routes = [];
|
|
18
|
+
router.stack.forEach((layer) => {
|
|
19
|
+
if (layer.route) {
|
|
20
|
+
// Extrae el método y la ruta
|
|
21
|
+
const methods = Object.keys(layer.route.methods)
|
|
22
|
+
.map((method) => method.toUpperCase())
|
|
23
|
+
.join(", ");
|
|
24
|
+
routes.push({
|
|
25
|
+
path: layer.route.path,
|
|
26
|
+
methods,
|
|
27
|
+
});
|
|
28
|
+
} else if (layer.name === "router") {
|
|
29
|
+
// Si es otro router, recursivamente analiza sus rutas
|
|
30
|
+
layer.handle.stack.forEach((nestedLayer) => {
|
|
31
|
+
if (nestedLayer.route) {
|
|
32
|
+
const methods = Object.keys(nestedLayer.route.methods)
|
|
33
|
+
.map((method) => method.toUpperCase())
|
|
34
|
+
.join(", ");
|
|
35
|
+
routes.push({
|
|
36
|
+
path: nestedLayer.route.path,
|
|
37
|
+
methods,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
console.table(routes);
|
|
44
|
+
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
module.exports = { listroutes }
|
package/donbot.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
const { Command } = require("commander");
|
|
4
|
+
const program = new Command();
|
|
5
|
+
|
|
6
|
+
//const filer = require('./cli/filer');
|
|
7
|
+
const access = require('./cli/access');
|
|
8
|
+
const deploy = require('./cli/deploy');
|
|
9
|
+
const routes = require('./cli/routes');
|
|
10
|
+
const flows = require('./cli/flows');
|
|
11
|
+
|
|
12
|
+
//Version
|
|
13
|
+
program
|
|
14
|
+
.version("DonBot 1.7.0")
|
|
15
|
+
.description( `
|
|
16
|
+
_____ _______ __ _ ______ _______ _______
|
|
17
|
+
| | | || | | || _ | | || |
|
|
18
|
+
| _ | | _ || || || | | | | _ ||_ _|
|
|
19
|
+
| | | || | | || || | | | | | | |
|
|
20
|
+
| |_| | | |_| || _ || _ | | |_| | | |
|
|
21
|
+
| | | || | | || |_| | | | | |
|
|
22
|
+
|____| |_______||_| |___||_____| |_______| |___|
|
|
23
|
+
|
|
24
|
+
DonBot CLI - Herramienta para operaciones automatizadas.
|
|
25
|
+
`);
|
|
26
|
+
|
|
27
|
+
//Creamos usuarios
|
|
28
|
+
program.
|
|
29
|
+
command("createuser <mail> <first_name> <last_name> <password>").
|
|
30
|
+
alias("cu").
|
|
31
|
+
description("Creamos un usuario en la base de datos").
|
|
32
|
+
action((mail, first_name, last_name, password) => {
|
|
33
|
+
access.registry(mail, first_name, last_name, password);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
//Create app Token
|
|
37
|
+
program.
|
|
38
|
+
command("access <user> <description> <expiration>").
|
|
39
|
+
alias("ac").
|
|
40
|
+
description("Crea un App Key, para una key sin expiracion enviar el valor noexpire en lugar del numero de dias u horas (2 days, 10h, 7d)").
|
|
41
|
+
action((user, description, expiration) => {
|
|
42
|
+
access.createKey(user, description, expiration);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
//Lectura de archivo
|
|
47
|
+
/*program.
|
|
48
|
+
command("readfile <path>").
|
|
49
|
+
alias("rf").
|
|
50
|
+
description("Leer el archivo").
|
|
51
|
+
action((path) => {
|
|
52
|
+
filer.readfile(path);
|
|
53
|
+
});*/
|
|
54
|
+
|
|
55
|
+
//Deploy
|
|
56
|
+
program.
|
|
57
|
+
command("deploy <type_deploy>").
|
|
58
|
+
alias("dp").
|
|
59
|
+
description(`Ejecuta un deploy por SSH a un servidor con el entorno preparado para donbot.
|
|
60
|
+
|
|
61
|
+
Valores para config-donbot-cli.json
|
|
62
|
+
|
|
63
|
+
<host> -- Host de servidor
|
|
64
|
+
<user> -- User de servidor
|
|
65
|
+
<port> -- Puerto de acceso
|
|
66
|
+
<path_cert> -- Path de ubicacion del cetificado para autenticación
|
|
67
|
+
<path_dest> -- Path de ubicacion en servidor destino
|
|
68
|
+
|
|
69
|
+
`).
|
|
70
|
+
action((type_deploy) => {
|
|
71
|
+
deploy.uploadCodeToServer(type_deploy);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Flow
|
|
75
|
+
|
|
76
|
+
program.
|
|
77
|
+
command("newcontroller <name>").
|
|
78
|
+
alias("nc").
|
|
79
|
+
description("Crea un nuevo flujo").
|
|
80
|
+
option("--cron", "Genera también un archivo de rutas para el controlador").
|
|
81
|
+
option("--endpoint", "Genera también un archivo de rutas para el controlador").
|
|
82
|
+
action((name,options) => {
|
|
83
|
+
flows.newflow(name,options);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Routes
|
|
87
|
+
|
|
88
|
+
program.
|
|
89
|
+
command("listroutes").
|
|
90
|
+
alias("lr").
|
|
91
|
+
description("Lista las rutas creadas").
|
|
92
|
+
action(() => {
|
|
93
|
+
routes.listroutes();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
program.parse(process.argv);
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "donbot-cli",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Cli de donbot",
|
|
5
|
+
"main": "donbot.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"donbot": "donbot.js"
|
|
8
|
+
},
|
|
9
|
+
"author": "Oscar Aparicio",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"bcryptjs": "^2.4.3",
|
|
13
|
+
"child_process": "^1.0.2",
|
|
14
|
+
"commander": "^13.1.0",
|
|
15
|
+
"dotenv": "^16.4.7",
|
|
16
|
+
"fs": "^0.0.1-security",
|
|
17
|
+
"jsonwebtoken": "^9.0.2",
|
|
18
|
+
"mongodb": "^6.13.0",
|
|
19
|
+
"path": "^0.12.7",
|
|
20
|
+
"readline": "^1.3.0",
|
|
21
|
+
"ssh2": "^1.16.0"
|
|
22
|
+
}
|
|
23
|
+
}
|