mirror of
https://github.com/EvolutionAPI/evolution-manager.git
synced 2025-07-13 07:04:50 -06:00
feat: cli v0.2.0
This commit is contained in:
parent
5e0c55b489
commit
b6f447bb0a
1
.gitignore
vendored
1
.gitignore
vendored
@ -20,3 +20,4 @@ pnpm-debug.log*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
.vercel
|
||||
|
2
bin/evolution-manager
Normal file
2
bin/evolution-manager
Normal file
@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env node
|
||||
require('./../lib/cli.js');
|
29
lib/cli.js
Normal file
29
lib/cli.js
Normal file
@ -0,0 +1,29 @@
|
||||
var argv = require('optimist').argv
|
||||
|
||||
|
||||
const operations = {
|
||||
// 'create': require('./create'),
|
||||
'help': require('./help'),
|
||||
'server': require('./server'),
|
||||
'pm2': require('./pm2'),
|
||||
}
|
||||
|
||||
|
||||
function main() {
|
||||
try{
|
||||
|
||||
var operation = argv._[0]
|
||||
if (!operation) operation = 'help'
|
||||
if (!operations[operation]) {
|
||||
console.error('Unknown operation: ' + operation)
|
||||
operation = 'help'
|
||||
}
|
||||
operations[operation](argv)
|
||||
} catch (e) {
|
||||
console.error(e.message || e)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
|
21
lib/help.js
Normal file
21
lib/help.js
Normal file
@ -0,0 +1,21 @@
|
||||
module.exports = () => {
|
||||
// welcome message
|
||||
console.log(`👋 Welcome to evolution-manager CLI!`)
|
||||
|
||||
// help message
|
||||
console.log(`📋 Available commands:`)
|
||||
console.log(` help`)
|
||||
console.log(` server`)
|
||||
console.log(` - start [--port=9615]`)
|
||||
console.log(` - build`)
|
||||
console.log(` pm2`)
|
||||
console.log(` - setup`)
|
||||
console.log(` - start`)
|
||||
console.log(` - stop`)
|
||||
console.log(` - restart`)
|
||||
console.log(` - delete`)
|
||||
|
||||
|
||||
// spacing
|
||||
console.log(`\n`)
|
||||
}
|
21
lib/pm2/delete.js
Normal file
21
lib/pm2/delete.js
Normal file
@ -0,0 +1,21 @@
|
||||
const { exec } = require('child_process');
|
||||
const detectPm2ProcessId = require('../utils/detectPm2ProcessId.js');
|
||||
|
||||
module.exports = async (argv, id) => {
|
||||
const pm2ProcessId = id || await detectPm2ProcessId();
|
||||
if(pm2ProcessId === false) {
|
||||
console.log('⚙️ Manager process not setup')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('⚙️ Deleting PM2 process')
|
||||
exec(`pm2 del ${pm2ProcessId}`, (err, stdout) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
console.log(stdout)
|
||||
})
|
||||
|
||||
console.log('🗑️ PM2 process deleted')
|
||||
}
|
25
lib/pm2/index.js
Normal file
25
lib/pm2/index.js
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
|
||||
const functions = {
|
||||
setup: require('./setup.js'),
|
||||
reset: require('./reset.js'),
|
||||
start: require('./start.js'),
|
||||
stop: require('./stop.js'),
|
||||
delete: require('./delete.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)
|
||||
}
|
||||
}
|
21
lib/pm2/reset.js
Normal file
21
lib/pm2/reset.js
Normal file
@ -0,0 +1,21 @@
|
||||
const { exec } = require('child_process');
|
||||
const detectPm2ProcessId = require('../utils/detectPm2ProcessId.js');
|
||||
|
||||
module.exports = async (argv, id) => {
|
||||
const pm2ProcessId = id || await detectPm2ProcessId();
|
||||
if (pm2ProcessId === false) {
|
||||
console.log('⚙️ Manager process not setup')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('⚙️ Restarting PM2 process')
|
||||
exec(`pm2 restart ${pm2ProcessId}`, (err, stdout) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
console.log(stdout)
|
||||
})
|
||||
|
||||
console.log('🚀 PM2 process restarted')
|
||||
}
|
34
lib/pm2/setup.js
Normal file
34
lib/pm2/setup.js
Normal file
@ -0,0 +1,34 @@
|
||||
const build = require('../utils/build.js');
|
||||
const verifyPm2Installation = require('../utils/verifyPm2Installation.js');
|
||||
const detectPm2ProcessId = require('../utils/detectPm2ProcessId.js');
|
||||
const { exec } = require('child_process')
|
||||
|
||||
module.exports = async (argv) => {
|
||||
const port = argv.port || 9615;
|
||||
|
||||
await verifyPm2Installation(true);
|
||||
await build();
|
||||
|
||||
const pm2ProcessId = await detectPm2ProcessId();
|
||||
|
||||
if (pm2ProcessId !== false) {
|
||||
console.log('📦 Manager process already setup')
|
||||
await require('./reset.js')(argv, pm2ProcessId);
|
||||
return
|
||||
}
|
||||
|
||||
console.log('⚙️ Starting PM2 process')
|
||||
console.time('⚙️ Starting PM2 process')
|
||||
exec(`pm2 serve dist/ ${port} --name evolution-manager --spa`, (err, stdout) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
console.log(stdout)
|
||||
console.timeEnd('⚙️ Starting PM2 process')
|
||||
|
||||
console.log('🚀 PM2 process started')
|
||||
console.log('🚀 Manager ready in port :' + port)
|
||||
})
|
||||
}
|
||||
|
21
lib/pm2/start.js
Normal file
21
lib/pm2/start.js
Normal file
@ -0,0 +1,21 @@
|
||||
const { exec } = require('child_process');
|
||||
const detectPm2ProcessId = require('../utils/detectPm2ProcessId.js');
|
||||
|
||||
module.exports = async (argv, id) => {
|
||||
const pm2ProcessId = id || await detectPm2ProcessId();
|
||||
if (pm2ProcessId === false) {
|
||||
console.log('⚙️ Manager process not setup')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('⚙️ Start PM2 process')
|
||||
exec(`pm2 start ${pm2ProcessId}`, (err, stdout) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
console.log(stdout)
|
||||
})
|
||||
|
||||
console.log('🚀 PM2 process started')
|
||||
}
|
21
lib/pm2/stop.js
Normal file
21
lib/pm2/stop.js
Normal file
@ -0,0 +1,21 @@
|
||||
const { exec } = require('child_process');
|
||||
const detectPm2ProcessId = require('../utils/detectPm2ProcessId.js');
|
||||
|
||||
module.exports = async (argv, id) => {
|
||||
const pm2ProcessId = id || await detectPm2ProcessId();
|
||||
if(pm2ProcessId === false) {
|
||||
console.log('⚙️ Manager process not setup')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('⚙️ Stopping PM2 process')
|
||||
exec(`pm2 stop ${pm2ProcessId}`, (err, stdout) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
console.log(stdout)
|
||||
})
|
||||
|
||||
console.log('🛑 PM2 process stopped')
|
||||
}
|
60
lib/server.js
Normal file
60
lib/server.js
Normal file
@ -0,0 +1,60 @@
|
||||
// const pm2 = require('pm2')
|
||||
const mime = require('mime-types');
|
||||
var http = require('http');
|
||||
var fs = require('fs');
|
||||
const url = require('url');
|
||||
const build = require('./utils/build.js')
|
||||
|
||||
const functions = {
|
||||
start,
|
||||
build,
|
||||
}
|
||||
|
||||
module.exports = async (argv) => {
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
function startServer(argv) {
|
||||
const { port = 9615 } = argv || {}
|
||||
|
||||
const index = fs.readFileSync(process.cwd() + '/dist/index.html');
|
||||
|
||||
http.createServer(function (req, res) {
|
||||
try {
|
||||
const parsedUrl = url.parse(req.url, true);
|
||||
|
||||
// verify if url is a file in dist folder
|
||||
if (parsedUrl.pathname === '/') throw {}
|
||||
let filePath = process.cwd() + '/dist' + parsedUrl.pathname;
|
||||
|
||||
if (fs.existsSync(filePath)) {
|
||||
const contentType = mime.lookup(filePath) || 'text/plain';
|
||||
res.writeHead(200, { 'Content-Type': contentType });
|
||||
res.end(fs.readFileSync(filePath));
|
||||
return
|
||||
}
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(index);
|
||||
} catch {
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(index);
|
||||
}
|
||||
}).listen(port);
|
||||
|
||||
console.log('🚀 Server start')
|
||||
console.log('🚀 Server listening on port ' + port)
|
||||
}
|
||||
|
||||
async function start(argv) {
|
||||
await build()
|
||||
startServer(argv)
|
||||
}
|
||||
|
25
lib/utils/build.js
Normal file
25
lib/utils/build.js
Normal file
@ -0,0 +1,25 @@
|
||||
const { exec } = require('child_process')
|
||||
const fs = require('fs')
|
||||
|
||||
module.exports = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log('📦 Build start')
|
||||
console.time('📦 Build complete')
|
||||
const distFolder = process.cwd() + '/dist'
|
||||
if (fs.existsSync(distFolder)) {
|
||||
console.time('📦 Remove dist folder')
|
||||
fs.rmSync(distFolder, { recursive: true, force: true })
|
||||
console.timeEnd('📦 Remove dist folder')
|
||||
}
|
||||
exec('npm run build', (err, stdout) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
console.log(stdout)
|
||||
console.timeEnd('📦 Build complete')
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
18
lib/utils/detectPm2ProcessId.js
Normal file
18
lib/utils/detectPm2ProcessId.js
Normal file
@ -0,0 +1,18 @@
|
||||
const { exec } = require('child_process');
|
||||
|
||||
module.exports = async () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec('pm2 jlist', async (err, stdout) => {
|
||||
if (err) {
|
||||
console.log('⚙️ PM2 is not installed');
|
||||
return reject(false);
|
||||
}
|
||||
|
||||
const list = JSON.parse(stdout);
|
||||
|
||||
const process = list.find((process) => process.name === 'evolution-manager');
|
||||
resolve(process ? process.pm_id : false);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
35
lib/utils/verifyPm2Installation.js
Normal file
35
lib/utils/verifyPm2Installation.js
Normal file
@ -0,0 +1,35 @@
|
||||
const { exec } = require('child_process');
|
||||
|
||||
module.exports = async (install = false) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec('pm2 -v', async (err) => {
|
||||
if (err) {
|
||||
console.log('⚙️ PM2 is not installed')
|
||||
if (!install) return reject(false)
|
||||
const installSuccess = await installPM2()
|
||||
if (!installSuccess) return reject(false)
|
||||
resolve(true)
|
||||
}
|
||||
console.log('⚙️ PM2 is installed')
|
||||
resolve(true)
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
function installPM2() {
|
||||
return new Promise((resolve) => {
|
||||
console.log('⚙️ Installing PM2...')
|
||||
console.time('⚙️ PM2 installed successfully')
|
||||
exec('npm install -g pm2', (err, stdout) => {
|
||||
console.timeEnd('⚙️ PM2 installed successfully')
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return resolve(false)
|
||||
}
|
||||
console.log(stdout)
|
||||
resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "evolution-manager",
|
||||
"version": "0.1.0",
|
||||
"version": "0.2.0",
|
||||
"main": "dist",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
@ -8,11 +8,16 @@
|
||||
"preview": "vite preview",
|
||||
"lint": "eslint . --fix --ignore-path .gitignore"
|
||||
},
|
||||
"directories": {
|
||||
"bin": "./bin"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mdi/font": "7.0.96",
|
||||
"axios": "^1.6.0",
|
||||
"core-js": "^3.29.0",
|
||||
"optimist": "^0.6.1",
|
||||
"pinia": "^2.0.0",
|
||||
"pm2": "^5.3.0",
|
||||
"roboto-fontface": "*",
|
||||
"vue": "^3.2.0",
|
||||
"vue-router": "^4.0.0",
|
||||
@ -22,6 +27,7 @@
|
||||
"@vitejs/plugin-vue": "^4.0.0",
|
||||
"eslint": "^8.37.0",
|
||||
"eslint-plugin-vue": "^9.3.0",
|
||||
"node-async-exec": "^1.2.0",
|
||||
"sass": "^1.60.0",
|
||||
"unplugin-fonts": "^1.0.3",
|
||||
"vite": "^4.2.0",
|
||||
|
Loading…
Reference in New Issue
Block a user