Skip to content

Commit

Permalink
增加运行命令
Browse files Browse the repository at this point in the history
  • Loading branch information
kscript committed Apr 5, 2024
1 parent d387795 commit f882ad0
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "command-manager",
"version": "0.2.1",
"version": "0.2.2",
"private": true,
"scripts": {
"start": "vue-cli-service serve --port 27000",
Expand Down
87 changes: 86 additions & 1 deletion server/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const path = require('path')
const fs = require('fs-extra')
const router = require('koa-router')()
const { useResponse } = require('../utils')
const { exec } = require('child_process')
const { exec, spawn } = require('child_process')
const commandList = {}

router.get('/', async (ctx) => {
await ctx.render('index', {})
Expand Down Expand Up @@ -48,4 +49,88 @@ router.post('/open', async (ctx) => {
useResponse(ctx, {}, 500, '文件目录不存在')
})

router.post('/stop', async (ctx) => {
data = ctx.request.body
console.log(commandList[data.uuid])
if (data.uuid && Array.isArray(commandList[data.uuid])) {
commandList[data.uuid].forEach(list => {
list.forEach(item => {
if (!item.close) {
item.child.kill('SIGKILL')
}
})
})
delete commandList[data.uuid]
return useResponse(ctx, 1)
}
useResponse(ctx, 0)
})

router.post('/exec', async (ctx) => {
const current = []
const commands = []
const data = ctx.request.body
if (data.uuid) {
commandList[data.uuid] = commandList[data.uuid] || []
commandList[data.uuid].push(current)
}
if (data.nodeVersion) {
commands.push('nvm use ' + data.nodeVersion)
}
if (typeof data.commandLines === 'string' || Array.isArray(data.commandLines)) {
const commandLines = (Array.isArray(data.commandLines) ? data.commandLines : data.commandLines.split('\n'))
commandLines.map(command => {
commands.push(command.replace(/(^\s+|\s+$)/g, ''))
})
const result = []
const promises = commands.map((command, index) => {
return new Promise((resolve, reject) => {
if (!command) resolve()
const child = spawn('cmd.exe', ['/c', command], {
cwd: data.path
})
const item = {
command,
child
}
current.push(item)
child.stdout.on('data', data => {
console.log(`${command} 输出:\n${data}\n`)
})
child.stderr.on('data', data => {
console.error(`${command} 错误:\n${data}\n`)
})
child.on('close', code => {
Object.assign(item, {
close: true,
code
})
result[index] = {
command,
code
}
if (code === 0) resolve()
else reject(code)
})
})
})
console.log(commands)
const execPromise = () => Promise.all(promises).then(() => {
useResponse(ctx, result.slice(data.nodeVersion ? 3 : 2))
}).catch(error => {
useResponse(ctx, {}, 500, error.toString())
}).finally(() => {
if (data.uuid) {
commandList[data.uuid] = commandList[data.uuid].filter(item => item !== current)
}
})
if (data.await) {
return execPromise()
} else {
execPromise()
}
}
useResponse(ctx, {})
})

module.exports = router
6 changes: 6 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ export default {
},
open (data) {
return fetchApi('open', 'post', data)
},
exec (data) {
return fetchApi('exec', 'post', data)
},
stop (data) {
return fetchApi('stop', 'post', data)
}
}
29 changes: 24 additions & 5 deletions src/views/manager/list.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<el-table :data="tableData" @dblclick="row => handleRow('managerEdit', row)">
<el-table :data="tableData" @row-dblclick="row => handleRow('managerEdit', row)">
<el-table-column type="index" label="序号" width="60" />
<el-table-column prop="name" label="名称" width="120" />
<el-table-column prop="port" label="端口" width="120" />
Expand All @@ -8,11 +8,14 @@
<el-table-column prop="path" label="脚本路径" />
<el-table-column prop="keywords" label="关键词" />
<el-table-column prop="desc" label="备注" />
<el-table-column prop="handle" label="操作" width="240">
<template #default="{ row }">
<el-button type="info" size="small" @click="handleOpen(row)">打开路径</el-button>
<el-button type="warning" size="small" @click="handleRow('managerEdit', row)">修改</el-button>
<el-table-column prop="handle" label="操作" width="400">
<template #default="{ row, $index }">
<el-button size="small" @click="handleOpen(row)">打开路径</el-button>
<el-button type="danger" size="small" @click="handleExec(row)">执行</el-button>
<el-button type="info" size="small" @click="handleStop(row)">停止</el-button>
<el-button type="success" size="small" @click="handleRow('managerEdit', row)">修改</el-button>
<el-button type="primary" size="small" @click="handleRow('managerCopy', row)">复制</el-button>
<el-button type="warning" size="small" @click="handleDelete(row, $index)">删除</el-button>
</template>
</el-table-column>
</el-table>
Expand All @@ -23,7 +26,9 @@ import { ref } from 'vue'
import api from '@/api'
import { manifest } from './common'
import router from '@/router'
import { ElMessageBox } from 'element-plus'
const handleRow = (type, row) => {
console.log(row)

Check warning on line 31 in src/views/manager/list.vue

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
router.push({
name: type,
query: {
Expand All @@ -34,6 +39,20 @@ const handleRow = (type, row) => {
const handleOpen = (row) => {
api.open(row)
}
const handleExec = (row) => {
api.exec(row)
}
const handleStop = (row) => {
api.stop(row)
}
const handleDelete = (row, $index) => {
ElMessageBox.confirm(`是否删除 ${row.name} 命令?`, {
title: '删除确认',
type: 'warning'
}).then(() => {
tableData.value.splice($index)
}).catch(console.log)

Check warning on line 54 in src/views/manager/list.vue

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
}
const tableData = ref(manifest.value.commands)
</script>
<style scoped>
Expand Down

0 comments on commit f882ad0

Please sign in to comment.