Skip to content

Commit

Permalink
feat: #319 PicGO图形化配置界面-增强SyCmd
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Feb 23, 2023
1 parent 955363b commit 2ce65a1
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 28 deletions.
11 changes: 8 additions & 3 deletions components/anki/AnkiIndex.vue
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions components/detail/PostDetailService.vue
Expand Up @@ -73,7 +73,6 @@

<!-- 管理图片 -->
<el-tooltip
v-if="false"
class="box-item"
effect="dark"
:content="$t('post.detail.button.pic.manage')"
Expand All @@ -86,7 +85,6 @@

<!-- 管理Anki -->
<el-tooltip
v-if="false"
class="box-item"
effect="dark"
:content="$t('post.detail.button.anki.mark')"
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "sy-post-publisher",
"private": true,
"version": "0.7.0",
"version": "0.6.8",
"scripts": {
"serve": "python scripts/serve.py",
"dev": "python scripts/dev.py",
Expand Down
89 changes: 67 additions & 22 deletions public/lib/cmd/syCmd.js
Expand Up @@ -28,35 +28,65 @@
/**
* 是否是Electron环境,等价于isInSiyuanOrSiyuanNewWin
*/
const isElectron = /Electron/.test(navigator.userAgent)
const { spawn } = require("child_process")

const { spawn } = isElectron ? require("child_process") : ""
const process = isElectron ? require("process") : ""
/**
* 获取Path环境变量
*/
const getEnvPath = (CUSTOM_PATH) => {
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 {
Expand All @@ -75,19 +105,34 @@ async function cmd(...command) {

/**
* 执行shell脚本
*
* @param shell
* @returns {Promise<undefined>}
*/
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
41 changes: 41 additions & 0 deletions 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
12 changes: 12 additions & 0 deletions 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<any>)
console.log("-----------------------")
console.log(result.data)
})
})
13 changes: 13 additions & 0 deletions utils/otherlib/scriptUtil.js
Expand Up @@ -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脚本
*
Expand All @@ -39,6 +51,7 @@ const execShellCmd = async (shell) => {

const scriptUtil = {
execShellCmd,
cmd,
}

export default scriptUtil

0 comments on commit 2ce65a1

Please sign in to comment.