doc and other commands

This commit is contained in:
Gabriel Pastori
2023-11-19 16:31:24 -03:00
parent f28fbfcea0
commit c66cd1ad7a
14 changed files with 289 additions and 77 deletions

18
lib/api/changeVersion.js Normal file
View File

@@ -0,0 +1,18 @@
const verifyEvolutionInstallation = require('../utils/verifyEvolutionInstallation.js');
const revertToVersion = require('../utils/revertToVersion.js');
module.exports = async (argv) => {
const { v } = argv || {}
if (!v) throw new Error('❌ Please specify a version to revert to. Example: evolution-manager api revert --v=1.5.0')
const isEvolutionInstalled = verifyEvolutionInstallation();
if (!isEvolutionInstalled) return;
console.log(`🔃 Reverting to Evolution-Api v${v}...`);
await revertToVersion(v);
console.log(`🔃 Reverted to Evolution-Api v${v} successfully`);
console.log('\n🔁 Please restart the process to use the reverted version\n')
};

View File

@@ -1,5 +1,10 @@
const functions = {
i: require('./setup.js'),
install: require('./setup.js'),
setup: require('./setup.js'),
uninstall: require('./uninstall.js'),
cv: require('./changeVersion.js'),
changeVersion: require('./changeVersion.js'),
}
module.exports = async (argv) => {

View File

@@ -1,16 +1,15 @@
const build = require('../utils/build.js');
const fs = require('fs-extra');
const path = require('path');
const verifyEvolutionInstallation = require('../utils/verifyEvolutionInstallation.js');
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');
console.time('📦 Copy dist folder to /Extras/evolution-manager');
const distFolder = path.join(__dirname, '..', '..', 'dist');
const extrasFolder = path.join(process.cwd(), 'Extras');
@@ -19,47 +18,18 @@ module.exports = async () => {
if (!fs.existsSync(evolutionManagerFolder)) fs.mkdirSync(evolutionManagerFolder);
fs.copySync(distFolder, evolutionManagerFolder);
console.timeEnd('📦 Copy dist folder to evolution-api/Extras/evolution-manager');
console.timeEnd('📦 Copy dist folder to /Extras/evolution-manager');
// Apply diff git patch
console.time('↘️ 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.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
}
console.log('\n🎉 Evolution-Api Manager installed successfully! 🎉');
console.log('🔁 Restart your Evolution-Api server to apply changes 🔁')
};

41
lib/api/uninstall.js Normal file
View File

@@ -0,0 +1,41 @@
const fs = require('fs-extra');
const path = require('path');
const { exec } = require('child_process');
const verifyEvolutionInstallation = require('../utils/verifyEvolutionInstallation.js');
const revertToVersion = require('../utils/revertToVersion.js');
module.exports = async () => {
const isEvolutionInstalled = verifyEvolutionInstallation();
if (!isEvolutionInstalled) return;
// Verify manager instalation
const extrasFolder = path.join(process.cwd(), 'Extras');
const evolutionManagerIndex = path.join(extrasFolder, 'evolution-manager/index.html');
if (!fs.existsSync(evolutionManagerIndex)) throw new Error('❌ Evolution Manager installation not found. Please install it first');
const apiVersion = isEvolutionInstalled.version;
// git pull force tag version
console.log(`🔃 Reverting to Evolution-Api v${apiVersion}...`);
await discardChanges();
await revertToVersion(apiVersion);
console.log(`🔃 Reverted to Evolution-Api v${apiVersion} successfully`);
console.log('\n🔁 Please restart the process to use the reverted version\n')
};
function discardChanges() {
return new Promise((resolve) => {
exec(`git clean -f -d -q && git checkout .`, (err) => {
if (err) {
console.error(err)
return resolve(false)
}
resolve(true)
})
})
}