优化 getQuickcommandTempFile

This commit is contained in:
fofolee 2022-04-29 20:48:03 +08:00
parent 7676441678
commit 363a39a36f
2 changed files with 37 additions and 30 deletions

View File

@ -157,14 +157,13 @@ window.quickcommand = {
}, },
// 载入在线资源 // 载入在线资源
loadRemoteScript: async function(url, forceUpdate = false) { loadRemoteScript: async function(url) {
if (!/^((ht|f)tps?):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?/.test(url)) throw 'url 不合法' if (!/^((ht|f)tps?):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?/.test(url)) throw 'url 不合法'
let remote = url let local = getQuickcommandTempFile('js')
let root = path.join(os.tmpdir(), 'qcRemoteScript') await this.downloadFile(url, local)
if (!fs.existsSync(root)) fs.mkdirSync(root) let source = require(local)
let local = path.join(root, require('crypto').createHash('md5').update(url).digest('hex')) fs.unlinkSync(local)
if (forceUpdate || !fs.existsSync(local)) await this.downloadFile(remote, local) return source
return require(local)
}, },
// 唤醒 uTools // 唤醒 uTools
@ -187,7 +186,7 @@ window.quickcommand = {
// 运行vbs脚本 // 运行vbs脚本
if (process.platform == 'win32') quickcommand.runVbs = function(script) { if (process.platform == 'win32') quickcommand.runVbs = function(script) {
return new Promise((reslove, reject) => { return new Promise((reslove, reject) => {
var tempfile = path.join(os.tmpdir(), 'TempVBSScript.vbs') var tempfile = getQuickcommandTempFile('vbs', 'TempVBSScript')
fs.writeFile(tempfile, iconv.encode(script, 'gbk'), err => { fs.writeFile(tempfile, iconv.encode(script, 'gbk'), err => {
child_process.exec(`cscript.exe /nologo "${tempfile}"`, { child_process.exec(`cscript.exe /nologo "${tempfile}"`, {
encoding: "buffer" encoding: "buffer"
@ -258,7 +257,7 @@ window.pluginInfo = () => {
let getSleepCodeByShell = ms => { let getSleepCodeByShell = ms => {
var cmd, tempFilePath var cmd, tempFilePath
if (utools.isWindows()) { if (utools.isWindows()) {
tempFilePath = getQuickcommandTempFile('vbs') tempFilePath = getQuickcommandTempFile('vbs', 'SleepVBSScript')
cmd = `echo set ws=CreateObject("Wscript.Shell") > ${tempFilePath} && echo Wscript.sleep ${ms} >> ${tempFilePath} && cscript /nologo ${tempFilePath}` cmd = `echo set ws=CreateObject("Wscript.Shell") > ${tempFilePath} && echo Wscript.sleep ${ms} >> ${tempFilePath} && cscript /nologo ${tempFilePath}`
} else { } else {
cmd = `sleep ${ms / 1000}` cmd = `sleep ${ms / 1000}`
@ -316,8 +315,16 @@ window.getUtoolsPlugins = () => {
return plugins; return plugins;
} }
window.getQuickcommandTempFile = ext => { window.getQuickcommandTempFile = (ext, name, dir = 'quickcommandTempDir') => {
return path.join(os.tmpdir(), `quickcommandTempFile.${ext}`) if (!name) name = new Date().getTime() + (Math.random() * 10 ** 6).toFixed()
let tempDir = path.join(os.tmpdir(), dir)
if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir)
return path.join(tempDir, `${name}.${ext}`)
}
window.delTempFile = (...args) => {
let tmpPath = path.join(os.tmpdir(), ...args)
if (fs.existsSync(tmpPath)) fs.unlinkSync(tmpPath)
} }
window.getBase64Ico = filepath => { window.getBase64Ico = filepath => {
@ -518,7 +525,7 @@ window.runCodeFile = (cmd, option, terminal, callback) => {
ext = option.ext, ext = option.ext,
charset = option.charset, charset = option.charset,
scptarg = option.scptarg || ""; scptarg = option.scptarg || "";
let script = getQuickcommandTempFile(ext) let script = getQuickcommandTempFile(ext, 'quickcommandTempScript')
// 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题 // 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题
if (charset.scriptCode) cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), charset.scriptCode); if (charset.scriptCode) cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), charset.scriptCode);
fs.writeFileSync(script, cmd); fs.writeFileSync(script, cmd);

View File

@ -177,7 +177,7 @@ export default {
argvs: imgUrl, argvs: imgUrl,
readfile: false, readfile: false,
}); });
let imgPath = window.getQuickcommandTempFile(imgInfo.ext); let imgPath = window.getQuickcommandTempFile(imgInfo.ext, 'TempImgFile');
quickcommand quickcommand
.downloadFile(imgUrl, imgPath) .downloadFile(imgUrl, imgPath)
.then(() => { .then(() => {