Skip to content

Commit

Permalink
feat: #319 PicGO图形化配置界面-增强SyCmd,适配Anki同步
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Feb 23, 2023
1 parent b08a794 commit 96de9a8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 17 deletions.
9 changes: 6 additions & 3 deletions components/anki/AnkiIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,16 @@ const updateCard = async () => {
isAnkiLoading.value = true
const dataDir: string = getSiyuanNewWinDataDir()
const workDir = `${dataDir}/widgets/sy-post-publisher/lib/cmd`
const ankisiyuanPath = `${dataDir}/widgets/sy-post-publisher/lib/cmd/ankisiyuan.bin`
logger.info("ankisiyuanPath=>", ankisiyuanPath)
const result = await scriptUtil.cmd(ankisiyuanPath)
const result = await scriptUtil.customCmd(ankisiyuanPath, [], workDir)
if (result.code === 0) {
ElMessage.success("操作成功,执行结果=>" + result.data)
ElMessage.success(
"操作成功,执行结果=>" + result.data.split("\n").slice(-2).join(" ")
)
} else {
ElMessage.error("操异常,错误消息=>" + result.data)
ElMessage.error("操作异常,错误消息=>" + result.data)
}
isAnkiLoading.value = false
Expand Down
46 changes: 37 additions & 9 deletions public/lib/cmd/syCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ async function cmd(...command) {
* @param cmd 命令
* @param args 参数数组
* @param env 环境变量(可选)
* @param cwd 执行路径(可选)
*/
async function customCmd(cmd, args, env = {}) {
async function customCmd(cmd, args, env = {}, cwd = process.cwd()) {
let p = spawn(cmd, args, {
cwd: process.cwd(),
cwd: cwd,
env: Object.assign({}, process.env, env),
})
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -106,25 +107,52 @@ async function customCmd(cmd, args, env = {}) {
/**
* 执行shell脚本
*
* @param shell
* @param shell shell命令
* @param cwd 工作目录
*/
async function customShellCmd(shell) {
const ret = await customCmd("bash", ["-c", shell])
async function customShellCmd(shell, cwd = undefined) {
const ret = await customCmd("bash", ["-c", shell], cwd)
return ret
}

const customPyCmd = async (pyCmd, pyArgs, pyPath = undefined) => {
/**
* 执行python命令
*
* @param pyCmd python命令
* @param pyArgs 参数
* @param pyPath python环境变量
* @param cwd 工作目录
*/
const customPyCmd = async (
pyCmd,
pyArgs,
pyPath = undefined,
cwd = undefined
) => {
const env = {
PATH: getEnvPath(pyPath),
}
return await customCmd(pyCmd, pyArgs, env)
return await customCmd(pyCmd, pyArgs, env, cwd)
}

const customNodeCmd = async (nodeCmd, nodeArgs, nodePath = undefined) => {
/**
* 执行Node命令
*
* @param nodeCmd node命令
* @param nodeArgs 参数
* @param nodePath Node环境变量
* @param cwd 工作目录
*/
const customNodeCmd = async (
nodeCmd,
nodeArgs,
nodePath = undefined,
cwd = undefined
) => {
const env = {
PATH: getEnvPath(nodePath),
}
return await customCmd(nodeCmd, nodeArgs, env)
return await customCmd(nodeCmd, nodeArgs, env, cwd)
}

const syCmd = {
Expand Down
5 changes: 4 additions & 1 deletion sy-scripts/customCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@

// 执行自定义脚本
const result = await window.SyCmd.customCmd(
"/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/data/widgets/sy-post-publisher/lib/cmd/ankisiyuan.bin"
"/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/data/widgets/sy-post-publisher/lib/cmd/ankisiyuan.bin",
[],
{},
"/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/data/widgets/sy-post-publisher/lib/cmd"
)
console.log("-----------------------")
result.data
Expand Down
23 changes: 19 additions & 4 deletions utils/otherlib/scriptUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,36 @@ const cmd = async (c) => {
return await syCmd.cmd(c)
}

/**
* 执行命令
*
* @param cmd 命令
* @param args 参数
* @param cwd 工作目录
*/
const customCmd = async (cmd, args, cwd) => {
const syWin = siyuanBrowserUtil.getSiyuanWindow()
const syCmd = syWin?.SyCmd

return await syCmd.customCmd(cmd, args, {}, cwd)
}

/**
* 执行shell脚本
*
* @param shell
* @param shell shell
*/
const execShellCmd = async (shell) => {
const customShellCmd = async (shell) => {
const syWin = siyuanBrowserUtil.getSiyuanWindow()
const syCmd = syWin?.SyCmd

return await syCmd.execShellCmd(shell)
return await syCmd.customShellCmd(shell)
}

const scriptUtil = {
execShellCmd,
cmd,
customCmd,
customShellCmd,
}

export default scriptUtil

0 comments on commit 96de9a8

Please sign in to comment.