diff --git a/server/src/__tests__/start-command-normalization.test.ts b/server/src/__tests__/start-command-normalization.test.ts index bf5e51b..dc806e5 100644 --- a/server/src/__tests__/start-command-normalization.test.ts +++ b/server/src/__tests__/start-command-normalization.test.ts @@ -1,4 +1,7 @@ -import { normalizeInstanceStartCommand } from '../utils/startCommandNormalization.js' +import { + extractPosixWorkingDirectoryExecutable, + normalizeInstanceStartCommand +} from '../utils/startCommandNormalization.js' describe('实例启动命令当前目录前缀补全', () => { // 模拟工作目录中存在的文件 @@ -57,6 +60,19 @@ describe('实例启动命令当前目录前缀补全', () => { expect(normalizeInstanceStartCommand('other.sh', 'linux', isWorkingDirectoryFile)).toBe('other.sh') }) + it('chmod 目标要认得出补全后的引号形式', () => { + expect(extractPosixWorkingDirectoryExecutable('./start.sh')).toBe('start.sh') + expect(extractPosixWorkingDirectoryExecutable('./start.sh --nogui')).toBe('start.sh') + expect(extractPosixWorkingDirectoryExecutable('./sub/run.sh -Xmx2G')).toBe('sub/run.sh') + const normalized = normalizeInstanceStartCommand('"start.sh"', 'linux', isWorkingDirectoryFile) + expect(normalized).toBe('"./start.sh"') + expect(extractPosixWorkingDirectoryExecutable(normalized)).toBe('start.sh') + expect(extractPosixWorkingDirectoryExecutable('"./start.sh" --nogui')).toBe('start.sh') + expect(extractPosixWorkingDirectoryExecutable('java -jar server.jar')).toBeNull() + expect(extractPosixWorkingDirectoryExecutable('.\\start.bat')).toBeNull() + expect(extractPosixWorkingDirectoryExecutable('')).toBeNull() + }) + it('空命令与非法输入不报错', () => { expect(normalizeInstanceStartCommand('', 'win32', isWorkingDirectoryFile)).toBe('') expect(normalizeInstanceStartCommand(' ', 'linux', isWorkingDirectoryFile)).toBe(' ') diff --git a/server/src/modules/instance/InstanceManager.ts b/server/src/modules/instance/InstanceManager.ts index 16b4420..273262e 100644 --- a/server/src/modules/instance/InstanceManager.ts +++ b/server/src/modules/instance/InstanceManager.ts @@ -13,7 +13,7 @@ import { buildJavaEnvironmentSetupCommand, resolveJavaRuntimeEnvironment } from '../../utils/javaRuntimeEnvironment.js' -import { normalizeInstanceStartCommand } from '../../utils/startCommandNormalization.js' +import { extractPosixWorkingDirectoryExecutable, normalizeInstanceStartCommand } from '../../utils/startCommandNormalization.js' const execAsync = promisify(exec) @@ -940,12 +940,12 @@ export class InstanceManager extends EventEmitter { ) } - // 检查是否是 ./ 开头的命令 - if (startCommand.startsWith('./') && (platform === 'linux' || platform === 'darwin')) { - // Linux/Mac 平台:自动为文件添加可执行权限 - // 提取文件名(处理可能有参数的情况) - const commandParts = startCommand.split(/\s+/) - const scriptPath = commandParts[0].substring(2) // 去掉 ./ + // Linux/Mac:给 ./脚本 加可执行权限。补全后的命令可能是 ./start.sh, + // 也可能是 "./start.sh"(用户原来写了引号)。后者不以 ./ 开头,但仍是同一个脚本。 + const scriptPath = (platform === 'linux' || platform === 'darwin') + ? extractPosixWorkingDirectoryExecutable(startCommand) + : null + if (scriptPath) { const fullPath = path.join(instance.workingDirectory, scriptPath) try { diff --git a/server/src/utils/startCommandNormalization.ts b/server/src/utils/startCommandNormalization.ts index 656aedc..9d13929 100644 --- a/server/src/utils/startCommandNormalization.ts +++ b/server/src/utils/startCommandNormalization.ts @@ -61,4 +61,32 @@ export const normalizeInstanceStartCommand = ( } return `${quote}${isWindows ? '.\\' : './'}${token}${quote}${rest}` +} + +/** + * 从即将发给 bash 的启动命令中取出 ./脚本 的工作目录相对路径,供 chmod +x 使用。 + * 与 normalizeInstanceStartCommand 一样,首个词可以带双引号。 + */ +export function extractPosixWorkingDirectoryExecutable(command: string): string | null { + const trimmed = typeof command === 'string' ? command.trim() : '' + if (!trimmed) { + return null + } + + const match = trimmed.match(/^(\S+)([\s\S]*)$/) + if (!match) { + return null + } + + let token = match[1] + if (token.length > 2 && token.startsWith('"') && token.endsWith('"')) { + token = token.slice(1, -1) + } + + if (!token.startsWith('./')) { + return null + } + + const scriptPath = token.slice(2) + return scriptPath || null } \ No newline at end of file