Skip to content

Commit

Permalink
feat: #27 使用插件系统挂载菜单-zhi插件同步
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 5, 2023
1 parent c044758 commit 2e6f855
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"new": "python script/version.py 1.0.0"
},
"dependencies": {
"compare-versions": "6.0.0-rc.1",
"loglevel": "^1.8.1",
"loglevel-plugin-prefix": "^0.8.4",
"react": "^18.2.0",
Expand Down
22 changes: 14 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 29 additions & 9 deletions src/apps/zhi/plugin-system/plugin-system-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@
* questions.
*/

// 警告1⚠️:此文件会在购建时生成js文件,并且由theme.js动态调用
// 警告2⚠️:请勿主动调用此文件中的任何方法

import logFactory from "~/src/utils/logUtil"
import pluginSystemUtil, {
HackPluginSystem,
} from "~/src/utils/otherlib/pluginSystemUtil"
import siyuanUtil from "~/src/utils/otherlib/siyuanUtil"
import strUtil from "~/src/utils/strUtil"
import nodeUtil from "~/src/utils/nodeUtil"
import { compareVersions } from "compare-versions"
import cjsUtil from "~/src/utils/cjsUtil"

// 警告1⚠️:此文件会在购建时生成js文件,并且由theme.js动态调用
// 警告2⚠️:请勿主动调用此文件中的任何方法

const fs = window.require("fs")
const fs = cjsUtil.safeRequire("fs")
const path = window.require("path")

/**
Expand Down Expand Up @@ -66,7 +69,7 @@ class PluginSystemHook {
}

// 当前插件有新版本
if (true) {
if (compareVersions(zhiPlugin.version, item.version) > 0) {
isUpdate = true
}

Expand Down Expand Up @@ -122,10 +125,11 @@ class PluginSystemHook {
const oldVersion = oldPluginInfo.oldVersion
this.logger.info(
strUtil.f(
"Plugin status : [{0}] isSynced=>{1}, isUpdate=>{2}, version Info: {3} -> {4}",
"Plugin status : [{0}] isSynced=>{1}, isUpdate=>{2}, forceUpdate=>{3}, version Info: {4} -> {5}",
pluginBasename,
oldPluginInfo.isSynced,
oldPluginInfo.isUpdate,
manifest.forceUpdate,
oldVersion,
manifest.version
)
Expand All @@ -143,7 +147,7 @@ class PluginSystemHook {
}

strUtil.f("Do syncing, please wait...")
// fs.copyFileSync(from, to)
nodeUtil.copyFolderSync(from, to)
syncedCount++
} else if (oldPluginInfo.isSynced && oldPluginInfo.isUpdate) {
// 新插件目录不一致,但是有版本号
Expand All @@ -157,11 +161,27 @@ class PluginSystemHook {
}

strUtil.f("Do syncing, please wait...")
// fs.copyFileSync(from, to)
nodeUtil.copyFolderSync(from, to)
syncedCount++
} else if (manifest.forceUpdate) {
this.logger.warn(
strUtil.f(
"Find forceUpdate flag in manifest.json, try forcing update plugin, [{0}] {1}.This flag is development only, before publish plugin, you should remove this flag from manifest.json!",
pluginBasename,
manifest.version
)
)

nodeUtil.rmFolder(to)
nodeUtil.copyFolderSync(from, to)
syncedCount++
} else {
this.logger.debug(
strUtil.f("{0} already synced and the latest version", item)
strUtil.f(
"Already synced and the latest version [{0}] {1}",
pluginBasename,
manifest.version
)
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/apps/zhi/zhi-plugins/zhi-demo-plugin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
*/

import logFactory from "~/src/utils/logUtil"
import cjsUtil from "~/src/utils/cjsUtil"

const siyuan = require("siyuan")

const siyuan = cjsUtil.safeRequire("siyuan")
const Plugin = siyuan.Plugin

/**
Expand Down
3 changes: 2 additions & 1 deletion src/apps/zhi/zhi-plugins/zhi-demo-plugin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "zhiDemoPlugin",
"author": "terwer",
"url": "https://github.com/terwer/zhi",
"version": "1.0.0"
"version": "1.0.0",
"forceUpdate": true
}
48 changes: 48 additions & 0 deletions src/utils/cjsUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.
*/

// 警告⚠️:请勿在非Node环境调用此文件中的任何方法

/**
* 安全的require
* 注意:使用vite打包,require和window.require行为不一样,为了兼容性,强烈建议使用cjsUtil.safeRequire
*
* @param moduleName 模块名
* @author terwer
* @since 1.0.0
*/
const safeRequire = (moduleName: string): any => {
if (require === window.require) {
return require(moduleName)
}

return window.require(moduleName)
}

const cjsUtil = {
safeRequire,
}

export default cjsUtil
77 changes: 77 additions & 0 deletions src/utils/nodeUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.
*/

// 警告⚠️:请勿在非Node环境调用此文件中的任何方法

import cjsUtil from "~/src/utils/cjsUtil"

const fs = cjsUtil.safeRequire("fs")
const path = cjsUtil.safeRequire("path")

/**
* 可以使用Node.js内置的fs模块中的`copyFileSync`或者`copyFile`方法来复制文件夹。不过需要注意,这两个方法只能复制单个文件,如果想要复制整个文件夹,需要自己编写递归函数实现。
* 本方法用于复制一个文件夹以及其中所有子文件和子文件夹
*
* @param source 源文件
* @param target 目标文件
* @author terwer
* @since 1.0.0
*/
const copyFolderSync = (source: string, target: string): void => {
if (!fs.existsSync(target)) {
fs.mkdirSync(target)
}

if (fs.lstatSync(source).isDirectory()) {
const files: any[] = fs.readdirSync(source)
files.forEach(function (file: any) {
const curSource = path.join(source, file)
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderSync(curSource, path.join(target, file))
} else {
fs.copyFileSync(curSource, path.join(target, file))
}
})
}
}

/**
* 删除文件夹
*
* @param folder 文件夹
*/
const rmFolder = (folder: string) => {
if (fs.existsSync(folder)) {
// fs.rm(folder, { recursive: true, force: true })
fs.rmdirSync(folder, { recursive: true })
}
}

const nodeUtil = {
copyFolderSync,
rmFolder,
}

export default nodeUtil

0 comments on commit 2e6f855

Please sign in to comment.