diff --git a/back/api/script.ts b/back/api/script.ts index 0a418bf6..caba92ef 100644 --- a/back/api/script.ts +++ b/back/api/script.ts @@ -218,7 +218,7 @@ export default (app: Router) => { celebrate({ body: Joi.object({ filename: Joi.string().required(), - content: Joi.string().optional(), + content: Joi.string().optional().allow(''), path: Joi.string().optional().allow(''), }), }), @@ -227,7 +227,7 @@ export default (app: Router) => { try { let { filename, content, path } = req.body; const { name, ext } = parse(filename); - const filePath = join(path, `${name}.swap${ext}`); + const filePath = join(config.scriptPath, path, `${name}.swap${ext}`); fs.writeFileSync(filePath, content || '', { encoding: 'utf8' }); const scriptService = Container.get(ScriptService); @@ -254,7 +254,7 @@ export default (app: Router) => { try { let { filename, content, path } = req.body; const { name, ext } = parse(filename); - const filePath = join(path, `${name}.swap${ext}`); + const filePath = join(config.scriptPath, path, `${name}.swap${ext}`); const scriptService = Container.get(ScriptService); const result = await scriptService.stopScript(filePath); diff --git a/back/services/script.ts b/back/services/script.ts index c40c936b..3973a316 100644 --- a/back/services/script.ts +++ b/back/services/script.ts @@ -1,9 +1,11 @@ import { Service, Inject } from 'typedi'; import winston from 'winston'; -import { ChildProcessWithoutNullStreams, spawn } from 'child_process'; +import fs from 'fs'; +import path from 'path'; import SockService from './sock'; import CronService from './cron'; import ScheduleService, { TaskCallbacks } from './schedule'; +import config from '../config'; @Service() export default class ScriptService { @@ -14,7 +16,7 @@ export default class ScriptService { private scheduleService: ScheduleService, ) {} - private taskCallbacks(): TaskCallbacks { + private taskCallbacks(filePath: string): TaskCallbacks { return { onEnd: async (cp, endTime, diff) => { this.sockService.sendMessage({ @@ -23,6 +25,9 @@ export default class ScriptService { 'YYYY-MM-DD HH:mm:ss', )} 耗时 ${diff} 秒`, }); + try { + fs.unlinkSync(filePath); + } catch (error) {} }, onError: async (message: string) => { this.sockService.sendMessage({ @@ -40,14 +45,17 @@ export default class ScriptService { } public async runScript(filePath: string) { - const command = `task -l ${filePath} now`; - this.scheduleService.runTask(command, this.taskCallbacks()); + const relativePath = path.relative(config.scriptPath, filePath); + const command = `task -l ${relativePath} now`; + this.scheduleService.runTask(command, this.taskCallbacks(filePath)); return { code: 200 }; } - public async stopScript(path: string) { - const err = await this.cronService.killTask(`task -l ${path} now`); + public async stopScript(filePath: string) { + const relativePath = path.relative(config.scriptPath, filePath); + const err = await this.cronService.killTask(`task -l ${relativePath} now`); + const str = err ? `\n${err}` : ''; this.sockService.sendMessage({ type: 'manuallyRunScript',