运行命令适配wsl

This commit is contained in:
purocean
2019-08-06 17:28:10 +08:00
parent 8bd4c3bf8a
commit 553f8cc698
+37 -29
View File
@@ -3,40 +3,48 @@ import * as os from 'os'
import * as path from 'path'
import { execFileSync } from 'child_process'
import * as wsl from './wsl'
import config from './config'
const isWsl = wsl.isWsl
const runCode = (language: string, code: string) => {
try {
switch (language) {
case 'bat':
const fileName = 'yn-run-cmd.bat'
code = code.split('\n').slice(1).join('\n') // 去掉第一行的注释
if (isWsl) {
const tmpFile = path.join(wsl.toWslPath(wsl.getWinTempPath()), fileName)
fs.writeFileSync(tmpFile, code)
return execFileSync('cmd.exe', ['/c', `${wsl.toWinPath(tmpFile).replace('\\', '/')}`]).toString()
} else {
const tmpFile = path.join(os.tmpdir(), fileName)
fs.writeFileSync(tmpFile, code)
return execFileSync('cmd', ['/c', tmpFile]).toString()
}
case 'bash':
return execFileSync('bash', ['-c', code]).toString()
case 'php':
return execFileSync('php', ['-r', code]).toString()
case 'python':
return execFileSync('python3', ['-c', code]).toString()
case 'py':
return execFileSync('python3', ['-c', code]).toString()
case 'js':
return execFileSync('node', ['-e', code]).toString()
default:
return `不支持 ${language} 语言`
}
} catch (e) {
return e.message
try {
const languageMap = {
bash: { cmd: 'bash', args: ['-c'] },
php: { cmd: 'php', args: ['-r'] },
python: { cmd: 'python3', args: ['-c'] },
py: { cmd: 'python3', args: ['-c'] },
js: { cmd: 'node', args: ['-e'] },
} as {[key: string]: {cmd: string, args: string[]}}
if (language === 'bat') {
const fileName = 'yn-run-cmd.bat'
code = code.split('\n').slice(1).join('\n') // 去掉第一行的注释
if (isWsl) {
const tmpFile = path.join(wsl.toWslPath(wsl.getWinTempPath()), fileName)
fs.writeFileSync(tmpFile, code)
return execFileSync('cmd.exe', ['/c', `${wsl.toWinPath(tmpFile).replace('\\', '/')}`]).toString()
} else {
const tmpFile = path.join(os.tmpdir(), fileName)
fs.writeFileSync(tmpFile, code)
return execFileSync('cmd', ['/c', tmpFile]).toString()
}
}
const runParams = languageMap[language]
if (!runParams) {
return `不支持 ${language} 语言`
}
const useWsl = os.platform() === 'win32' && config.get('runCodeUseWsl', false)
if (useWsl) {
return execFileSync('wsl.exe', ['--', runParams.cmd].concat(runParams.args).concat([code])).toString()
}
return execFileSync(runParams.cmd, runParams.args.concat([code])).toString()
} catch (e) {
return e.message
}
}
export default {