mirror of
https://github.com/EvolutionAPI/evolution-manager.git
synced 2025-12-20 03:52:19 -06:00
add path api to CLI
This commit is contained in:
16
lib/api/index.js
Normal file
16
lib/api/index.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const functions = {
|
||||
setup: require('./setup.js'),
|
||||
}
|
||||
|
||||
module.exports = async (argv) => {
|
||||
try {
|
||||
if (argv._.length === 1) throw new Error('No operation specified')
|
||||
const operation = argv._[1]
|
||||
if (!functions[operation]) throw new Error(`Unknown operation: ${operation}`)
|
||||
|
||||
await functions[operation](argv)
|
||||
} catch (e) {
|
||||
console.error(e.message || e)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
65
lib/api/setup.js
Normal file
65
lib/api/setup.js
Normal file
@@ -0,0 +1,65 @@
|
||||
const build = require('../utils/build.js');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = async () => {
|
||||
const isEvolutionInstalled = verifyEvolutionInstallation();
|
||||
if (!isEvolutionInstalled) return;
|
||||
console.log('👍 Evolution-Api installation found');
|
||||
|
||||
await build({ VITE_BASE_URL: '/manager/' });
|
||||
|
||||
// copy dist folder to evolution-api/Extras/evolution-manager
|
||||
console.time('📦 Copy dist folder to evolution-api/Extras/evolution-manager');
|
||||
|
||||
const distFolder = path.join(__dirname, '..', '..', 'dist');
|
||||
const extrasFolder = path.join(process.cwd(), 'Extras');
|
||||
const evolutionManagerFolder = path.join(extrasFolder, 'evolution-manager');
|
||||
|
||||
if (!fs.existsSync(evolutionManagerFolder)) fs.mkdirSync(evolutionManagerFolder);
|
||||
fs.copySync(distFolder, evolutionManagerFolder);
|
||||
|
||||
console.timeEnd('📦 Copy dist folder to evolution-api/Extras/evolution-manager');
|
||||
|
||||
|
||||
// Apply diff git patch
|
||||
console.time('↘️ Apply diff git patch');
|
||||
const patchPath = path.join(__dirname, './view.router.ts.patch');
|
||||
const apiFile = path.join(process.cwd(), 'src', 'whatsapp', 'routers', 'view.router.ts');
|
||||
// copy/replace file with patch
|
||||
fs.copySync(patchPath, apiFile);
|
||||
console.timeEnd('↘️ Apply diff git patch');
|
||||
|
||||
|
||||
console.log('\n 🎉 Evolution-Api Manager installed successfully! 🎉');
|
||||
console.log('👉 Restart your Evolution-Api server to apply changes 👈')
|
||||
};
|
||||
|
||||
function verifyEvolutionInstallation(version = "1.5.0") {
|
||||
// load package.json current context
|
||||
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
||||
if (!fs.existsSync(packageJsonPath)) {
|
||||
console.error("🚨 package.json not found. Certify you are in the root of the Evolution-Api installation")
|
||||
return false
|
||||
}
|
||||
var packageJson = fs.readFileSync(packageJsonPath, 'utf8');
|
||||
packageJson = JSON.parse(packageJson);
|
||||
|
||||
|
||||
// check if evolution is installed
|
||||
if (packageJson.name !== "evolution-api") {
|
||||
console.error("🚨 This is not a Evolution-API installation. Certify you are in the root of the Evolution-Api installation")
|
||||
return false
|
||||
}
|
||||
|
||||
// verify if version is same or higher
|
||||
if (version) {
|
||||
const semver = require('semver');
|
||||
if (!semver.gte(packageJson.version, version)) {
|
||||
console.error(`🚨 Evolution-Api version ${version} or higher is required. Please update your Evolution-Api installation`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
39
lib/api/view.router.ts.patch
Normal file
39
lib/api/view.router.ts.patch
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Router } from 'express';
|
||||
import fs from 'fs';
|
||||
import mime from 'mime-types';
|
||||
import path from 'path';
|
||||
|
||||
import { RouterBroker } from '../abstract/abstract.router';
|
||||
|
||||
export class ViewsRouter extends RouterBroker {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
const index = fs.readFileSync(path.join(__dirname, '../../../', 'Extras/evolution-manager', 'index.html'));
|
||||
|
||||
this.router.get('/*', (req, res) => {
|
||||
try {
|
||||
const pathname = req.url.split('?')[0];
|
||||
|
||||
// verify if url is a file in dist folder
|
||||
if (pathname === '/') throw {};
|
||||
const filePath = path.join(__dirname, '../../../', 'Extras/evolution-manager', pathname);
|
||||
|
||||
if (fs.existsSync(filePath)) {
|
||||
const contentType = mime.lookup(filePath) || 'text/plain';
|
||||
res.set('Content-Type', contentType);
|
||||
res.end(fs.readFileSync(filePath));
|
||||
return;
|
||||
}
|
||||
|
||||
res.set('Content-Type', 'text/html');
|
||||
res.send(index);
|
||||
} catch {
|
||||
res.set('Content-Type', 'text/html');
|
||||
res.send(index);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public readonly router = Router();
|
||||
}
|
||||
Reference in New Issue
Block a user