diff --git a/components/anki/AnkiIndex.vue b/components/anki/AnkiIndex.vue index fa065246..29b7d664 100644 --- a/components/anki/AnkiIndex.vue +++ b/components/anki/AnkiIndex.vue @@ -181,7 +181,8 @@ const updateCard = async () => { const dataDir: string = getSiyuanNewWinDataDir() const ankisiyuanPath = `${dataDir}/widgets/sy-post-publisher/lib/cmd/ankisiyuan.bin` - const result = await scriptUtil.execShellCmd(ankisiyuanPath) + logger.info("ankisiyuanPath=>", ankisiyuanPath) + const result = await scriptUtil.cmd(ankisiyuanPath) if (result.code === 0) { ElMessage.success("操作成功,执行结果=>" + result.data) } else { @@ -265,19 +266,23 @@ const initPage = async () => { const valueArr = item.value?.split("\n") deckArr = valueArr[0] ?.replace(/"/g, "") - .replace(/"/g, "") .replace(/deck_name=/g, "") ?.split("::") if (valueArr.length > 1) { tagArr = valueArr[1] ?.replace(/"/g, "") - .replace(/"/g, "") .replace(/tags=\[/g, "") .replace(/]/g, "") .split(",") } } + deckArr = deckArr.filter(function (str) { + return str !== "" + }) + tagArr = tagArr.filter(function (str) { + return str !== "" + }) logger.debug("deckArr=>", deckArr) logger.debug("tagArr=>", tagArr) diff --git a/components/detail/PostDetailService.vue b/components/detail/PostDetailService.vue index ce251cb1..75f71853 100644 --- a/components/detail/PostDetailService.vue +++ b/components/detail/PostDetailService.vue @@ -73,7 +73,6 @@ { + let ENV_PATH = process.env.PATH + + const NEW_ENV_PATH = CUSTOM_PATH || "" + if (NEW_ENV_PATH !== "") { + ENV_PATH = process.env.PATH + ":" + NEW_ENV_PATH + } + return ENV_PATH +} + +/** + * 简单执行命令 + * + * @param command + */ async function cmd(...command) { - let p = spawn(command[0], command.slice(1)) + return await customCmd(command[0], [command.slice(1)]) +} + +/** + * 自定义命令 + * + * @param cmd 命令 + * @param args 参数数组 + * @param env 环境变量(可选) + */ +async function customCmd(cmd, args, env = {}) { + let p = spawn(cmd, args, { + cwd: process.cwd(), + env: Object.assign({}, process.env, env), + }) return new Promise((resolve, reject) => { let output = "" try { - p.stdout.on("data", (x) => { - // process.stdout.write(x.toString()) - // resolve(x.toString()) - output += x.toString() - }) + if (p.stdout) { + p.stdout.on("data", (x) => { + output += x.toString() + }) + } - p.stderr.on("data", (x) => { - // process.stderr.write(x.toString()) - // reject(x.toString()) - output += x.toString() - }) + if (p.stderr) { + p.stderr.on("data", (x) => { + output += x.toString() + }) + } p.on("exit", (code) => { - // resolve(code) - output += code + console.log("exit code=>", code) }) p.on("close", (code) => { let ret + output = output.replace(/\n$/, "") if (!code) { ret = { code: 0, data: output } } else { @@ -75,19 +105,34 @@ async function cmd(...command) { /** * 执行shell脚本 + * * @param shell - * @returns {Promise} */ -async function execShellCmd(shell) { - console.log("exec shell=>", shell) - const ret = await cmd("bash", "-c", shell) - console.log("exec finished=>", ret) +async function customShellCmd(shell) { + const ret = await customCmd("bash", ["-c", shell]) return ret } +const customPyCmd = async (pyCmd, pyArgs, pyPath = undefined) => { + const env = { + PATH: getEnvPath(pyPath), + } + return await customCmd(pyCmd, pyArgs, env) +} + +const customNodeCmd = async (nodeCmd, nodeArgs, nodePath = undefined) => { + const env = { + PATH: getEnvPath(nodePath), + } + return await customCmd(nodeCmd, nodeArgs, env) +} + const syCmd = { cmd, - execShellCmd, + customCmd, + customShellCmd, + customPyCmd, + customNodeCmd, } module.exports = syCmd diff --git a/sy-scripts/customCmd.js b/sy-scripts/customCmd.js new file mode 100644 index 00000000..dc8d570e --- /dev/null +++ b/sy-scripts/customCmd.js @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023, Terwer . All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Terwer designates this + * particular file as subject to the "Classpath" exception as provided + * by Terwer in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com + * or visit www.terwer.space if you need additional information or have any + * questions. + */ + +// 执行shell脚本 +const shellResult = await window.SyCmd.customShellCmd("ls") +console.log("-----------------------") +shellResult.data + +// 执行python脚本 +const pyPath = "/Users/terwer/Documents/mydocs/my-scripts/venv/bin" +const pyResult = await window.SyCmd.customPyCmd("python", ["-V"], pyPath) +console.log("-----------------------") +pyResult.data + +// 执行node脚本 +const nodePath = "/Users/terwer/Documents/app/node-v16.14.0-darwin-x64/bin" +const nodeResult = await window.SyCmd.customNodeCmd("node", ["-v"], nodePath) +console.log("-----------------------") +nodeResult.data diff --git a/test/public/lib/cmd/syCmd.test.ts b/test/public/lib/cmd/syCmd.test.ts new file mode 100644 index 00000000..69649f7d --- /dev/null +++ b/test/public/lib/cmd/syCmd.test.ts @@ -0,0 +1,12 @@ +import { describe } from "vitest" + +describe("syCmd test", async () => { + it("cmd test", async () => { + const syCmd = require("public/lib/cmd/syCmd.js") + // const syCmd = window.SyCmd + + const result = await (syCmd.customPyCmd("python", ["-V"]) as Promise) + console.log("-----------------------") + console.log(result.data) + }) +}) diff --git a/utils/otherlib/scriptUtil.js b/utils/otherlib/scriptUtil.js index 26819f8b..c831b4ac 100644 --- a/utils/otherlib/scriptUtil.js +++ b/utils/otherlib/scriptUtil.js @@ -25,6 +25,18 @@ import siyuanBrowserUtil from "~/utils/otherlib/siyuanBrowserUtil" +/** + * 执行命令 + * + * @param c + */ +const cmd = async (c) => { + const syWin = siyuanBrowserUtil.getSiyuanWindow() + const syCmd = syWin?.SyCmd + + return await syCmd.cmd(c) +} + /** * 执行shell脚本 * @@ -39,6 +51,7 @@ const execShellCmd = async (shell) => { const scriptUtil = { execShellCmd, + cmd, } export default scriptUtil