Skip to content

Commit

Permalink
chore: 使用 zhi-cli 重构项目
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 9, 2023
1 parent de3518c commit 982c63c
Show file tree
Hide file tree
Showing 14 changed files with 628 additions and 73 deletions.
4 changes: 1 addition & 3 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import { build } from "vite"
import path from "path"
import dts from "vite-plugin-dts"

// libraries
const libraries = [
Expand All @@ -47,7 +46,6 @@ const libraries = [
for (const libItem of libraries) {
await build({
configFile: false,
plugins: [dts()],
resolve: {
alias: [
{
Expand Down Expand Up @@ -76,7 +74,7 @@ for (const libItem of libraries) {
return chunkName
},
},
external: ["path"],
external: ["path", "fs"],
},
},
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"vitest": "^0.29.2"
},
"dependencies": {
"compare-versions": "6.0.0-rc.1",
"zhi-log": "^1.5.5"
}
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src/apps/zhi/plugin-system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const initPluginSystem = (): DependencyItem[] => {
"/appearance/themes/zhi/dist-cjs/plugin-system/plugin-system-hook.cjs",
format: "cjs",
importType: "require",
runAs: "electron",
},
]
}
Expand Down
182 changes: 178 additions & 4 deletions src/apps/zhi/plugin-system/plugin-system-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,193 @@
*/

import ZhiUtil from "~/src/utils/ZhiUtil"
import strUtil from "~/src/utils/strUtil"
import nodeUtil from "~/src/utils/nodeUtil"
import pluginSystemUtil, {
HackPluginSystem,
} from "~/src/apps/zhi/plugin-system/pluginSystemUtil"
import siyuanUtil from "~/src/utils/siyuanUtil"
import { compareVersions } from "compare-versions"
import cjsUtil from "~/src/utils/cjsUtil"

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

/**
* 插件系统入口(由theme.js动态调用,请勿主动调用)
* vite构建配置:config/vite.cjs.config.plugin.system.hook
*
* @author terwer
* @since 1.0.0
*/
class PluginSystemHook {
private readonly logger

constructor() {
this.logger = ZhiUtil.zhiSdk().getLogger()
}

/**
* 获取插件同步信息
*
* @param p 插件系统对象
* @param zhiPlugin 插件对象
*/
getOldPluginInfo(p: any, zhiPlugin: any) {
let isSynced = false
let isUpdate = false
let oldVersion = pluginSystemUtil.OLD_VERSION_ZERO

const plugins = p.pslm.storageMangager.thirdPartyPlugins
for (const item of plugins) {
// this.logger.debug("Plugin=>", item)
// 不是当前插件跳过
if (zhiPlugin.name !== item.name) {
continue
}

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

oldVersion = item.version
isSynced = true
}

return { isSynced, oldVersion, isUpdate }
}

async syncZhiPlugins(p: any) {
const hack = new HackPluginSystem()

this.logger.info("Start syncing zhi plugins ...")

// 主题插件目录
const zhiPluginsPath = path.join(
siyuanUtil.ZHI_CJS_PATH(),
pluginSystemUtil.ZHI_PLUGIN_FOLDER
)
// this.logger.info("Zhi plugins folder=>", zhiPluginsPath)

// 插件系统默认目录
const pluginsPath = path.join(
siyuanUtil.SIYUAN_DATA_PATH(),
pluginSystemUtil.PLUGIN_FOLDER
)
// this.logger.info("Plugins folder=>", pluginsPath)

let syncedCount = 0
let zhiPlugins: any = []
// 未找到主题差距,不同步
if (!fs.existsSync(zhiPluginsPath)) {
this.logger.warn("No zhi plugins found, stop!")
} else {
// 扫描插件并同步
zhiPlugins = await hack.scanPlugins(zhiPluginsPath)
// this.logger.debug("zhiPlugins=>", zhiPlugins)
for (const item of zhiPlugins) {
const pluginBasename = path.basename(item)
const from = item
const to = path.join(pluginsPath, pluginBasename)
this.logger.debug(
strUtil.f("Try syncing zhi plugin {0}", pluginBasename)
)

const manifest = await hack.getManifest(
path.join(item, pluginSystemUtil.MANIFEST)
)
// this.logger.debug("ZhiPlugin=>", manifest)

const oldPluginInfo = this.getOldPluginInfo(p, manifest)
const oldVersion = oldPluginInfo.oldVersion
this.logger.info(
strUtil.f(
"Plugin status : [{0}] isSynced=>{1}, isUpdate=>{2}, forceUpdate=>{3}, version Info: {4} -> {5}",
pluginBasename,
oldPluginInfo.isSynced,
oldPluginInfo.isUpdate,
manifest.forceUpdate,
oldVersion,
manifest.version
)
)

// 同步需满足下面条件
// 1. 未同步过或者有新版本
// 2. 新旧插件注册信息目录均保持一致
if (!oldPluginInfo.isSynced) {
// 未同步过,但是目标目录已存在
if (fs.existsSync(to)) {
throw new Error(
strUtil.f("Expected forder already exists=>{0}", to)
)
}

strUtil.f("Do syncing, please wait...")
nodeUtil.copyFolderSync(from, to)
syncedCount++
} else if (oldPluginInfo.isSynced && oldPluginInfo.isUpdate) {
// 新插件目录不一致,但是有版本号
if (!fs.existsSync(to)) {
throw new Error(
strUtil.f(
"Conflict plugin exists, manifest exists but dest folder is not correct with original, please fix plugin folder name.Expected forder is=>{0}",
to
)
)
}

strUtil.f("Do syncing, please wait...")
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(
"Already synced and the latest version [{0}] {1}",
pluginBasename,
manifest.version
)
)
}
}
}

this.logger.info(
strUtil.f(
"Zhi theme plugins Synced.Scaned {0}, synced {1} plugin(s).",
zhiPlugins.length,
syncedCount
)
)

if (syncedCount > 0) {
this.logger.warn(
strUtil.f(
"Synced {0} zhi plugins, you need to reload siyuan to take effect.",
syncedCount
)
)
}
}

async init() {
// await pluginSystemUtil.initPluginSystem()
//
// const sys = await pluginSystemUtil.getPluginSystem()
// await this.syncZhiPlugins(sys)
await pluginSystemUtil.initPluginSystem()

const sys = await pluginSystemUtil.getPluginSystem()
await this.syncZhiPlugins(sys)

this.logger.info("PluginSystemHook inited.")
}
Expand Down
Loading

0 comments on commit 982c63c

Please sign in to comment.